1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<?php
namespace Drupal\Core\Command;
use Drupal\Component\ProxyBuilder\ProxyBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Provides a console command to generate proxy classes.
*
* @see lazy_services
* @see core/scripts/generate-proxy.sh
*/
class GenerateProxyClassCommand extends Command {
/**
* The proxy builder.
*
* @var \Drupal\Component\ProxyBuilder\ProxyBuilder
*/
protected $proxyBuilder;
/**
* Constructs a new GenerateProxyClassCommand instance.
*
* @param \Drupal\Component\ProxyBuilder\ProxyBuilder $proxy_builder
* The proxy builder.
*/
public function __construct(ProxyBuilder $proxy_builder) {
parent::__construct();
$this->proxyBuilder = $proxy_builder;
}
/**
* {@inheritdoc}
*/
protected function configure() {
$this->setName('generate-proxy-class')
->setDefinition([
new InputArgument('class_name', InputArgument::REQUIRED, 'The class to be proxied'),
new InputArgument('namespace_root_path', InputArgument::REQUIRED, 'The filepath to the root of the namespace.'),
])
->setDescription('Dumps a generated proxy class into its appropriate namespace.')
->addUsage('\'Drupal\Core\Batch\BatchStorage\' "core/lib/Drupal/Core"')
->addUsage('\'Drupal\block\BlockRepository\' "core/modules/block/src"')
->addUsage('\'Drupal\my_module\MyClass\' "modules/contrib/my_module/src"');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$class_name = ltrim($input->getArgument('class_name'), '\\');
$namespace_root = $input->getArgument('namespace_root_path');
$match = [];
preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match);
if ($match) {
$root_namespace = $match[1];
$rest_fqcn = $match[2];
$proxy_filename = $namespace_root . '/ProxyClass/' . str_replace('\\', '/', $rest_fqcn) . '.php';
$proxy_class_name = $root_namespace . '\\ProxyClass\\' . $rest_fqcn;
$proxy_class_string = $this->proxyBuilder->build($class_name);
$file_string = <<<EOF
<?php
// phpcs:ignoreFile
/**
* This file was generated via php core/scripts/generate-proxy-class.php '$class_name' "$namespace_root".
*/
{{ proxy_class_string }}
EOF;
$file_string = str_replace(
['{{ proxy_class_name }}', '{{ proxy_class_string }}'],
[$proxy_class_name, $proxy_class_string],
$file_string
);
mkdir(dirname($proxy_filename), 0775, TRUE);
file_put_contents($proxy_filename, $file_string);
$output->writeln(sprintf('Proxy of class %s written to %s', $class_name, $proxy_filename));
}
return 0;
}
}
|