Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / ShowGenerateChainListener.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\EventSubscriber\ShowGenerateChainListener.
6  */
7
8 namespace Drupal\Console\Core\EventSubscriber;
9
10 use Symfony\Component\Console\ConsoleEvents;
11 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 use Symfony\Component\Yaml\Dumper;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class ShowGenerateChainListener
20  *
21  * @package Drupal\Console\Core\EventSubscriber
22  */
23 class ShowGenerateChainListener implements EventSubscriberInterface
24 {
25     /**
26      * @var TranslatorManagerInterface
27      */
28     protected $translator;
29
30     /**
31      * @var array
32      */
33     private $skipCommands = [
34         'self-update',
35         'list',
36         'help',
37         'drush'
38     ];
39
40     /**
41      * @var array
42      */
43     private $skipOptions = [
44         'env',
45         'generate-inline',
46         'generate-chain',
47         'no-interaction'
48     ];
49
50     /**
51      * @var array
52      */
53     private $skipArguments = [
54         'command',
55         'command_name'
56     ];
57
58     /**
59      * ShowGenerateChainListener constructor.
60      *
61      * @param TranslatorManagerInterface $translator
62      */
63     public function __construct(
64         TranslatorManagerInterface $translator
65     ) {
66         $this->translator = $translator;
67     }
68
69     /**
70      * @param ConsoleTerminateEvent $event
71      */
72     public function showGenerateChain(ConsoleTerminateEvent $event)
73     {
74         if ($event->getExitCode() != 0) {
75             return;
76         }
77
78         /* @var Command $command */
79         $command = $event->getCommand();
80         /* @var DrupalStyle $io */
81         $io = new DrupalStyle($event->getInput(), $event->getOutput());
82
83         $command_name = $command->getName();
84
85         $this->skipArguments[] = $command_name;
86
87         if (in_array($command->getName(), $this->skipCommands)) {
88             return;
89         }
90
91         $input = $event->getInput();
92
93         if ($input->getOption('generate-chain')) {
94             $options = array_filter($input->getOptions());
95             foreach ($this->skipOptions as $remove_option) {
96                 unset($options[$remove_option]);
97             }
98
99             $arguments = array_filter($input->getArguments());
100             foreach ($this->skipArguments as $remove_argument) {
101                 unset($arguments[$remove_argument]);
102             }
103
104             $commandData['command'] = $command_name;
105
106             if ($options) {
107                 $commandData['options'] = $options;
108             }
109
110             if ($arguments) {
111                 $commandData['arguments'] = $arguments;
112             }
113
114             $io->commentBlock(
115                 $this->translator->trans('application.messages.chain.generated')
116             );
117
118             $dumper = new Dumper();
119             $tableRows = [
120                 ' -',
121                 $dumper->dump($commandData, 4)
122             ];
123
124             $io->writeln('commands:');
125             $io->table([], [$tableRows], 'compact');
126         }
127     }
128
129     /**
130      * @{@inheritdoc}
131      */
132     public static function getSubscribedEvents()
133     {
134         return [ConsoleEvents::TERMINATE => 'showGenerateChain'];
135     }
136 }