From aa5197b259cf67e239ca241e8a27bd452b6b3e78 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Sat, 6 Apr 2019 23:47:52 +0200 Subject: [PATCH] Generator of rules condition (#3827) * Add Rules Condition command * Add generate:plugin:rules:condition command. Generator for the command and templates --- config/services/generate.yml | 3 + config/services/generator.yml | 13 +- .../Generate/PluginRulesConditionCommand.php | 271 ++++++++++++++++++ .../PluginRulesConditionGenerator.php | 45 +++ .../Plugin/Condition/rulescondition.php.twig | 43 +++ 5 files changed, 371 insertions(+), 4 deletions(-) create mode 100644 src/Command/Generate/PluginRulesConditionCommand.php create mode 100644 src/Generator/PluginRulesConditionGenerator.php create mode 100644 templates/module/src/Plugin/Condition/rulescondition.php.twig diff --git a/config/services/generate.yml b/config/services/generate.yml index fdd721370..9ff8b0981 100644 --- a/config/services/generate.yml +++ b/config/services/generate.yml @@ -129,6 +129,9 @@ services: arguments: ['@console.extension_manager', '@console.plugin_rules_action_generator','@console.string_converter', '@console.validator', '@console.chain_queue'] tags: - { name: drupal.command } + console.generate_plugin_rules_condition: + class: Drupal\Console\Command\Generate\PluginRulesConditionCommand + arguments: ['@console.extension_manager', '@console.plugin_rules_condition_generator','@console.string_converter', '@console.validator', '@console.chain_queue'] console.generate_plugin_rules_dataprocessor: class: Drupal\Console\Command\Generate\PluginRulesDataprocessorCommand arguments: ['@console.extension_manager', '@console.plugin_rules_dataprocessor_generator','@console.string_converter', '@console.validator', '@console.chain_queue'] diff --git a/config/services/generator.yml b/config/services/generator.yml index 31193cc2f..9503963d3 100644 --- a/config/services/generator.yml +++ b/config/services/generator.yml @@ -126,11 +126,16 @@ services: arguments: ['@console.extension_manager'] tags: - { name: drupal.generator } + console.plugin_rules_condition_generator: + class: Drupal\Console\Generator\PluginRulesConditionGenerator + arguments: ['@console.extension_manager'] + tags: + - { name: drupal.generator } console.plugin_rules_dataprocessor_generator: - class: Drupal\Console\Generator\PluginRulesDataprocessorGenerator - arguments: ['@console.extension_manager'] - tags: - - { name: drupal.generator } + class: Drupal\Console\Generator\PluginRulesDataprocessorGenerator + arguments: ['@console.extension_manager'] + tags: + - { name: drupal.generator } console.plugin_skeleton_generator: class: Drupal\Console\Generator\PluginSkeletonGenerator arguments: ['@console.extension_manager'] diff --git a/src/Command/Generate/PluginRulesConditionCommand.php b/src/Command/Generate/PluginRulesConditionCommand.php new file mode 100644 index 000000000..048255b39 --- /dev/null +++ b/src/Command/Generate/PluginRulesConditionCommand.php @@ -0,0 +1,271 @@ +extensionManager = $extensionManager; + $this->generator = $generator; + $this->stringConverter = $stringConverter; + $this->validator = $validator; + $this->chainQueue = $chainQueue; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('generate:plugin:rules:condition') + ->setDescription($this->trans('commands.generate.plugin.rules.condition.description')) + ->setHelp($this->trans('commands.generate.plugin.rules.condition.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.condition.options.class') + ) + ->addOption( + 'label', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.plugin.rules.condition.options.label') + ) + ->addOption( + 'plugin-id', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.plugin.rules.condition.options.plugin-id') + ) + ->addOption( + 'category', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.plugin.rules.condition.options.category') + ) + ->addOption( + 'context', + null, + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + $this->trans('commands.generate.plugin.rules.condition.options.context') + ) + ->setAliases(['gprc']); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation + if (!$this->confirmOperation()) { + return 1; + } + + $module = $input->getOption('module'); + $class_name = $this->validator->validateClassName($input->getOption('class')); + $label = $input->getOption('label'); + $plugin_id = $input->getOption('plugin-id'); + $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, + ]); + + $this->chainQueue->addCommand('cache:rebuild', + ['cache' => 'discovery']); + + return 0; + } + + protected function interact(InputInterface $input, OutputInterface $output) + { + // --module option + $this->getModuleOption(); + + // --class option + $class_name = $input->getOption('class'); + if (!$class_name) { + $class_name = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.options.class'), + 'DefaultCondition', + function ($class_name) { + return $this->validator->validateClassName($class_name); + } + ); + $input->setOption('class', $class_name); + } + + // --label option + $label = $input->getOption('label'); + if (!$label) { + $label = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.options.label'), + $this->stringConverter->camelCaseToHuman($class_name) + ); + $input->setOption('label', $label); + } + + // --plugin-id option + $plugin_id = $input->getOption('plugin-id'); + if (!$plugin_id) { + $plugin_id = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.options.plugin-id'), + $this->stringConverter->camelCaseToUnderscore($class_name) + ); + $input->setOption('plugin-id', $plugin_id); + } + + // --category option + $category = $input->getOption('category'); + if (!$category) { + $category = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.options.category'), + $this->stringConverter->camelCaseToUnderscore($class_name) + ); + $input->setOption('category', $category); + } + + // --context option + $context = $input->getOption('context'); + if (empty($context)) { + + $context = []; + if ($this->getIo()->confirm( + $this->trans('commands.generate.plugin.rules.condition.questions.context'), + true + )) { + while (true) { + $this->getIo()->newLine(); + + $input_name = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.questions.context-name') + ); + + $input_type = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.questions.context-type') + ); + + $input_label = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.questions.context-label') + ); + + $input_description = $this->getIo()->ask( + $this->trans('commands.generate.plugin.rules.condition.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.condition.questions.another-context'), + true + )) { + break; + } + } + } + } else { + $context = $this->explodeInlineArray($context); + } + + $input->setOption('context', $context); + } +} diff --git a/src/Generator/PluginRulesConditionGenerator.php b/src/Generator/PluginRulesConditionGenerator.php new file mode 100644 index 000000000..97453f6e0 --- /dev/null +++ b/src/Generator/PluginRulesConditionGenerator.php @@ -0,0 +1,45 @@ +extensionManager = $extensionManager; + } + + /** + * {@inheritdoc} + */ + public function generate(array $parameters) + { + $module = $parameters['module']; + $class_name = $parameters['class_name']; + + $this->renderFile( + 'module/src/Plugin/Condition/rulescondition.php.twig', + $this->extensionManager->getPluginPath($module, 'Condition') . '/' . $class_name . '.php', + $parameters + ); + } +} diff --git a/templates/module/src/Plugin/Condition/rulescondition.php.twig b/templates/module/src/Plugin/Condition/rulescondition.php.twig new file mode 100644 index 000000000..8b925752d --- /dev/null +++ b/templates/module/src/Plugin/Condition/rulescondition.php.twig @@ -0,0 +1,43 @@ +{% extends "base/class.php.twig" %} + +{% block file_path %} +\Drupal\{{module}}\Plugin\Condition\{{class_name}}. +{% endblock %} + +{% block namespace_class %} +namespace Drupal\{{module}}\Plugin\Condition; +{% endblock %} + +{% block use_class %} +use Drupal\rules\Core\RulesConditionBase; +{% endblock %} + +{% block class_declaration %} +/** + * Provides a '{{class_name}}' condition. + * + * @Condition( + * id = "{{plugin_id}}", + * label = @Translation("{{label}}"), + * category = @Translation("{{category}}"), +{% if context %} + * context = { +{% for item in context %} + * "{{ item.name }}" = @ContextDefinition("{{ item.type }}", + * label = @Translation("{{ item.label }}"), + * description = @Translation("{{ item.description }}") + * ), +{% endfor %} + * } +{% endif %} + * ) + */ +class {{class_name}} extends RulesConditionBase {% endblock %} +{% block class_methods %} + /** + * {@inheritdoc} + */ + public function doEvaluate($object = NULL) { + // Insert code here. + } +{% endblock %} \ No newline at end of file