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