-
-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generate:plugin:queue] Create new command to generate QueueWorker (#…
…3950) * Added command Plugin Queueworker. * Create template twig queue worker. * Plugin Queueworker generator. * Define services queue plugin. * Fix services queue worker. * Fix generator queue. * Rename class option plugin queueworker genertor command class. * Rename queue_id to plugin id. * Added doc comment twig queue worker. * Added comment block class. * Comment queue_worker. * Not print docblock if class_path is not defined. * Fix spaces class base twig. * Remove comments class files. * Remove extension manager.
- Loading branch information
1 parent
098f487
commit 5e668f6
Showing
5 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
use Drupal\Console\Core\Command\Command; | ||
use Drupal\Console\Core\Utils\ChainQueue; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Drupal\Console\Core\Generator\GeneratorInterface; | ||
use Drupal\Console\Command\Shared\ModuleTrait; | ||
use Drupal\Console\Command\Shared\ConfirmationTrait; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Drupal\Console\Utils\Validator; | ||
use Drupal\Console\Core\Utils\StringConverter; | ||
|
||
/** | ||
* Class PluginQueueWorkerCommand. | ||
* | ||
* @package Drupal\Console\Command\Generate | ||
*/ | ||
class PluginQueueWorkerCommand extends Command { | ||
|
||
use ModuleTrait; | ||
use ConfirmationTrait; | ||
|
||
/** | ||
* Drupal\Console\Core\Generator\GeneratorInterface definition. | ||
* | ||
* @var \Drupal\Console\Core\Generator\GeneratorInterface | ||
*/ | ||
protected $generator; | ||
|
||
/** | ||
* Validator. | ||
* | ||
* @var \Drupal\Console\Utils\Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* String converter. | ||
* | ||
* @var \Drupal\Console\Core\Utils\StringConverter | ||
*/ | ||
protected $stringConverter; | ||
|
||
/** | ||
* Chain queue. | ||
* | ||
* @var \Drupal\Console\Core\Utils\ChainQueue | ||
*/ | ||
protected $chainQueue; | ||
|
||
|
||
/** | ||
* PluginQueueWorkerCommand constructor. | ||
* | ||
* @param \Drupal\Console\Core\Generator\GeneratorInterface $queue_generator | ||
* Queue Generator. | ||
* @param \Drupal\Console\Utils\Validator $validator | ||
* Validator. | ||
* @param \Drupal\Console\Core\Utils\StringConverter $stringConverter | ||
* String Converter. | ||
* @param \Drupal\Console\Core\Utils\ChainQueue $chainQueue | ||
* Chain queue. | ||
*/ | ||
public function __construct( | ||
GeneratorInterface $queue_generator, | ||
Validator $validator, | ||
StringConverter $stringConverter, | ||
ChainQueue $chainQueue | ||
) { | ||
$this->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
use Drupal\Console\Core\Generator\GeneratorInterface; | ||
|
||
/** | ||
* Class PluginQueueWorkerGenerator. | ||
* | ||
* @package Drupal\Console\Generator | ||
*/ | ||
class PluginQueueWorkerGenerator extends Generator implements GeneratorInterface { | ||
|
||
/** | ||
* Extension Manager. | ||
* | ||
* @var \Drupal\Console\Extension\Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* PluginQueueWorker constructor. | ||
* | ||
* @param \Drupal\Console\Extension\Manager $extensionManager | ||
* Extension manager. | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager | ||
) { | ||
$this->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 | ||
); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
templates/module/src/Plugin/QueueWorker/queue_worker.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |