Skip to content

Commit

Permalink
Renovated command generate:plugin:rules:action (#3824)
Browse files Browse the repository at this point in the history
  • Loading branch information
LOBsTerr committed Apr 6, 2019
1 parent 7103912 commit a626074
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 164 deletions.
238 changes: 139 additions & 99 deletions src/Command/Generate/PluginRulesActionCommand.php
Expand Up @@ -7,18 +7,19 @@

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Command\Shared\ArrayInputTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\Shared\ServicesTrait;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Generator\PluginRulesActionGenerator;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\Shared\ServicesTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;

/**
* Class PluginRulesActionCommand
Expand All @@ -27,18 +28,20 @@
*/
class PluginRulesActionCommand extends Command
{
use ServicesTrait;
use ModuleTrait;

use ArrayInputTrait;
use ConfirmationTrait;
use ModuleTrait;
use ServicesTrait;

/**
* @var Manager
*/
* @var Manager
*/
protected $extensionManager;

/**
* @var PluginRulesActionGenerator
*/
* @var PluginRulesActionGenerator
*/
protected $generator;

/**
Expand All @@ -60,18 +63,18 @@ class PluginRulesActionCommand extends Command
/**
* PluginRulesActionCommand constructor.
*
* @param Manager $extensionManager
* @param Manager $extensionManager
* @param PluginRulesActionGenerator $generator
* @param StringConverter $stringConverter
* @param Validator $validator
* @param ChainQueue $chainQueue
* @param StringConverter $stringConverter
* @param Validator $validator
* @param ChainQueue $chainQueue
*/
public function __construct(
Manager $extensionManager,
PluginRulesActionGenerator $generator,
StringConverter $stringConverter,
Validator $validator,
ChainQueue $chainQueue
Manager $extensionManager,
PluginRulesActionGenerator $generator,
StringConverter $stringConverter,
Validator $validator,
ChainQueue $chainQueue
) {
$this->extensionManager = $extensionManager;
$this->generator = $generator;
Expand All @@ -84,47 +87,46 @@ public function __construct(
protected function configure()
{
$this
->setName('generate:plugin:rulesaction')
->setDescription($this->trans('commands.generate.plugin.rulesaction.description'))
->setHelp($this->trans('commands.generate.plugin.rulesaction.help'))
->addOption(
'module',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rulesaction.options.class')
)
->addOption(
'label',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rulesaction.options.label')
)
->addOption(
'plugin-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rulesaction.options.plugin-id')
)
->addOption('type', null, InputOption::VALUE_REQUIRED, $this->trans('commands.generate.plugin.rulesaction.options.type'))
->addOption(
'category',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.plugin.rulesaction.options.category')
)
->addOption(
'context',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rulesaction.options.context')
)
->setAliases(['gpra']);
->setName('generate:plugin:rules:action')
->setDescription($this->trans('commands.generate.plugin.rules.action.description'))
->setHelp($this->trans('commands.generate.plugin.rules.action.help'))
->addOption(
'module',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rules.action.options.class')
)
->addOption(
'label',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rules.action.options.label')
)
->addOption(
'plugin-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rules.action.options.plugin-id')
)
->addOption(
'category',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.rules.action.options.category')
)
->addOption(
'context',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.plugin.rules.action.options.context')
)
->setAliases(['gpra']);
}

/**
Expand All @@ -141,21 +143,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
$class_name = $this->validator->validateClassName($input->getOption('class'));
$label = $input->getOption('label');
$plugin_id = $input->getOption('plugin-id');
$type = $input->getOption('type');
$category = $input->getOption('category');
$context = $input->getOption('context');
$noInteraction = $input->getOption('no-interaction');

// Parse nested data.
if ($noInteraction) {
$context = $this->explodeInlineArray($context);
}

$this->generator->generate([
'module' => $module,
'class_name' => $class_name,
'label' => $label,
'plugin_id' => $plugin_id,
'category' => $category,
'context' => $context,
'type' => $type,
'module' => $module,
'class_name' => $class_name,
'label' => $label,
'plugin_id' => $plugin_id,
'category' => $category,
'context' => $context,
]);

$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
$this->chainQueue->addCommand('cache:rebuild',
['cache' => 'discovery']);

return 0;
}
Expand All @@ -169,11 +176,11 @@ protected function interact(InputInterface $input, OutputInterface $output)
$class_name = $input->getOption('class');
if (!$class_name) {
$class_name = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.class'),
'DefaultAction',
function ($class_name) {
return $this->validator->validateClassName($class_name);
}
$this->trans('commands.generate.plugin.rules.action.options.class'),
'DefaultAction',
function ($class_name) {
return $this->validator->validateClassName($class_name);
}
);
$input->setOption('class', $class_name);
}
Expand All @@ -182,8 +189,8 @@ function ($class_name) {
$label = $input->getOption('label');
if (!$label) {
$label = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.label'),
$this->stringConverter->camelCaseToHuman($class_name)
$this->trans('commands.generate.plugin.rules.action.options.label'),
$this->stringConverter->camelCaseToHuman($class_name)
);
$input->setOption('label', $label);
}
Expand All @@ -192,40 +199,73 @@ function ($class_name) {
$plugin_id = $input->getOption('plugin-id');
if (!$plugin_id) {
$plugin_id = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.plugin-id'),
$this->stringConverter->camelCaseToUnderscore($class_name)
$this->trans('commands.generate.plugin.rules.action.options.plugin-id'),
$this->stringConverter->camelCaseToUnderscore($class_name)
);
$input->setOption('plugin-id', $plugin_id);
}

// --type option
$type = $input->getOption('type');
if (!$type) {
$type = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.type'),
'user'
);
$input->setOption('type', $type);
}

// --category option
$category = $input->getOption('category');
if (!$category) {
$category = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.category'),
$this->stringConverter->camelCaseToUnderscore($class_name)
$this->trans('commands.generate.plugin.rules.action.options.category'),
$this->stringConverter->camelCaseToUnderscore($class_name)
);
$input->setOption('category', $category);
}

// --context option
$context = $input->getOption('context');
if (!$context) {
$context = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rulesaction.options.context'),
$this->stringConverter->camelCaseToUnderscore($class_name)
);
$input->setOption('context', $context);
if (empty($context)) {

$context = [];
if ($this->getIo()->confirm(
$this->trans('commands.generate.plugin.rules.action.questions.context'),
true
)) {
while (true) {
$this->getIo()->newLine();

$input_name = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rules.action.questions.context-name')
);

$input_type = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rules.action.questions.context-type')
);

$input_label = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rules.action.questions.context-label')
);

$input_description = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rules.action.questions.context-description')
);

array_push(
$context,
[
'name' => $input_name,
'type' => $input_type,
'label' => $input_label,
'description' => $input_description,
]
);

$this->getIo()->newLine();
if (!$this->getIo()->confirm(
$this->trans('commands.generate.plugin.rules.action.questions.another-context'),
true
)) {
break;
}
}
}
} else {
$context = $this->explodeInlineArray($context);
}

$input->setOption('context', $context);
}
}
2 changes: 1 addition & 1 deletion src/Command/Shared/ArrayInputTrait.php
Expand Up @@ -30,7 +30,7 @@ public function explodeInlineArray($inlineInputs)
$parameters = [];
foreach ($explodeInput as $inlineParameter) {
$inlineParameter = trim($inlineParameter);
list($key, $value) = explode(':', $inlineParameter);
list($key, $value) = explode('":"', $inlineParameter);
$key = rtrim(ltrim($key, '"'), '"');
$value = rtrim(ltrim($value, '"'), '"');
if (!empty($value)) {
Expand Down
11 changes: 2 additions & 9 deletions src/Generator/PluginRulesActionGenerator.php
Expand Up @@ -35,17 +35,10 @@ public function generate(array $parameters)
{
$module = $parameters['module'];
$class_name = $parameters['class_name'];
$plugin_id = $parameters['plugin_id'];

$this->renderFile(
'module/src/Plugin/Action/rulesaction.php.twig',
$this->extensionManager->getPluginPath($module, 'Action') . '/' . $class_name . '.php',
$parameters
);

$this->renderFile(
'module/system.action.action.yml.twig',
$this->extensionManager->getModule($module)->getPath() . '/config/install/system.action.' . $plugin_id . '.yml',
'module/src/Plugin/RulesAction/rulesaction.php.twig',
$this->extensionManager->getPluginPath($module, 'RulesAction') . '/' . $class_name . '.php',
$parameters
);
}
Expand Down

0 comments on commit a626074

Please sign in to comment.