Skip to content

Commit

Permalink
[generate:command] Add generator option. (#3720)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Jan 22, 2018
1 parent 723d580 commit edd12a9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 13 deletions.
25 changes: 24 additions & 1 deletion src/Command/Generate/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand All @@ -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()) {
Expand All @@ -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,
Expand All @@ -167,7 +179,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$interact,
$class,
$containerAware,
$build_services
$build_services,
$generator,
$class_generator
);

$this->site->removeCachedServicesFile();
Expand Down Expand Up @@ -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);
}
}
}
9 changes: 9 additions & 0 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 62 additions & 12 deletions src/Generator/CommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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
);
}
}
}
3 changes: 3 additions & 0 deletions templates/module/src/Command/command.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
38 changes: 38 additions & 0 deletions templates/module/src/Generator/generator.php.twig
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit edd12a9

Please sign in to comment.