Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Application.php
1 <?php
2
3 namespace Drupal\Console\Core;
4
5 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Symfony\Component\EventDispatcher\EventDispatcher;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Application as BaseApplication;
12 use Drupal\Console\Core\EventSubscriber\DefaultValueEventListener;
13 use Drupal\Console\Core\EventSubscriber\ShowGenerateChainListener;
14 use Drupal\Console\Core\EventSubscriber\ShowTipsListener;
15 use Drupal\Console\Core\EventSubscriber\ShowWelcomeMessageListener;
16 use Drupal\Console\Core\EventSubscriber\ValidateExecutionListener;
17 use Drupal\Console\Core\EventSubscriber\ShowGeneratedFilesListener;
18 use Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener;
19 use Drupal\Console\Core\EventSubscriber\CallCommandListener;
20 use Drupal\Console\Core\Utils\ConfigurationManager;
21 use Drupal\Console\Core\Style\DrupalStyle;
22 use Drupal\Console\Core\Utils\ChainDiscovery;
23 use Drupal\Console\Core\Command\Chain\ChainCustomCommand;
24
25 /**
26  * Class Application
27  *
28  * @package Drupal\Console
29  */
30 class Application extends BaseApplication
31 {
32     /**
33      * @var ContainerInterface
34      */
35     protected $container;
36
37     /**
38      * @var string
39      */
40     protected $commandName;
41
42     /**
43      * ConsoleApplication constructor.
44      *
45      * @param ContainerInterface $container
46      * @param string             $name
47      * @param string             $version
48      */
49     public function __construct(
50         ContainerInterface$container,
51         $name,
52         $version
53     ) {
54         $this->container = $container;
55         parent::__construct($name, $version);
56         $this->addOptions();
57     }
58
59     /**
60      * @return TranslatorManagerInterface
61      */
62     public function getTranslator()
63     {
64         if ($this->container) {
65             return $this->container->get('console.translator_manager');
66         }
67
68         return null;
69     }
70
71     /**
72      * @param $key string
73      *
74      * @return string
75      */
76     public function trans($key)
77     {
78         if ($this->getTranslator()) {
79             return $this->getTranslator()->trans($key);
80         }
81
82         return null;
83     }
84
85     /**
86      * {@inheritdoc}
87      */
88     public function doRun(InputInterface $input, OutputInterface $output)
89     {
90         $io = new DrupalStyle($input, $output);
91         if ($commandName = $this->getCommandName($input)) {
92             $this->commandName = $commandName;
93         }
94         $this->registerEvents();
95         $this->registerExtendCommands();
96         $this->registerCommandsFromAutoWireConfiguration();
97         $this->registerChainCommands();
98
99         /**
100          * @var ConfigurationManager $configurationManager
101          */
102         $configurationManager = $this->container
103             ->get('console.configuration_manager');
104
105         if ($commandName && !$this->has($commandName)) {
106             $io->error(
107                 sprintf(
108                     $this->trans('application.errors.invalid-command'),
109                     $this->commandName
110                 )
111             );
112
113             return 1;
114         }
115
116         $code = parent::doRun(
117             $input,
118             $output
119         );
120
121         if ($this->commandName != 'init' && $configurationManager->getMissingConfigurationFiles()) {
122             $io->warning($this->trans('application.site.errors.missing-config-file'));
123             $io->listing($configurationManager->getMissingConfigurationFiles());
124             $io->commentBlock(
125                 $this->trans('application.site.errors.missing-config-file-command')
126             );
127         }
128
129         if ($this->getCommandName($input) == 'list' && $this->container->hasParameter('console.warning')) {
130             $io->warning(
131                 $this->trans($this->container->getParameter('console.warning'))
132             );
133         }
134
135         return $code;
136     }
137
138     /**
139      * registerEvents
140      */
141     private function registerEvents()
142     {
143         $dispatcher = new EventDispatcher();
144         /* @todo Register listeners as services */
145         $dispatcher->addSubscriber(
146             new ValidateExecutionListener(
147                 $this->container->get('console.translator_manager'),
148                 $this->container->get('console.configuration_manager')
149             )
150         );
151         $dispatcher->addSubscriber(
152             new ShowWelcomeMessageListener(
153                 $this->container->get('console.translator_manager')
154             )
155         );
156         $dispatcher->addSubscriber(
157             new DefaultValueEventListener(
158                 $this->container->get('console.configuration_manager')
159             )
160         );
161         $dispatcher->addSubscriber(
162             new ShowTipsListener(
163                 $this->container->get('console.translator_manager')
164             )
165         );
166         $dispatcher->addSubscriber(
167             new CallCommandListener(
168                 $this->container->get('console.chain_queue')
169             )
170         );
171         $dispatcher->addSubscriber(
172             new ShowGeneratedFilesListener(
173                 $this->container->get('console.file_queue'),
174                 $this->container->get('console.show_file')
175             )
176         );
177         $dispatcher->addSubscriber(
178             new ShowGenerateInlineListener(
179                 $this->container->get('console.translator_manager')
180             )
181         );
182         $dispatcher->addSubscriber(
183             new ShowGenerateChainListener(
184                 $this->container->get('console.translator_manager')
185             )
186         );
187
188         $this->setDispatcher($dispatcher);
189     }
190
191     /**
192      * addOptions
193      */
194     private function addOptions()
195     {
196         $this->getDefinition()->addOption(
197             new InputOption(
198                 '--env',
199                 '-e',
200                 InputOption::VALUE_OPTIONAL,
201                 $this->trans('application.options.env'), 'prod'
202             )
203         );
204         $this->getDefinition()->addOption(
205             new InputOption(
206                 '--root',
207                 null,
208                 InputOption::VALUE_OPTIONAL,
209                 $this->trans('application.options.root')
210             )
211         );
212         $this->getDefinition()->addOption(
213             new InputOption(
214                 '--debug',
215                 null,
216                 InputOption::VALUE_NONE,
217                 $this->trans('application.options.debug')
218             )
219         );
220         $this->getDefinition()->addOption(
221             new InputOption(
222                 '--learning',
223                 null,
224                 InputOption::VALUE_NONE,
225                 $this->trans('application.options.learning')
226             )
227         );
228         $this->getDefinition()->addOption(
229             new InputOption(
230                 '--generate-chain',
231                 '-c',
232                 InputOption::VALUE_NONE,
233                 $this->trans('application.options.generate-chain')
234             )
235         );
236         $this->getDefinition()->addOption(
237             new InputOption(
238                 '--generate-inline',
239                 '-i',
240                 InputOption::VALUE_NONE,
241                 $this->trans('application.options.generate-inline')
242             )
243         );
244         $this->getDefinition()->addOption(
245             new InputOption(
246                 '--generate-doc',
247                 '-d',
248                 InputOption::VALUE_NONE,
249                 $this->trans('application.options.generate-doc')
250             )
251         );
252         $this->getDefinition()->addOption(
253             new InputOption(
254                 '--target',
255                 '-t',
256                 InputOption::VALUE_OPTIONAL,
257                 $this->trans('application.options.target')
258             )
259         );
260         $this->getDefinition()->addOption(
261             new InputOption(
262                 '--uri',
263                 '-l',
264                 InputOption::VALUE_REQUIRED,
265                 $this->trans('application.options.uri')
266             )
267         );
268         $this->getDefinition()->addOption(
269             new InputOption(
270                 '--yes',
271                 '-y',
272                 InputOption::VALUE_NONE,
273                 $this->trans('application.options.yes')
274             )
275         );
276     }
277
278     /**
279      * registerExtendCommands
280      */
281     private function registerExtendCommands()
282     {
283         $this->container->get('console.configuration_manager')
284             ->loadExtendConfiguration();
285     }
286
287     /**
288      * registerCommandsFromAutoWireConfiguration
289      */
290     private function registerCommandsFromAutoWireConfiguration()
291     {
292         $configuration = $this->container->get('console.configuration_manager')
293             ->getConfiguration();
294
295         $autoWireForcedCommands = $configuration
296             ->get('application.autowire.commands.forced');
297
298         if (!is_array($autoWireForcedCommands)) {
299             return;
300         }
301
302         foreach ($autoWireForcedCommands as $autoWireForcedCommand) {
303             try {
304                 if (!$autoWireForcedCommand['class']) {
305                     continue;
306                 }
307
308                 $reflectionClass = new \ReflectionClass(
309                     $autoWireForcedCommand['class']
310                 );
311
312                 $arguments = [];
313                 if (array_key_exists('arguments', $autoWireForcedCommand)) {
314                     foreach ($autoWireForcedCommand['arguments'] as $argument) {
315                         $argument = substr($argument, 1);
316                         $arguments[] = $this->container->get($argument);
317                     }
318                 }
319
320                 $command = $reflectionClass->newInstanceArgs($arguments);
321
322                 if (method_exists($command, 'setTranslator')) {
323                     $command->setTranslator(
324                         $this->container->get('console.translator_manager')
325                     );
326                 }
327                 if (method_exists($command, 'setContainer')) {
328                     $command->setContainer(
329                         $this->container->get('service_container')
330                     );
331                 }
332
333                 $this->add($command);
334             } catch (\Exception $e) {
335                 echo $e->getMessage() . PHP_EOL;
336                 continue;
337             }
338         }
339
340         $autoWireNameCommand = $configuration->get(
341             sprintf(
342                 'application.autowire.commands.name.%s',
343                 $this->commandName
344             )
345         );
346
347         if ($autoWireNameCommand) {
348             try {
349                 $arguments = [];
350                 if (array_key_exists('arguments', $autoWireNameCommand)) {
351                     foreach ($autoWireNameCommand['arguments'] as $argument) {
352                         $argument = substr($argument, 1);
353                         $arguments[] = $this->container->get($argument);
354                     }
355                 }
356
357                 $reflectionClass = new \ReflectionClass(
358                     $autoWireNameCommand['class']
359                 );
360                 $command = $reflectionClass->newInstanceArgs($arguments);
361
362                 if (method_exists($command, 'setTranslator')) {
363                     $command->setTranslator(
364                         $this->container->get('console.translator_manager')
365                     );
366                 }
367                 if (method_exists($command, 'setContainer')) {
368                     $command->setContainer(
369                         $this->container->get('service_container')
370                     );
371                 }
372
373                 $this->add($command);
374             } catch (\Exception $e) {
375                 echo $e->getMessage() . PHP_EOL;
376             }
377         }
378     }
379
380     /**
381      * registerChainCommands
382      */
383     public function registerChainCommands()
384     {
385         /**
386          * @var ChainDiscovery $chainDiscovery
387          */
388         $chainDiscovery = $this->container->get('console.chain_discovery');
389         $chainCommands = $chainDiscovery->getChainCommands();
390
391         foreach ($chainCommands as $name => $chainCommand) {
392             try {
393                 $file = $chainCommand['file'];
394                 $description = $chainCommand['description'];
395                 $command = new ChainCustomCommand($name, $description, $file);
396                 $this->add($command);
397             } catch (\Exception $e) {
398                 echo $e->getMessage() . PHP_EOL;
399             }
400         }
401     }
402
403     /**
404      * Finds a command by name or alias.
405      *
406      * @param string $name A command name or a command alias
407      *
408      * @return mixed A Command instance
409      *
410      * Override parent find method to avoid name collisions with automatically
411      * generated command abbreviations.
412      * Command name validation was previously done at doRun method.
413      */
414     public function find($name)
415     {
416         return $this->get($name);
417     }
418 }