-
-
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.
Add generate:plugin:rules:dataprocessor command (#3828)
- Loading branch information
Showing
5 changed files
with
273 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
184 changes: 184 additions & 0 deletions
184
src/Command/Generate/PluginRulesDataprocessorCommand.php
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,184 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Command\Generate\PluginRulesActionCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
use Drupal\Console\Command\Shared\ConfirmationTrait; | ||
use Drupal\Console\Command\Shared\ModuleTrait; | ||
use Drupal\Console\Command\Shared\ServicesTrait; | ||
use Drupal\Console\Core\Command\Command; | ||
use Drupal\Console\Core\Utils\StringConverter; | ||
use Drupal\Console\Core\Utils\ChainQueue; | ||
use Drupal\Console\Extension\Manager; | ||
use Drupal\Console\Generator\PluginRulesDataprocessorGenerator; | ||
use Drupal\Console\Utils\Validator; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Class PluginRulesDataprocessorCommand | ||
* | ||
* @package Drupal\Console\Command\Generate | ||
*/ | ||
class PluginRulesDataprocessorCommand extends Command | ||
{ | ||
|
||
use ConfirmationTrait; | ||
use ModuleTrait; | ||
use ServicesTrait; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* @var PluginRulesDataprocessorGenerator | ||
*/ | ||
protected $generator; | ||
|
||
/** | ||
* @var StringConverter | ||
*/ | ||
protected $stringConverter; | ||
|
||
/** | ||
* @var Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* @var ChainQueue | ||
*/ | ||
protected $chainQueue; | ||
|
||
|
||
/** | ||
* PluginRulesDataprocessorCommand constructor. | ||
* | ||
* @param Manager $extensionManager | ||
* @param PluginRulesDataprocessorGenerator $generator | ||
* @param StringConverter $stringConverter | ||
* @param Validator $validator | ||
* @param ChainQueue $chainQueue | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager, | ||
PluginRulesDataprocessorGenerator $generator, | ||
StringConverter $stringConverter, | ||
Validator $validator, | ||
ChainQueue $chainQueue | ||
) { | ||
$this->extensionManager = $extensionManager; | ||
$this->generator = $generator; | ||
$this->stringConverter = $stringConverter; | ||
$this->validator = $validator; | ||
$this->chainQueue = $chainQueue; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('generate:plugin:rules:dataprocessor') | ||
->setDescription($this->trans('commands.generate.plugin.rules.dataprocessor.description')) | ||
->setHelp($this->trans('commands.generate.plugin.rules.dataprocessor.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.dataprocessor.options.class') | ||
) | ||
->addOption( | ||
'label', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.rules.dataprocessor.options.label') | ||
) | ||
->addOption( | ||
'plugin-id', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.rules.dataprocessor.options.plugin-id') | ||
) | ||
->setAliases(['gprd']); | ||
} | ||
|
||
/** | ||
* {@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'); | ||
|
||
$this->generator->generate([ | ||
'module' => $module, | ||
'class_name' => $class_name, | ||
'label' => $label, | ||
'plugin_id' => $plugin_id, | ||
]); | ||
|
||
$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.dataprocessor.options.class'), | ||
'DefaultDataprocessor', | ||
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.dataprocessor.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.dataprocessor.options.plugin-id'), | ||
$this->stringConverter->camelCaseToUnderscore($class_name) | ||
); | ||
$input->setOption('plugin-id', $plugin_id); | ||
} | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Generator\PluginBlockGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
class PluginRulesDataprocessorGenerator extends Generator | ||
{ | ||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* PluginRulesDataprocessorGenerator constructor. | ||
* | ||
* @param Manager $extensionManager | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager | ||
) { | ||
$this->extensionManager = $extensionManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate(array $parameters) | ||
{ | ||
$module = $parameters['module']; | ||
$class_name = $parameters['class_name']; | ||
|
||
$this->renderFile( | ||
'module/src/Plugin/RulesDataProcessor/rulesdataprocessor.php.twig', | ||
$this->extensionManager->getPluginPath($module, 'RulesDataProcessor') . '/' . $class_name . '.php', | ||
$parameters | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
templates/module/src/Plugin/RulesDataProcessor/rulesdataprocessor.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,34 @@ | ||
{% extends "base/class.php.twig" %} | ||
|
||
{% block file_path %} | ||
Drupal\{{module}}\Plugin\RulesDataProcessor\{{class_name}}. | ||
{% endblock %} | ||
|
||
{% block namespace_class %} | ||
namespace Drupal\{{module}}\Plugin\RulesDataProcessor; | ||
{% endblock %} | ||
|
||
{% block use_class %} | ||
use Drupal\Core\Plugin\PluginBase; | ||
use Drupal\rules\Context\DataProcessorInterface; | ||
use Drupal\rules\Engine\ExecutionStateInterface; | ||
{% endblock %} | ||
|
||
{% block class_declaration %} | ||
/** | ||
* Provides a '{{class_name}}' action. | ||
* | ||
* @RulesDataProcessor( | ||
* id = "{{plugin_id}}", | ||
* label = @Translation("{{label}}") | ||
* ) | ||
*/ | ||
class {{class_name}} extends PluginBase implements DataProcessorInterface {% endblock %} | ||
{% block class_methods %} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process($value, ExecutionStateInterface $rules_state) { | ||
// Insert code here. | ||
} | ||
{% endblock %} |