Skip to content
Permalink
Browse files

A new command : Generates Validation constraint plugin (#3768)

* A new command : Generates Validation constraint plugin

* Add a line in the end of the file
  • Loading branch information...
LOBsTerr authored and jmolivas committed Apr 21, 2018
1 parent 368bbfa commit c5e5d07191da41f617dcd5284af596ec2c014027
@@ -214,3 +214,8 @@ services:
arguments: ['@console.extension_manager', '@console.js_test_generator', '@console.validator']
tags:
- { name: drupal.command }
console.generate_plugin_validation_constraint:
class: Drupal\Console\Command\Generate\PluginValidationConstraintCommand
arguments: ['@console.extension_manager', '@console.validation_constraint_generator','@console.string_converter', '@console.validator', '@console.chain_queue']
tags:
- { name: drupal.command }
@@ -211,3 +211,8 @@ services:
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.validation_constraint_generator:
class: Drupal\Console\Generator\PluginValidationConstraintGenerator
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
@@ -0,0 +1,221 @@
<?php
/**
* @file
* Contains \Drupal\Console\Command\Generate\PluginValidationConstraintCommand.
*/
namespace Drupal\Console\Command\Generate;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Generator\PluginValidationConstraintGenerator;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
/**
* Class PluginValidationConstraintCommand
*
* @package Drupal\Console\Command\Generate
*/
class PluginValidationConstraintCommand extends Command
{
use ModuleTrait;
use ConfirmationTrait;
/**
* @var Manager
*/
protected $extensionManager;
/**
* @var PluginValidationConstraintGenerator
*/
protected $generator;
/**
* @var StringConverter
*/
protected $stringConverter;
/**
* @var Validator
*/
protected $validator;
/**
* @var ChainQueue
*/
protected $chainQueue;
/**
* PluginValidationConstraintCommand constructor.
*
* @param Manager $extensionManager
* @param PluginValidationConstraintGenerator $generator
* @param StringConverter $stringConverter
* @param Validator $validator
* @param ChainQueue $chainQueue
*/
public function __construct(
Manager $extensionManager,
PluginValidationConstraintGenerator $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:validationconstraint')
->setDescription($this->trans('commands.generate.plugin.validationconstraint.description'))
->setHelp($this->trans('commands.generate.plugin.validationconstraint.help'))
->addOption(
'module',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.plugin.validationconstraint.options.class')
)
->addOption(
'label',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.validationconstraint.options.label')
)
->addOption(
'plugin-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.validationconstraint.options.plugin-id')
)
->addOption(
'hook',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.validationconstraint.options.hook')
)
->addOption(
'field-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.validationconstraint.options.field-id')
)
->addOption(
'bundle',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.validationconstraint.options.bundle')
)
->setAliases(['gpvc']);
}
/**
* {@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');
$className = $this->validator->validateClassName($input->getOption('class'));
$label = $input->getOption('label');
$pluginId = $input->getOption('plugin-id');
$hook = $input->getOption('hook');
$fieldId = $input->getOption('field-id');
$bundle = $input->getOption('bundle');
$this->generator->generate([
'module' => $module,
'class_name' => $className,
'label' => $label,
'plugin_id' => $pluginId,
'field_id' => $fieldId,
'hook' => $hook,
'bundle' => $bundle,
]);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
return 0;
}
protected function interact(InputInterface $input, OutputInterface $output)
{
// --module option
$this->getModuleOption();
// --class option
$className = $input->getOption('class');
if (!$className) {
$className = $this->getIo()->ask(
$this->trans('commands.generate.plugin.validationconstraint.questions.class'),
'ExampleConstraint',
function ($className) {
return $this->validator->validateClassName($className);
}
);
$input->setOption('class', $className);
}
// --plugin label option
$label = $input->getOption('label');
if (!$label) {
$label = $this->getIo()->ask(
$this->trans('commands.generate.plugin.validationconstraint.questions.label'),
$this->stringConverter->camelCaseToHuman($className)
);
$input->setOption('label', $label);
}
// --plugin-id option
$pluginId = $input->getOption('plugin-id');
if (!$pluginId) {
$pluginId = $this->getIo()->ask(
$this->trans('commands.generate.plugin.validationconstraint.questions.plugin-id'),
$this->stringConverter->camelCaseToUnderscore($className)
);
$input->setOption('plugin-id', $pluginId);
}
$hook = $this->getIo()->confirm(
$this->trans('commands.generate.plugin.validationconstraint.questions.hook'),
false
);
if (!empty($hook)) {
$fieldId = $this->getIo()->ask(
$this->trans('commands.generate.plugin.validationconstraint.questions.field-id')
);
$input->setOption('field-id', $fieldId);
$bundle = $this->getIo()->ask(
$this->trans('commands.generate.plugin.validationconstraint.questions.bundle')
);
$input->setOption('bundle', $bundle);
}
$input->setOption('hook', $hook);
}
}
@@ -0,0 +1,63 @@
<?php
/**
* @file
* Contains \Drupal\Console\Generator\PluginValidationConstraintGenerator.
*/
namespace Drupal\Console\Generator;
use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;
class PluginValidationConstraintGenerator extends Generator
{
/**
* @var Manager
*/
protected $extensionManager;
/**
* PluginValidationConstraintGenerator constructor.
*
* @param Manager $extensionManager
*/
public function __construct(Manager $extensionManager) {
$this->extensionManager = $extensionManager;
}
/**
* {@inheritdoc}
*/
public function generate(array $parameters)
{
$module = $parameters['module'];
$className = $parameters['class_name'];
$hook = $parameters['hook'];
$pluginPath = $this->extensionManager->getPluginPath($module, 'Validation/Constraint') . '/' . $className;
// Generates Contraint class.
$this->renderFile(
'module/src/Plugin/Validation/Constraint/constraint.php.twig',
$pluginPath . '.php',
$parameters
);
// Generates Validator class.
$this->renderFile(
'module/src/Plugin/Validation/Constraint/validator.php.twig',
$pluginPath . 'Validator.php',
$parameters
);
if (!empty($hook)) {
$this->renderFile(
'module/src/Entity/Bundle/entity-bundle-field-info-alter.php.twig',
$this->extensionManager->getModule($module)->getPath() . '/' . $module . '.module',
$parameters,
FILE_APPEND
);
}
}
}
@@ -0,0 +1,20 @@

