ee4704bf327cfc4f5909dec9dbcfbe90635e8294
[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     ];
48
49     /**
50      * @var array
51      */
52     private $skipArguments = [
53         'command',
54         'command_name'
55     ];
56
57     /**
58      * ShowGenerateChainListener constructor.
59      *
60      * @param TranslatorManagerInterface $translator
61      */
62     public function __construct(
63         TranslatorManagerInterface $translator
64     ) {
65         $this->translator = $translator;
66     }
67
68     /**
69      * @param ConsoleTerminateEvent $event
70      */
71     public function showGenerateChain(ConsoleTerminateEvent $event)
72     {
73         if ($event->getExitCode() != 0) {
74             return;
75         }
76
77         /* @var Command $command */
78         $command = $event->getCommand();
79         /* @var DrupalStyle $io */
80         $io = new DrupalStyle($event->getInput(), $event->getOutput());
81
82         $command_name = $command->getName();
83
84         $this->skipArguments[] = $command_name;
85
86         if (in_array($command->getName(), $this->skipCommands)) {
87             return;
88         }
89
90         $input = $event->getInput();
91
92         if ($input->getOption('generate-chain')) {
93             $options = array_filter($input->getOptions());
94             foreach ($this->skipOptions as $remove_option) {
95                 unset($options[$remove_option]);
96             }
97
98             $arguments = array_filter($input->getArguments());
99             foreach ($this->skipArguments as $remove_argument) {
100                 unset($arguments[$remove_argument]);
101             }
102
103             $commandData['command'] = $command_name;
104
105             if ($options) {
106                 $commandData['options'] = $options;
107             }
108
109             if ($arguments) {
110                 $commandData['arguments'] = $arguments;
111             }
112
113             $io->commentBlock(
114                 $this->translator->trans('application.messages.chain.generated')
115             );
116
117             $dumper = new Dumper();
118             $tableRows = [
119                 ' -',
120                 $dumper->dump($commandData, 4)
121             ];
122
123             $io->writeln('commands:');
124             $io->table([], [$tableRows], 'compact');
125         }
126     }
127
128     /**
129      * @{@inheritdoc}
130      */
131     public static function getSubscribedEvents()
132     {
133         return [ConsoleEvents::TERMINATE => 'showGenerateChain'];
134     }
135 }