|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Console\Command\Generate; |
| 4 | + |
| 5 | +use Drupal\Console\Core\Command\Command; |
| 6 | +use Drupal\Console\Core\Utils\ChainQueue; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Drupal\Console\Core\Generator\GeneratorInterface; |
| 10 | +use Drupal\Console\Command\Shared\ModuleTrait; |
| 11 | +use Drupal\Console\Command\Shared\ConfirmationTrait; |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use Drupal\Console\Utils\Validator; |
| 14 | +use Drupal\Console\Core\Utils\StringConverter; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class PluginQueueWorkerCommand. |
| 18 | + * |
| 19 | + * @package Drupal\Console\Command\Generate |
| 20 | + */ |
| 21 | +class PluginQueueWorkerCommand extends Command { |
| 22 | + |
| 23 | + use ModuleTrait; |
| 24 | + use ConfirmationTrait; |
| 25 | + |
| 26 | + /** |
| 27 | + * Drupal\Console\Core\Generator\GeneratorInterface definition. |
| 28 | + * |
| 29 | + * @var \Drupal\Console\Core\Generator\GeneratorInterface |
| 30 | + */ |
| 31 | + protected $generator; |
| 32 | + |
| 33 | + /** |
| 34 | + * Validator. |
| 35 | + * |
| 36 | + * @var \Drupal\Console\Utils\Validator |
| 37 | + */ |
| 38 | + protected $validator; |
| 39 | + |
| 40 | + /** |
| 41 | + * String converter. |
| 42 | + * |
| 43 | + * @var \Drupal\Console\Core\Utils\StringConverter |
| 44 | + */ |
| 45 | + protected $stringConverter; |
| 46 | + |
| 47 | + /** |
| 48 | + * Chain queue. |
| 49 | + * |
| 50 | + * @var \Drupal\Console\Core\Utils\ChainQueue |
| 51 | + */ |
| 52 | + protected $chainQueue; |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * PluginQueueWorkerCommand constructor. |
| 57 | + * |
| 58 | + * @param \Drupal\Console\Core\Generator\GeneratorInterface $queue_generator |
| 59 | + * Queue Generator. |
| 60 | + * @param \Drupal\Console\Utils\Validator $validator |
| 61 | + * Validator. |
| 62 | + * @param \Drupal\Console\Core\Utils\StringConverter $stringConverter |
| 63 | + * String Converter. |
| 64 | + * @param \Drupal\Console\Core\Utils\ChainQueue $chainQueue |
| 65 | + * Chain queue. |
| 66 | + */ |
| 67 | + public function __construct( |
| 68 | + GeneratorInterface $queue_generator, |
| 69 | + Validator $validator, |
| 70 | + StringConverter $stringConverter, |
| 71 | + ChainQueue $chainQueue |
| 72 | + ) { |
| 73 | + $this->generator = $queue_generator; |
| 74 | + $this->validator = $validator; |
| 75 | + $this->stringConverter = $stringConverter; |
| 76 | + $this->chainQueue = $chainQueue; |
| 77 | + parent::__construct(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritdoc} |
| 82 | + */ |
| 83 | + protected function configure() { |
| 84 | + $this |
| 85 | + ->setName('generate:plugin:queue') |
| 86 | + ->setDescription($this->trans('commands.generate.plugin.queue.description')) |
| 87 | + ->setHelp($this->trans('commands.generate.plugin.queue.help')) |
| 88 | + ->addOption( |
| 89 | + 'module', |
| 90 | + NULL, |
| 91 | + InputOption::VALUE_REQUIRED, |
| 92 | + $this->trans('commands.generate.plugin.queue.options.module') |
| 93 | + ) |
| 94 | + ->addOption( |
| 95 | + 'class', |
| 96 | + NULL, |
| 97 | + InputOption::VALUE_REQUIRED, |
| 98 | + $this->trans('commands.generate.plugin.queue.options.class') |
| 99 | + ) |
| 100 | + ->addOption( |
| 101 | + 'plugin-id', |
| 102 | + NULL, |
| 103 | + InputOption::VALUE_REQUIRED, |
| 104 | + $this->trans('commands.generate.plugin.queue.options.plugin-id') |
| 105 | + ) |
| 106 | + ->addOption( |
| 107 | + 'cron-time', |
| 108 | + NULL, |
| 109 | + InputOption::VALUE_REQUIRED, |
| 110 | + $this->trans('commands.generate.plugin.queue.options.cron-time') |
| 111 | + ) |
| 112 | + ->addOption( |
| 113 | + 'label', |
| 114 | + NULL, |
| 115 | + InputOption::VALUE_REQUIRED, |
| 116 | + $this->trans('commands.generate.plugin.queue.options.label') |
| 117 | + ) |
| 118 | + ->setAliases(['gpqueue']); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritdoc} |
| 124 | + */ |
| 125 | + protected function interact(InputInterface $input, OutputInterface $output) { |
| 126 | + // --module option. |
| 127 | + $this->getModuleOption(); |
| 128 | + |
| 129 | + // --class option. |
| 130 | + $queue_class = $input->getOption('class'); |
| 131 | + if (!$queue_class) { |
| 132 | + $queue_class = $this->getIo()->ask( |
| 133 | + $this->trans('commands.generate.plugin.queue.questions.class'), |
| 134 | + 'ExampleQueue', |
| 135 | + function ($queue_class) { |
| 136 | + return $this->validator->validateClassName($queue_class); |
| 137 | + } |
| 138 | + ); |
| 139 | + $input->setOption('class', $queue_class); |
| 140 | + } |
| 141 | + |
| 142 | + // --plugin-id option. |
| 143 | + $plugin_id = $input->getOption('plugin-id'); |
| 144 | + if (!$plugin_id) { |
| 145 | + $plugin_id = $this->getIo()->ask( |
| 146 | + $this->trans('commands.generate.plugin.queue.questions.plugin-id'), |
| 147 | + 'example_plugin_id', |
| 148 | + function ($plugin_id) { |
| 149 | + return $this->stringConverter->camelCaseToUnderscore($plugin_id); |
| 150 | + } |
| 151 | + ); |
| 152 | + $input->setOption('plugin-id', $plugin_id); |
| 153 | + } |
| 154 | + |
| 155 | + // --cron-time option. |
| 156 | + $cron_time = $input->getOption('cron-time'); |
| 157 | + if (!$cron_time) { |
| 158 | + $cron_time = $this->getIo()->ask( |
| 159 | + $this->trans('commands.generate.plugin.queue.questions.cron-time'), |
| 160 | + 30 |
| 161 | + ); |
| 162 | + $input->setOption('cron-time', $cron_time); |
| 163 | + } |
| 164 | + |
| 165 | + // --label option. |
| 166 | + $label = $input->getOption('label'); |
| 167 | + if (!$label) { |
| 168 | + $label = $this->getIo()->ask( |
| 169 | + $this->trans('commands.generate.plugin.queue.questions.label'), |
| 170 | + 'Queue description.' |
| 171 | + ); |
| 172 | + $input->setOption('label', $label); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * {@inheritdoc} |
| 178 | + */ |
| 179 | + protected function execute(InputInterface $input, OutputInterface $output) { |
| 180 | + // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation |
| 181 | + if (!$this->confirmOperation()) { |
| 182 | + return 1; |
| 183 | + } |
| 184 | + $module = $input->getOption('module'); |
| 185 | + $queue_class = $input->getOption('class'); |
| 186 | + $plugin_id = $input->getOption('plugin-id'); |
| 187 | + $cron_time = $input->getOption('cron-time'); |
| 188 | + $label = $input->getOption('label'); |
| 189 | + $this->generator->generate([ |
| 190 | + 'module' => $module, |
| 191 | + 'class_name' => $queue_class, |
| 192 | + 'plugin_id' => $plugin_id, |
| 193 | + 'cron_time' => $cron_time, |
| 194 | + 'label' => $label, |
| 195 | + ]); |
| 196 | + |
| 197 | + $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']); |
| 198 | + |
| 199 | + return 0; |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments