GenerateProxyClassCommand.php 2.7 KB

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