{% block use_class %}
use Drupal\Core\Entity\EntityTypeInterface;
{% endblock %}

/**
* Implements hook_entity_bundle_field_info_alter().
*/
function {{module}}_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
{% if bundle %}
if ($bundle === '{{ bundle }}') {
{% endif %}
if (isset($fields['{{ field_id }}'])) {
// Use the ID as defined in the annotation of the constraint definition.
$fields['{{ field_id }}']->addConstraint('{{ plugin_id }}', []);
}
{% if bundle %}
}
{% endif %}
}
@@ -0,0 +1,34 @@
{% extends "base/class.php.twig" %}

{% block file_path %}
\Drupal\{{ module }}\Plugin\Validation\Constraint\{{ class_name }}.
{% endblock %}

{% block namespace_class %}
namespace Drupal\{{ module }}\Plugin\Validation\Constraint;
{% endblock %}

{% block use_class %}
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
{% endblock %}

{% block class_declaration %}
/**
* Plugin implementation of the '{{ plugin_id }}'.
*
* @Constraint(
* id = "{{ plugin_id }}",
* label = @Translation("{{ label }}", context = "Validation"),
* )
*/
class {{ class_name }} extends Constraint
{% endblock %}

{% block class_properties %}
// The message that will be shown if the value is empty.
public $isEmpty = '%value is empty';

// The message that will be shown if the value is not unique.
public $notUnique = '%value is not unique';
{% endblock %}

0 comments on commit c5e5d07

Please sign in to comment.
You can’t perform that action at this time.