5462596de3bba9c153c49c70290613cd373b8912
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginTypeAnnotationCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginTypeAnnotationCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Generator\PluginTypeAnnotationGenerator;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ServicesTrait;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\FormTrait;
17 use Drupal\Console\Command\Shared\ConfirmationTrait;
18 use Symfony\Component\Console\Command\Command;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Core\Command\Shared\CommandTrait;
21 use Drupal\Console\Extension\Manager;
22 use Drupal\Console\Core\Utils\StringConverter;
23
24 /**
25  * Class PluginTypeAnnotationCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class PluginTypeAnnotationCommand extends Command
30 {
31     use ServicesTrait;
32     use ModuleTrait;
33     use FormTrait;
34     use ConfirmationTrait;
35     use CommandTrait;
36
37     /**
38  * @var Manager
39 */
40     protected $extensionManager;
41
42     /**
43  * @var PluginTypeAnnotationGenerator
44 */
45     protected $generator;
46
47     /**
48      * @var StringConverter
49      */
50     protected $stringConverter;
51
52     /**
53      * PluginTypeAnnotationCommand constructor.
54      *
55      * @param Manager                       $extensionManager
56      * @param PluginTypeAnnotationGenerator $generator
57      * @param StringConverter               $stringConverter
58      */
59     public function __construct(
60         Manager $extensionManager,
61         PluginTypeAnnotationGenerator $generator,
62         StringConverter $stringConverter
63     ) {
64         $this->extensionManager = $extensionManager;
65         $this->generator = $generator;
66         $this->stringConverter = $stringConverter;
67         parent::__construct();
68     }
69
70     protected function configure()
71     {
72         $this
73             ->setName('generate:plugin:type:annotation')
74             ->setDescription($this->trans('commands.generate.plugin.type.annotation.description'))
75             ->setHelp($this->trans('commands.generate.plugin.type.annotation.help'))
76             ->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
77             ->addOption(
78                 'class',
79                 '',
80                 InputOption::VALUE_OPTIONAL,
81                 $this->trans('commands.generate.plugin.type.annotation.options.class')
82             )
83             ->addOption(
84                 'machine-name',
85                 '',
86                 InputOption::VALUE_OPTIONAL,
87                 $this->trans('commands.generate.plugin.type.annotation.options.plugin-id')
88             )
89             ->addOption(
90                 'label',
91                 '',
92                 InputOption::VALUE_OPTIONAL,
93                 $this->trans('commands.generate.plugin.type.annotation.options.label')
94             );
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     protected function execute(InputInterface $input, OutputInterface $output)
101     {
102         $module = $input->getOption('module');
103         $class_name = $input->getOption('class');
104         $machine_name = $input->getOption('machine-name');
105         $label = $input->getOption('label');
106
107         $this->generator->generate($module, $class_name, $machine_name, $label);
108     }
109
110     protected function interact(InputInterface $input, OutputInterface $output)
111     {
112         $io = new DrupalStyle($input, $output);
113
114         // --module option
115         $module = $input->getOption('module');
116         if (!$module) {
117             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
118             $module = $this->moduleQuestion($io);
119             $input->setOption('module', $module);
120         }
121
122         // --class option
123         $class_name = $input->getOption('class');
124         if (!$class_name) {
125             $class_name = $io->ask(
126                 $this->trans('commands.generate.plugin.type.annotation.options.class'),
127                 'ExamplePlugin'
128             );
129             $input->setOption('class', $class_name);
130         }
131
132         // --machine-name option
133         $machine_name = $input->getOption('machine-name');
134         if (!$machine_name) {
135             $machine_name = $io->ask(
136                 $this->trans('commands.generate.plugin.type.annotation.options.machine-name'),
137                 $this->stringConverter->camelCaseToUnderscore($class_name)
138             );
139             $input->setOption('machine-name', $machine_name);
140         }
141
142         // --label option
143         $label = $input->getOption('label');
144         if (!$label) {
145             $label = $io->ask(
146                 $this->trans('commands.generate.plugin.type.annotation.options.label'),
147                 $this->stringConverter->camelCaseToHuman($class_name)
148             );
149             $input->setOption('label', $label);
150         }
151     }
152 }