cca91d85974f825dd782b5ba1c6aac643a990987
[yaffs-website] / vendor / drupal / console / src / Command / Generate / CommandCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\CommandCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Command\Shared\ExtensionTrait;
11 use Drupal\Console\Command\Shared\ServicesTrait;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Drupal\Console\Core\Command\ContainerAwareCommand;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Command\Shared\ModuleTrait;
18 use Drupal\Console\Generator\CommandGenerator;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22 use Drupal\Console\Utils\Site;
23
24 class CommandCommand extends ContainerAwareCommand
25 {
26     use ConfirmationTrait;
27     use ServicesTrait;
28     use ModuleTrait;
29     use ExtensionTrait;
30
31     /**
32      * @var CommandGenerator
33      */
34     protected $generator;
35
36     /**
37      * @var Manager
38      */
39     protected $extensionManager;
40
41     /**
42      * @var Validator
43      */
44     protected $validator;
45
46     /**
47      * @var StringConverter
48      */
49     protected $stringConverter;
50
51     /**
52      * @var Site
53      */
54     protected $site;
55
56     /**
57      * CommandCommand constructor.
58      *
59      * @param CommandGenerator $generator
60      * @param Manager          $extensionManager
61      * @param Validator        $validator
62      * @param StringConverter  $stringConverter
63      * @param Site             $site
64      */
65     public function __construct(
66         CommandGenerator $generator,
67         Manager $extensionManager,
68         Validator $validator,
69         StringConverter $stringConverter,
70         Site  $site
71     ) {
72         $this->generator = $generator;
73         $this->extensionManager = $extensionManager;
74         $this->validator = $validator;
75         $this->stringConverter = $stringConverter;
76         $this->site = $site;
77         parent::__construct();
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     protected function configure()
84     {
85         $this
86             ->setName('generate:command')
87             ->setDescription($this->trans('commands.generate.command.description'))
88             ->setHelp($this->trans('commands.generate.command.help'))
89             ->addOption(
90                 'extension',
91                 null,
92                 InputOption::VALUE_REQUIRED,
93                 $this->trans('commands.common.options.extension')
94             )
95             ->addOption(
96                 'extension-type',
97                 null,
98                 InputOption::VALUE_REQUIRED,
99                 $this->trans('commands.common.options.extension-type')
100             )
101             ->addOption(
102                 'class',
103                 null,
104                 InputOption::VALUE_REQUIRED,
105                 $this->trans('commands.generate.command.options.class')
106             )
107             ->addOption(
108                 'name',
109                 null,
110                 InputOption::VALUE_REQUIRED,
111                 $this->trans('commands.generate.command.options.name')
112             )
113             ->addOption(
114                 'initialize',
115                 null,
116                 InputOption::VALUE_NONE,
117                 $this->trans('commands.generate.command.options.initialize')
118             )
119             ->addOption(
120                 'interact',
121                 null,
122                 InputOption::VALUE_NONE,
123                 $this->trans('commands.generate.command.options.interact')
124             )
125             ->addOption(
126                 'container-aware',
127                 null,
128                 InputOption::VALUE_NONE,
129                 $this->trans('commands.generate.command.options.container-aware')
130             )
131             ->addOption(
132                 'services',
133                 null,
134                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
135                 $this->trans('commands.common.options.services')
136             )
137             ->addOption(
138                 'generator',
139                 null,
140                 InputOption::VALUE_NONE,
141                 $this->trans('commands.generate.command.options.generator')
142             )
143             ->setAliases(['gco']);
144     }
145
146     /**
147      * {@inheritdoc}
148      */
149     protected function execute(InputInterface $input, OutputInterface $output)
150     {
151         $extension = $input->getOption('extension');
152         $extensionType = $input->getOption('extension-type');
153         $class = $this->validator->validateCommandName($input->getOption('class'));
154         $name = $input->getOption('name');
155         $initialize = $input->getOption('initialize');
156         $interact = $input->getOption('interact');
157         $containerAware = $input->getOption('container-aware');
158         $services = $input->getOption('services');
159         $generator = $input->getOption('generator');
160
161         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
162         if (!$this->confirmOperation()) {
163             return 1;
164         }
165
166         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
167         $build_services = $this->buildServices($services);
168
169         $class_generator = null;
170         if ($generator) {
171             $class_generator = str_replace('Command', 'Generator', $class);
172         }
173
174         $this->generator->generate([
175             'extension' => $extension,
176             'extension_type' => $extensionType,
177             'name' => $name,
178             'initialize' => $initialize,
179             'interact' => $interact,
180             'class_name' => $class,
181             'container_aware' => $containerAware,
182             'services' => $build_services,
183             'class_generator' => $class_generator,
184             'generator' => $generator,
185         ]);
186
187         $this->site->removeCachedServicesFile();
188
189         return 0;
190     }
191
192     /**
193      * {@inheritdoc}
194      */
195     protected function interact(InputInterface $input, OutputInterface $output)
196     {
197         $extension = $input->getOption('extension');
198         if (!$extension) {
199             $extension = $this->extensionQuestion(true, true);
200             $input->setOption('extension', $extension->getName());
201             $input->setOption('extension-type', $extension->getType());
202         }
203
204         $extensionType = $input->getOption('extension-type');
205         if (!$extensionType) {
206             $extensionType = $this->extensionTypeQuestion();
207             $input->setOption('extension-type', $extensionType);
208         }
209
210         $name = $input->getOption('name');
211         if (!$name) {
212             $name = $this->getIo()->ask(
213                 $this->trans('commands.generate.command.questions.name'),
214                 sprintf('%s:default', $extension->getName())
215             );
216             $input->setOption('name', $name);
217         }
218
219         $initialize = $input->getOption('initialize');
220         if (!$initialize) {
221             $initialize = $this->getIo()->confirm(
222                 $this->trans('commands.generate.command.questions.initialize'),
223                 false
224             );
225             $input->setOption('initialize', $initialize);
226         }
227
228         $interact = $input->getOption('interact');
229         if (!$interact) {
230             $interact = $this->getIo()->confirm(
231                 $this->trans('commands.generate.command.questions.interact'),
232                 false
233             );
234             $input->setOption('interact', $interact);
235         }
236
237         $class = $input->getOption('class');
238         if (!$class) {
239             $class = $this->getIo()->ask(
240                 $this->trans('commands.generate.command.questions.class'),
241                 'DefaultCommand',
242                 function ($class) {
243                     return $this->validator->validateCommandName($class);
244                 }
245             );
246             $input->setOption('class', $class);
247         }
248
249         $containerAware = $input->getOption('container-aware');
250         if (!$containerAware) {
251             $containerAware = $this->getIo()->confirm(
252                 $this->trans('commands.generate.command.questions.container-aware'),
253                 false
254             );
255             $input->setOption('container-aware', $containerAware);
256         }
257
258         if (!$containerAware) {
259             // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
260             $services = $this->servicesQuestion();
261             $input->setOption('services', $services);
262         }
263
264         $generator = $input->getOption('generator');
265         if (!$generator) {
266             $generator = $this->getIo()->confirm(
267                 $this->trans('commands.generate.command.questions.generator'),
268                 false
269             );
270             $input->setOption('generator', $generator);
271         }
272     }
273 }