diff --git a/src/Command/Generate/CommandCommand.php b/src/Command/Generate/CommandCommand.php index 887d6e5f7..9f06a03ab 100644 --- a/src/Command/Generate/CommandCommand.php +++ b/src/Command/Generate/CommandCommand.php @@ -134,6 +134,12 @@ protected function configure() InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $this->trans('commands.common.options.services') ) + ->addOption( + 'generator', + null, + InputOption::VALUE_NONE, + $this->trans('commands.generate.command.options.generator') + ) ->setAliases(['gco']); } @@ -150,6 +156,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $interact = $input->getOption('interact'); $containerAware = $input->getOption('container-aware'); $services = $input->getOption('services'); + $generator = $input->getOption('generator'); // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration if (!$this->confirmGeneration()) { @@ -159,6 +166,11 @@ protected function execute(InputInterface $input, OutputInterface $output) // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices $build_services = $this->buildServices($services); + $class_generator = null; + if ($generator) { + $class_generator = str_replace('Command', 'Generator', $class); + } + $this->generator->generate( $extension, $extensionType, @@ -167,7 +179,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $interact, $class, $containerAware, - $build_services + $build_services, + $generator, + $class_generator ); $this->site->removeCachedServicesFile(); @@ -246,5 +260,14 @@ function ($class) { $services = $this->servicesQuestion(); $input->setOption('services', $services); } + + $generator = $input->getOption('generator'); + if (!$generator) { + $generator = $this->getIo()->confirm( + $this->trans('commands.generate.command.questions.generator'), + false + ); + $input->setOption('generator', $generator); + } } } diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index 576afc859..f664d1435 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -106,6 +106,15 @@ public function getCommandDirectory($fullPath=false) return $this->getSourcePath($fullPath) . '/Command/'; } + /** + * @param bool $fullPath + * @return string + */ + public function getGeneratorDirectory($fullPath=false) + { + return $this->getSourcePath($fullPath) . '/Generator/'; + } + /** * @param bool $fullPath * @return string diff --git a/src/Generator/CommandGenerator.php b/src/Generator/CommandGenerator.php index 132185a45..be9a6f919 100644 --- a/src/Generator/CommandGenerator.php +++ b/src/Generator/CommandGenerator.php @@ -45,14 +45,16 @@ public function __construct( /** * Generate. * - * @param string $extension Extension name - * @param string $extensionType Extension type - * @param string $name Command name - * @param string $initialize Initialize method - * @param string $interact Interact method - * @param string $class Class name - * @param boolean $containerAware Container Aware command - * @param array $services Services array + * @param string $extension Extension name + * @param string $extensionType Extension type + * @param string $name Command name + * @param string $initialize Initialize method + * @param string $interact Interact method + * @param string $class Class name + * @param boolean $containerAware Container Aware command + * @param array $services Services array + * @param boolean $generator Generate generator + * @param string $class_generator Generator Class name */ public function generate( $extension, @@ -62,11 +64,14 @@ public function generate( $interact, $class, $containerAware, - $services + $services, + $generator, + $class_generator ) { $command_key = str_replace(':', '.', $name); - $extensionObject = $this->extensionManager->getDrupalExtension($extensionType, $extension); + $extensionObject = $this->extensionManager + ->getDrupalExtension($extensionType, $extension); $parameters = [ 'extension' => $extension, @@ -78,18 +83,34 @@ public function generate( 'container_aware' => $containerAware, 'command_key' => $command_key, 'services' => $services, - 'tags' => ['name' => 'drupal.command'], + 'tags' => [ 'name' => 'drupal.command' ], 'class_path' => sprintf('Drupal\%s\Command\%s', $extension, $class), 'file_exists' => file_exists($extensionObject->getPath().'/console.services.yml'), + 'class_generator' => $class_generator, + 'class_generator_path' => sprintf('Drupal\%s\Command\%s', $extension, $class_generator), ]; + $commandServiceName = $extension.'.'.str_replace(':', '_', $name); + $generatorServiceName = $commandServiceName.'_generator'; + + if ($generator) { + $machineName = str_replace('.', '_', $generatorServiceName); + $parameters['services'][$generatorServiceName] = [ + 'name' => $generatorServiceName, + 'machine_name' => $machineName, + 'camel_case_name' => 'generator', + 'class' => 'Drupal\Console\Core\Generator\GeneratorInterface', + 'short' => 'GeneratorInterface', + ]; + } + $this->renderFile( 'module/src/Command/command.php.twig', $extensionObject->getCommandDirectory().$class.'.php', $parameters ); - $parameters['name'] = $extension.'.'.str_replace(':', '_', $name); + $parameters['name'] = $commandServiceName; $this->renderFile( 'module/services.yml.twig', @@ -102,5 +123,34 @@ public function generate( 'module/src/Command/console/translations/en/command.yml.twig', $extensionObject->getPath().'/console/translations/en/'.$command_key.'.yml' ); + + if ($generator) { + $this->renderFile( + 'module/src/Generator/generator.php.twig', + $extensionObject->getGeneratorDirectory() . $class_generator . '.php', + $parameters + ); + + $parameters = array_merge( + $parameters, + [ + 'name' => $generatorServiceName, + 'class_name' => $class_generator, + 'services' => [], + 'tags' => [ 'name' => 'drupal.generator' ], + 'class_path' => sprintf('Drupal\%s\Generator\%s', $extension, $class_generator), + 'file_exists' => file_exists($extensionObject->getPath().'/console.services.yml'), + 'class_generator' => $class_generator, + 'class_generator_path' => sprintf('Drupal\%s\Generator\%s', $extension, $class_generator), + ] + ); + + $this->renderFile( + 'module/services.yml.twig', + $extensionObject->getPath() .'/console.services.yml', + $parameters, + FILE_APPEND + ); + } } } diff --git a/templates/module/src/Command/command.php.twig b/templates/module/src/Command/command.php.twig index 2b22077e9..6724ef93b 100644 --- a/templates/module/src/Command/command.php.twig +++ b/templates/module/src/Command/command.php.twig @@ -77,6 +77,9 @@ class {{ class_name }} extends {% if container_aware %}ContainerAwareCommand{% e protected function execute(InputInterface $input, OutputInterface $output) { $this->getIo()->info('execute'); $this->getIo()->info($this->trans('commands.{{ command_key }}.messages.success')); +{% if class_generator %} + $this->generator->generate([]); +{% endif %} } {%- endblock -%} diff --git a/templates/module/src/Generator/generator.php.twig b/templates/module/src/Generator/generator.php.twig new file mode 100644 index 000000000..0d3195ce1 --- /dev/null +++ b/templates/module/src/Generator/generator.php.twig @@ -0,0 +1,38 @@ +{% extends "base/class.php.twig" %} + +{% block file_path %} +\Drupal\{{extension}}\Generator\{{ class_generator }}. +{% endblock %} + +{% block namespace_class %} +namespace Drupal\{{extension}}\Generator; +{% endblock %} + +{% block use_class %} +use Drupal\Console\Core\Generator\Generator; +use Drupal\Console\Core\Generator\GeneratorInterface; +{% endblock %} + +{% block class_declaration %} +/** + * Class {{ class_generator }} + * + * @package Drupal\Console\Generator + */ +class {{ class_generator }} extends Generator implements GeneratorInterface +{% endblock %} + +{% block class_methods %} + /** + * {@inheritdoc} + */ + public function generate(array $parameters) + { +// Example how to render a twig template using the renderFile method +// $this->renderFile( +// 'path/to/file.php.twig', +// 'path/to/file.php', +// $parameters +// ); + } +{% endblock %}