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