diff --git a/config/services/generate.yml b/config/services/generate.yml index 02fc07ab3..d07647ef6 100644 --- a/config/services/generate.yml +++ b/config/services/generate.yml @@ -144,6 +144,11 @@ services: arguments: ['@console.extension_manager', '@console.plugin_views_field_generator', '@console.site','@console.string_converter', '@console.validator', '@console.chain_queue'] tags: - { name: drupal.command } + console.generate_plugin_queue: + class: Drupal\Console\Command\Generate\PluginQueueWorkerCommand + arguments: ['@console.plugin_queue_generator','@console.validator','@console.string_converter','@console.chain_queue'] + tags: + - { name: drupal.command } console.generate_post_update: class: Drupal\Console\Command\Generate\PostUpdateCommand arguments: ['@console.extension_manager', '@console.post_update_generator', '@console.site', '@console.validator','@console.chain_queue'] diff --git a/config/services/generator.yml b/config/services/generator.yml index d99ed62ed..7db7cbb85 100644 --- a/config/services/generator.yml +++ b/config/services/generator.yml @@ -146,6 +146,11 @@ services: arguments: ['@console.extension_manager'] tags: - { name: drupal.generator } + console.plugin_queue_generator: + class: Drupal\Console\Generator\PluginQueueWorkerGenerator + arguments: ['@console.extension_manager'] + tags: + - { name: drupal.generator } console.route_subscriber_generator: class: Drupal\Console\Generator\RouteSubscriberGenerator arguments: ['@console.extension_manager'] diff --git a/src/Command/Generate/PluginQueueWorkerCommand.php b/src/Command/Generate/PluginQueueWorkerCommand.php new file mode 100644 index 000000000..9832e5432 --- /dev/null +++ b/src/Command/Generate/PluginQueueWorkerCommand.php @@ -0,0 +1,202 @@ +generator = $queue_generator; + $this->validator = $validator; + $this->stringConverter = $stringConverter; + $this->chainQueue = $chainQueue; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() { + $this + ->setName('generate:plugin:queue') + ->setDescription($this->trans('commands.generate.plugin.queue.description')) + ->setHelp($this->trans('commands.generate.plugin.queue.help')) + ->addOption( + 'module', + NULL, + InputOption::VALUE_REQUIRED, + $this->trans('commands.generate.plugin.queue.options.module') + ) + ->addOption( + 'class', + NULL, + InputOption::VALUE_REQUIRED, + $this->trans('commands.generate.plugin.queue.options.class') + ) + ->addOption( + 'plugin-id', + NULL, + InputOption::VALUE_REQUIRED, + $this->trans('commands.generate.plugin.queue.options.plugin-id') + ) + ->addOption( + 'cron-time', + NULL, + InputOption::VALUE_REQUIRED, + $this->trans('commands.generate.plugin.queue.options.cron-time') + ) + ->addOption( + 'label', + NULL, + InputOption::VALUE_REQUIRED, + $this->trans('commands.generate.plugin.queue.options.label') + ) + ->setAliases(['gpqueue']); + } + + + /** + * {@inheritdoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) { + // --module option. + $this->getModuleOption(); + + // --class option. + $queue_class = $input->getOption('class'); + if (!$queue_class) { + $queue_class = $this->getIo()->ask( + $this->trans('commands.generate.plugin.queue.questions.class'), + 'ExampleQueue', + function ($queue_class) { + return $this->validator->validateClassName($queue_class); + } + ); + $input->setOption('class', $queue_class); + } + + // --plugin-id option. + $plugin_id = $input->getOption('plugin-id'); + if (!$plugin_id) { + $plugin_id = $this->getIo()->ask( + $this->trans('commands.generate.plugin.queue.questions.plugin-id'), + 'example_plugin_id', + function ($plugin_id) { + return $this->stringConverter->camelCaseToUnderscore($plugin_id); + } + ); + $input->setOption('plugin-id', $plugin_id); + } + + // --cron-time option. + $cron_time = $input->getOption('cron-time'); + if (!$cron_time) { + $cron_time = $this->getIo()->ask( + $this->trans('commands.generate.plugin.queue.questions.cron-time'), + 30 + ); + $input->setOption('cron-time', $cron_time); + } + + // --label option. + $label = $input->getOption('label'); + if (!$label) { + $label = $this->getIo()->ask( + $this->trans('commands.generate.plugin.queue.questions.label'), + 'Queue description.' + ); + $input->setOption('label', $label); + } + } + + /** + * {@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'); + $queue_class = $input->getOption('class'); + $plugin_id = $input->getOption('plugin-id'); + $cron_time = $input->getOption('cron-time'); + $label = $input->getOption('label'); + $this->generator->generate([ + 'module' => $module, + 'class_name' => $queue_class, + 'plugin_id' => $plugin_id, + 'cron_time' => $cron_time, + 'label' => $label, + ]); + + $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']); + + return 0; + } + +} diff --git a/src/Generator/PluginQueueWorkerGenerator.php b/src/Generator/PluginQueueWorkerGenerator.php new file mode 100644 index 000000000..1262ea03e --- /dev/null +++ b/src/Generator/PluginQueueWorkerGenerator.php @@ -0,0 +1,50 @@ +extensionManager = $extensionManager; + } + + /** + * {@inheritdoc} + */ + public function generate(array $parameters) { + $module = $parameters['module']; + $queue_class = $parameters['class_name']; + + $this->renderer->addSkeletonDir(__DIR__ . '/../../console/templates'); + $this->renderFile( + 'module/src/Plugin/QueueWorker/queue_worker.php.twig', + $this->extensionManager->getPluginPath($module, 'QueueWorker') . '/' . $queue_class . '.php', + $parameters + ); + } + +} diff --git a/templates/module/src/Plugin/QueueWorker/queue_worker.php.twig b/templates/module/src/Plugin/QueueWorker/queue_worker.php.twig new file mode 100644 index 000000000..911f0a711 --- /dev/null +++ b/templates/module/src/Plugin/QueueWorker/queue_worker.php.twig @@ -0,0 +1,29 @@ +{% extends "base/class.php.twig" %} + +{% block namespace_class %} +namespace Drupal\{{ module }}\Plugin\QueueWorker; +{% endblock %} + +{% block use_class %} +use Drupal\Core\Queue\QueueWorkerBase; +{% endblock %} + +{% block class_declaration %} +/** + * Plugin implementation od the {{ plugin_id }} queueworker. + * + * @QueueWorker ( + * id = "{{ plugin_id }}", + * title = @Translation("{{ label }}"), + * cron = {"time" = {{ cron_time }}} + * ) + */ +class {{ class_name }} extends QueueWorkerBase {% endblock %} +{% block class_methods %} + /** + * {@inheritdoc} + */ + public function processItem($data) { + // Process item operations. + } +{% endblock %}