5 * Contains \Drupal\Console\Command\Generate\PluginSkeletonCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Generator\PluginSkeletonGenerator;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Command\Shared\ServicesTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Core\Utils\ChainQueue;
23 use Drupal\Console\Utils\Validator;
26 * Class PluginSkeletonCommand
28 * @package Drupal\Console\Command\Generate
30 class PluginSkeletonCommand extends Command
33 use ConfirmationTrait;
35 use ContainerAwareCommandTrait;
40 protected $extensionManager;
43 * @var PluginSkeletonGenerator
48 * @var StringConverter
50 protected $stringConverter;
60 protected $chainQueue;
64 * PluginSkeletonCommand constructor.
66 * @param Manager $extensionManager
67 * @param PluginSkeletonGenerator $generator
68 * @param StringConverter $stringConverter
69 * @param Validator $validator
70 * @param ChainQueue $chainQueue
72 public function __construct(
73 Manager $extensionManager,
74 PluginSkeletonGenerator $generator,
75 StringConverter $stringConverter,
77 ChainQueue $chainQueue
79 $this->extensionManager = $extensionManager;
80 $this->generator = $generator;
81 $this->stringConverter = $stringConverter;
82 $this->validator = $validator;
83 $this->chainQueue = $chainQueue;
84 parent::__construct();
87 protected $pluginGeneratorsImplemented = [
88 'block' => 'generate:plugin:block',
89 'ckeditor.plugin' => 'generate:plugin:ckeditorbutton',
90 'condition' => 'generate:plugin:condition',
91 'field.formatter' => 'generate:plugin:fieldformatter',
92 'field.field_type' => 'generate:plugin:fieldtype',
93 'field.widget' =>'generate:plugin:fieldwidget',
94 'image.effect' => 'generate:plugin:imageeffect',
95 'mail' => 'generate:plugin:mail'
98 protected function configure()
101 ->setName('generate:plugin:skeleton')
102 ->setDescription($this->trans('commands.generate.plugin.skeleton.description'))
103 ->setHelp($this->trans('commands.generate.plugin.skeleton.help'))
107 InputOption::VALUE_REQUIRED,
108 $this->trans('commands.common.options.module')
113 InputOption::VALUE_REQUIRED,
114 $this->trans('commands.generate.plugin.options.plugin-id')
119 InputOption::VALUE_OPTIONAL,
120 $this->trans('commands.generate.plugin.block.options.class')
125 InputOption::VALUE_OPTIONAL| InputOption::VALUE_IS_ARRAY,
126 $this->trans('commands.common.options.services')
133 protected function execute(InputInterface $input, OutputInterface $output)
135 $io = new DrupalStyle($input, $output);
136 $plugins = $this->getPlugins();
138 // @see use Drupal\Console\Command\ConfirmationTrait::confirmGeneration
139 if (!$this->confirmGeneration($io)) {
143 $module = $input->getOption('module');
145 $pluginId = $input->getOption('plugin-id');
146 $plugin = ucfirst($this->stringConverter->underscoreToCamelCase($pluginId));
148 // Confirm that plugin.manager is available
149 if (!$this->validator->validatePluginManagerServiceExist($pluginId, $plugins)) {
150 throw new \Exception(
152 $this->trans('commands.generate.plugin.skeleton.messages.plugin-dont-exist'),
158 if (array_key_exists($pluginId, $this->pluginGeneratorsImplemented)) {
161 $this->trans('commands.generate.plugin.skeleton.messages.plugin-generator-implemented'),
163 $this->pluginGeneratorsImplemented[$pluginId]
168 $className = $input->getOption('class');
169 $services = $input->getOption('services');
171 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
172 $buildServices = $this->buildServices($services);
173 $pluginMetaData = $this->getPluginMetadata($pluginId);
175 $this->generator->generate($module, $pluginId, $plugin, $className, $pluginMetaData, $buildServices);
177 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
180 protected function interact(InputInterface $input, OutputInterface $output)
182 $io = new DrupalStyle($input, $output);
184 $module = $input->getOption('module');
186 // @see Drupal\Console\Command\ModuleTrait::moduleQuestion
187 $module = $this->moduleQuestion($io);
188 $input->setOption('module', $module);
191 $pluginId = $input->getOption('plugin-id');
193 $plugins = $this->getPlugins();
194 $pluginId = $io->choiceNoList(
195 $this->trans('commands.generate.plugin.skeleton.questions.plugin'),
198 $input->setOption('plugin-id', $pluginId);
201 if (array_key_exists($pluginId, $this->pluginGeneratorsImplemented)) {
204 $this->trans('commands.generate.plugin.skeleton.messages.plugin-dont-exist'),
206 $this->pluginGeneratorsImplemented[$pluginId]
212 $class = $input->getOption('class');
215 $this->trans('commands.generate.plugin.skeleton.options.class'),
216 sprintf('%s%s', 'Default', ucfirst($this->stringConverter->underscoreToCamelCase($pluginId))),
218 return $this->validator->validateClassName($class);
221 $input->setOption('class', $class);
225 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
226 $services = $input->getOption('services');
228 $services = $this->servicesQuestion($io);
229 $input->setOption('services', $services);
233 protected function getPluginMetadata($pluginId)
236 'serviceId' => 'plugin.manager.' . $pluginId,
239 // Load service and create reflection
240 $service = \Drupal::service($pluginMetaData['serviceId']);
242 $reflectionClass = new \ReflectionClass($service);
244 // Get list of properties with $reflectionClass->getProperties();
245 $pluginManagerProperties = [
246 'subdir' => 'subdir',
247 'pluginInterface' => 'pluginInterface',
248 'pluginDefinitionAnnotationName' => 'pluginAnnotation',
251 foreach ($pluginManagerProperties as $propertyName => $key) {
252 if (!$reflectionClass->hasProperty($propertyName)) {
253 $pluginMetaData[$key] = '';
257 $property = $reflectionClass->getProperty($propertyName);
258 $property->setAccessible(true);
259 $pluginMetaData[$key] = $property->getValue($service);
262 if (empty($pluginMetaData['pluginInterface'])) {
263 $pluginMetaData['pluginInterfaceMethods'] = [];
265 $pluginMetaData['pluginInterfaceMethods'] = $this->getClassMethods($pluginMetaData['pluginInterface']);
268 if (isset($pluginMetaData['pluginAnnotation']) && class_exists($pluginMetaData['pluginAnnotation'])) {
269 $pluginMetaData['pluginAnnotationProperties'] = $this->getPluginAnnotationProperties($pluginMetaData['pluginAnnotation']);
271 $pluginMetaData['pluginAnnotationProperties'] = [];
274 return $pluginMetaData;
278 * Get data for the methods of a class.
281 * The fully-qualified name of class.
284 * An array keyed by method name, where each value is an array containing:
285 * - 'name: The name of the method.
286 * - 'declaration': The function declaration line.
287 * - 'description': The description from the method's docblock first line.
289 protected function getClassMethods($class)
291 // Get a reflection class.
292 $classReflection = new \ReflectionClass($class);
293 $methods = $classReflection->getMethods();
298 foreach ($methods as $method) {
299 $methodData['name'] = $method->getName();
301 $filename = $method->getFileName();
302 $source = file($filename);
303 $startLine = $method->getStartLine();
305 $methodData['declaration'] = substr(trim($source[$startLine - 1]), 0, -1);
307 $methodDocComment = explode("\n", $method->getDocComment());
308 foreach ($methodDocComment as $line) {
309 if (substr($line, 0, 5) == ' * ') {
310 $methodData['description'] = substr($line, 5);
315 $metaData[$method->getName()] = $methodData;
322 * Get the list of properties from an annotation class.
324 * @param $pluginAnnotationClass
325 * The fully-qualified name of the plugin annotation class.
328 * An array keyed by property name, where each value is an array containing:
329 * - 'name: The name of the property.
330 * - 'description': The description from the property's docblock first line.
332 protected function getPluginAnnotationProperties($pluginAnnotationClass)
334 // Get a reflection class for the annotation class.
335 // Each property of the annotation class describes a property for the
336 // plugin annotation.
337 $annotationReflection = new \ReflectionClass($pluginAnnotationClass);
338 $propertiesReflection = $annotationReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
340 $pluginProperties = [];
341 $annotationPropertyMetadata = [];
343 foreach ($propertiesReflection as $propertyReflection) {
344 $annotationPropertyMetadata['name'] = $propertyReflection->name;
346 $propertyDocblock = $propertyReflection->getDocComment();
347 $propertyDocblockLines = explode("\n", $propertyDocblock);
348 foreach ($propertyDocblockLines as $line) {
349 if (substr($line, 0, 3) == '/**') {
353 // Take the first actual docblock line to be the description.
354 if (!isset($annotationPropertyMetadata['description']) && substr($line, 0, 5) == ' * ') {
355 $annotationPropertyMetadata['description'] = substr($line, 5);
358 // Look for a @var token, to tell us the type of the property.
359 if (substr($line, 0, 10) == ' * @var ') {
360 $annotationPropertyMetadata['type'] = substr($line, 10);
364 $pluginProperties[$propertyReflection->name] = $annotationPropertyMetadata;
367 return $pluginProperties;
370 protected function getPlugins()
374 foreach ($this->container->getServiceIds() as $serviceId) {
375 if (strpos($serviceId, 'plugin.manager.') === 0) {
376 $plugins[] = substr($serviceId, 15);