5085bb5d1bc00b860cb10626fcaf092e5ecb5be2
[yaffs-website] / vendor / drupal / console / src / Application.php
1 <?php
2
3 namespace Drupal\Console;
4
5 use Doctrine\Common\Annotations\AnnotationRegistry;
6 use Symfony\Component\Console\Input\InputInterface;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Console\Annotations\DrupalCommandAnnotationReader;
10 use Drupal\Console\Utils\AnnotationValidator;
11 use Drupal\Console\Core\Application as BaseApplication;
12
13 /**
14  * Class Application
15  *
16  * @package Drupal\Console
17  */
18 class Application extends BaseApplication
19 {
20     /**
21      * @var string
22      */
23     const NAME = 'Drupal Console';
24
25     /**
26      * @var string
27      */
28     const VERSION = '1.0.0-rc16';
29
30     public function __construct(ContainerInterface $container)
31     {
32         parent::__construct($container, $this::NAME, $this::VERSION);
33     }
34
35     /**
36      * {@inheritdoc}
37      */
38     public function doRun(InputInterface $input, OutputInterface $output)
39     {
40         $this->registerGenerators();
41         $this->registerCommands();
42         $clear = $this->container->get('console.configuration_manager')
43             ->getConfiguration()
44             ->get('application.clear')?:false;
45         if ($clear === true || $clear === 'true') {
46             $output->write(sprintf("\033\143"));
47         }
48
49         $exitCode = parent::doRun($input, $output);
50         return $exitCode;
51     }
52
53     private function registerGenerators()
54     {
55         if ($this->container->hasParameter('drupal.generators')) {
56             $consoleGenerators = $this->container->getParameter(
57                 'drupal.generators'
58             );
59         } else {
60             $consoleGenerators = array_keys(
61                 $this->container->findTaggedServiceIds('drupal.generator')
62             );
63         }
64
65         foreach ($consoleGenerators as $name) {
66             if (!$this->container->has($name)) {
67                 continue;
68             }
69
70             try {
71                 $generator = $this->container->get($name);
72             } catch (\Exception $e) {
73                 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
74
75                 continue;
76             }
77
78             if (!$generator) {
79                 continue;
80             }
81
82             if (method_exists($generator, 'setRenderer')) {
83                 $generator->setRenderer(
84                     $this->container->get('console.renderer')
85                 );
86             }
87
88             if (method_exists($generator, 'setFileQueue')) {
89                 $generator->setFileQueue(
90                     $this->container->get('console.file_queue')
91                 );
92             }
93         }
94     }
95
96     private function registerCommands()
97     {
98         if ($this->container->hasParameter('drupal.commands')) {
99             $consoleCommands = $this->container->getParameter(
100                 'drupal.commands'
101             );
102         } else {
103             $consoleCommands = array_keys(
104                 $this->container->findTaggedServiceIds('drupal.command')
105             );
106             $this->container->setParameter(
107                 'console.warning',
108                 'application.site.errors.settings'
109             );
110         }
111
112         $serviceDefinitions = [];
113         $annotationValidator = null;
114         $annotationCommandReader = null;
115         if ($this->container->hasParameter('console.service_definitions')) {
116             $serviceDefinitions = $this->container
117                 ->getParameter('console.service_definitions');
118
119             /**
120              * @var DrupalCommandAnnotationReader $annotationCommandReader
121              */
122             $annotationCommandReader = $this->container
123                 ->get('console.annotation_command_reader');
124
125             /**
126              * @var AnnotationValidator $annotationValidator
127              */
128             $annotationValidator = $this->container
129                 ->get('console.annotation_validator');
130         }
131
132         $aliases = $this->container->get('console.configuration_manager')
133             ->getConfiguration()
134             ->get('application.commands.aliases')?:[];
135
136         foreach ($consoleCommands as $name) {
137             AnnotationRegistry::reset();
138             AnnotationRegistry::registerLoader(
139                 [
140                     $this->container->get('class_loader'),
141                     "loadClass"
142                 ]
143             );
144
145             if (!$this->container->has($name)) {
146                 continue;
147             }
148
149             if ($annotationValidator && $annotationCommandReader) {
150                 if (!$serviceDefinition = $serviceDefinitions[$name]) {
151                     continue;
152                 }
153
154                 $annotation = $annotationCommandReader
155                     ->readAnnotation($serviceDefinition->getClass());
156                 if ($annotation) {
157                     $this->container->get('console.translator_manager')
158                         ->addResourceTranslationsByExtension(
159                             $annotation['extension'],
160                             $annotation['extensionType']
161                         );
162                 }
163
164                 if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
165                     continue;
166                 }
167             }
168
169             try {
170                 $command = $this->container->get($name);
171             } catch (\Exception $e) {
172                 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
173
174                 continue;
175             }
176
177             if (!$command) {
178                 continue;
179             }
180
181             if (method_exists($command, 'setTranslator')) {
182                 $command->setTranslator(
183                     $this->container->get('console.translator_manager')
184                 );
185             }
186
187             if (method_exists($command, 'setContainer')) {
188                 $command->setContainer(
189                     $this->container->get('service_container')
190                 );
191             }
192
193             if (array_key_exists($command->getName(), $aliases)) {
194                 $commandAliases = $aliases[$command->getName()];
195                 if (!is_array($commandAliases)) {
196                     $commandAliases = [$commandAliases];
197                 }
198                 $command->setAliases($commandAliases);
199             }
200
201             $this->add($command);
202         }
203     }
204
205     public function getData()
206     {
207         $singleCommands = [
208             'about',
209             'chain',
210             'check',
211             'exec',
212             'help',
213             'init',
214             'list',
215             'shell',
216             'server'
217         ];
218
219         $languages = $this->container->get('console.configuration_manager')
220             ->getConfiguration()
221             ->get('application.languages');
222
223         $data = [];
224         foreach ($singleCommands as $singleCommand) {
225             $data['commands']['misc'][] = $this->commandData($singleCommand);
226         }
227
228         $namespaces = array_filter(
229             $this->getNamespaces(), function ($item) {
230                 return (strpos($item, ':')<=0);
231             }
232         );
233         sort($namespaces);
234         array_unshift($namespaces, 'misc');
235
236         foreach ($namespaces as $namespace) {
237             $commands = $this->all($namespace);
238             usort(
239                 $commands, function ($cmd1, $cmd2) {
240                     return strcmp($cmd1->getName(), $cmd2->getName());
241                 }
242             );
243
244             foreach ($commands as $command) {
245                 if (method_exists($command, 'getModule')) {
246                     if ($command->getModule() == 'Console') {
247                         $data['commands'][$namespace][] = $this->commandData(
248                             $command->getName()
249                         );
250                     }
251                 } else {
252                     $data['commands'][$namespace][] = $this->commandData(
253                         $command->getName()
254                     );
255                 }
256             }
257         }
258
259         $input = $this->getDefinition();
260         $options = [];
261         foreach ($input->getOptions() as $option) {
262             $options[] = [
263                 'name' => $option->getName(),
264                 'description' => $this->trans('application.options.'.$option->getName())
265             ];
266         }
267         $arguments = [];
268         foreach ($input->getArguments() as $argument) {
269             $arguments[] = [
270                 'name' => $argument->getName(),
271                 'description' => $this->trans('application.arguments.'.$argument->getName())
272             ];
273         }
274
275         $data['application'] = [
276             'namespaces' => $namespaces,
277             'options' => $options,
278             'arguments' => $arguments,
279             'languages' => $languages,
280             'messages' => [
281                 'title' =>  $this->trans('commands.generate.doc.gitbook.messages.title'),
282                 'note' =>  $this->trans('commands.generate.doc.gitbook.messages.note'),
283                 'note_description' =>  $this->trans('commands.generate.doc.gitbook.messages.note-description'),
284                 'command' =>  $this->trans('commands.generate.doc.gitbook.messages.command'),
285                 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
286                 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
287                 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
288                 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
289                 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
290                 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
291             ],
292             'examples' => []
293         ];
294
295         return $data;
296     }
297
298     private function commandData($commandName)
299     {
300         if (!$this->has($commandName)) {
301             return [];
302         }
303
304         $command = $this->find($commandName);
305
306         $input = $command->getDefinition();
307         $options = [];
308         foreach ($input->getOptions() as $option) {
309             $options[$option->getName()] = [
310                 'name' => $option->getName(),
311                 'description' => $this->trans($option->getDescription()),
312             ];
313         }
314
315         $arguments = [];
316         foreach ($input->getArguments() as $argument) {
317             $arguments[$argument->getName()] = [
318                 'name' => $argument->getName(),
319                 'description' => $this->trans($argument->getDescription()),
320             ];
321         }
322
323         $commandKey = str_replace(':', '.', $command->getName());
324
325         $examples = [];
326         for ($i = 0; $i < 5; $i++) {
327             $description = sprintf(
328                 'commands.%s.examples.%s.description',
329                 $commandKey,
330                 $i
331             );
332             $execution = sprintf(
333                 'commands.%s.examples.%s.execution',
334                 $commandKey,
335                 $i
336             );
337
338             if ($description != $this->trans($description)) {
339                 $examples[] = [
340                     'description' => $this->trans($description),
341                     'execution' => $this->trans($execution)
342                 ];
343             } else {
344                 break;
345             }
346         }
347
348         $data = [
349             'name' => $command->getName(),
350             'description' => $command->getDescription(),
351             'options' => $options,
352             'arguments' => $arguments,
353             'examples' => $examples,
354             'aliases' => $command->getAliases(),
355             'key' => $commandKey,
356             'dashed' => str_replace(':', '-', $command->getName()),
357             'messages' => [
358                 'usage' =>  $this->trans('commands.generate.doc.gitbook.messages.usage'),
359                 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
360                 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
361                 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
362                 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
363                 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
364                 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
365             ],
366         ];
367
368         return $data;
369     }
370
371     public function setContainer($container)
372     {
373         $this->container = $container;
374         $this->registerGenerators();
375         $this->registerCommands();
376     }
377 }