Skip to content

Commit

Permalink
[generate:plugin:queue] Create new command to generate QueueWorker (#…
Browse files Browse the repository at this point in the history
…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
Eduardo-Morales-Alberti authored and LOBsTerr committed Apr 6, 2019
1 parent 098f487 commit 5e668f6
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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']
Expand Down
5 changes: 5 additions & 0 deletions config/services/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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']
Expand Down
202 changes: 202 additions & 0 deletions src/Command/Generate/PluginQueueWorkerCommand.php
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;
}

}
50 changes: 50 additions & 0 deletions src/Generator/PluginQueueWorkerGenerator.php
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 templates/module/src/Plugin/QueueWorker/queue_worker.php.twig
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 %}

0 comments on commit 5e668f6

Please sign in to comment.