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']);
182 protected function interact(InputInterface $input, OutputInterface $output)
184 $io = new DrupalStyle($input, $output);
186 $module = $input->getOption('module');
188 // @see Drupal\Console\Command\ModuleTrait::moduleQuestion
189 $module = $this->moduleQuestion($io);
190 $input->setOption('module', $module);
193 $pluginId = $input->getOption('plugin-id');
195 $plugins = $this->getPlugins();
196 $pluginId = $io->choiceNoList(
197 $this->trans('commands.generate.plugin.skeleton.questions.plugin'),
200 $input->setOption('plugin-id', $pluginId);
203 if (array_key_exists($pluginId, $this->pluginGeneratorsImplemented)) {
206 $this->trans('commands.generate.plugin.skeleton.messages.plugin-dont-exist'),
208 $this->pluginGeneratorsImplemented[$pluginId]
214 $class = $input->getOption('class');
217 $this->trans('commands.generate.plugin.skeleton.options.class'),
218 sprintf('%s%s', 'Default', ucfirst($this->stringConverter->underscoreToCamelCase($pluginId))),
220 return $this->validator->validateClassName($class);
223 $input->setOption('class', $class);
227 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
228 $services = $input->getOption('services');
230 $services = $this->servicesQuestion($io);
231 $input->setOption('services', $services);
235 protected function getPluginMetadata($pluginId)
238 'serviceId' => 'plugin.manager.' . $pluginId,
241 // Load service and create reflection
242 $service = \Drupal::service($pluginMetaData['serviceId']);
244 $reflectionClass = new \ReflectionClass($service);
246 // Get list of properties with $reflectionClass->getProperties();
247 $pluginManagerProperties = [
248 'subdir' => 'subdir',
249 'pluginInterface' => 'pluginInterface',
250 'pluginDefinitionAnnotationName' => 'pluginAnnotation',
253 foreach ($pluginManagerProperties as $propertyName => $key) {
254 if (!$reflectionClass->hasProperty($propertyName)) {
255 $pluginMetaData[$key] = '';
259 $property = $reflectionClass->getProperty($propertyName);
260 $property->setAccessible(true);
261 $pluginMetaData[$key] = $property->getValue($service);
264 if (empty($pluginMetaData['pluginInterface'])) {
265 $pluginMetaData['pluginInterfaceMethods'] = [];
267 $pluginMetaData['pluginInterfaceMethods'] = $this->getClassMethods($pluginMetaData['pluginInterface']);
270 if (isset($pluginMetaData['pluginAnnotation']) && class_exists($pluginMetaData['pluginAnnotation'])) {
271 $pluginMetaData['pluginAnnotationProperties'] = $this->getPluginAnnotationProperties($pluginMetaData['pluginAnnotation']);
273 $pluginMetaData['pluginAnnotationProperties'] = [];
276 return $pluginMetaData;
280 * Get data for the methods of a class.
283 * The fully-qualified name of class.
286 * An array keyed by method name, where each value is an array containing:
287 * - 'name: The name of the method.
288 * - 'declaration': The function declaration line.
289 * - 'description': The description from the method's docblock first line.
291 protected function getClassMethods($class)
293 // Get a reflection class.
294 $classReflection = new \ReflectionClass($class);
295 $methods = $classReflection->getMethods();
300 foreach ($methods as $method) {
301 $methodData['name'] = $method->getName();
303 $filename = $method->getFileName();
304 $source = file($filename);
305 $startLine = $method->getStartLine();
307 $methodData['declaration'] = substr(trim($source[$startLine - 1]), 0, -1);
309 $methodDocComment = explode("\n", $method->getDocComment());
310 foreach ($methodDocComment as $line) {
311 if (substr($line, 0, 5) == ' * ') {
312 $methodData['description'] = substr($line, 5);
317 $metaData[$method->getName()] = $methodData;
324 * Get the list of properties from an annotation class.
326 * @param $pluginAnnotationClass
327 * The fully-qualified name of the plugin annotation class.
330 * An array keyed by property name, where each value is an array containing:
331 * - 'name: The name of the property.
332 * - 'description': The description from the property's docblock first line.
334 protected function getPluginAnnotationProperties($pluginAnnotationClass)
336 // Get a reflection class for the annotation class.
337 // Each property of the annotation class describes a property for the
338 // plugin annotation.
339 $annotationReflection = new \ReflectionClass($pluginAnnotationClass);
340 $propertiesReflection = $annotationReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
342 $pluginProperties = [];
343 $annotationPropertyMetadata = [];
345 foreach ($propertiesReflection as $propertyReflection) {
346 $annotationPropertyMetadata['name'] = $propertyReflection->name;
348 $propertyDocblock = $propertyReflection->getDocComment();
349 $propertyDocblockLines = explode("\n", $propertyDocblock);
350 foreach ($propertyDocblockLines as $line) {
351 if (substr($line, 0, 3) == '/**') {
355 // Take the first actual docblock line to be the description.
356 if (!isset($annotationPropertyMetadata['description']) && substr($line, 0, 5) == ' * ') {
357 $annotationPropertyMetadata['description'] = substr($line, 5);
360 // Look for a @var token, to tell us the type of the property.
361 if (substr($line, 0, 10) == ' * @var ') {
362 $annotationPropertyMetadata['type'] = substr($line, 10);
366 $pluginProperties[$propertyReflection->name] = $annotationPropertyMetadata;
369 return $pluginProperties;
372 protected function getPlugins()
376 foreach ($this->container->getServiceIds() as $serviceId) {
377 if (strpos($serviceId, 'plugin.manager.') === 0) {
378 $plugins[] = substr($serviceId, 15);