Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / ShowGenerateInlineListener.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener.
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\Console\Command\Command;
14 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
15 use Drupal\Console\Core\Style\DrupalStyle;
16
17 /**
18  * Class ShowGenerateInlineListener
19  *
20  * @package Drupal\Console\Core\EventSubscriber
21  */
22 class ShowGenerateInlineListener implements EventSubscriberInterface
23 {
24     /**
25      * @var TranslatorManagerInterface
26      */
27     protected $translator;
28
29     /**
30      * @var array
31      */
32     private $skipCommands = [
33         'self-update',
34         'list',
35         'help',
36         'drush'
37     ];
38
39     /**
40      * @var array
41      */
42     private $skipOptions = [
43         'env',
44         'generate-inline',
45         'generate-chain'
46     ];
47
48     /**
49      * @var array
50      */
51     private $skipArguments = [
52         'command',
53         'command_name'
54     ];
55
56     /**
57      * ShowGenerateInlineListener constructor.
58      *
59      * @param TranslatorManagerInterface $translator
60      */
61     public function __construct(
62         TranslatorManagerInterface $translator
63     ) {
64         $this->translator = $translator;
65     }
66
67     /**
68      * @param ConsoleTerminateEvent $event
69      */
70     public function showGenerateInline(ConsoleTerminateEvent $event)
71     {
72         if ($event->getExitCode() != 0) {
73             return;
74         }
75
76         /* @var Command $command */
77         $command = $event->getCommand();
78         /* @var DrupalStyle $io */
79         $io = new DrupalStyle($event->getInput(), $event->getOutput());
80
81         $command_name = $command->getName();
82
83         $this->skipArguments[] = $command_name;
84
85         if (in_array($command->getName(), $this->skipCommands)) {
86             return;
87         }
88
89         $input = $event->getInput();
90         if ($input->getOption('generate-inline')) {
91             $options = array_filter($input->getOptions());
92             foreach ($this->skipOptions as $remove_option) {
93                 unset($options[$remove_option]);
94             }
95
96             $arguments = array_filter($input->getArguments());
97             foreach ($this->skipArguments as $remove_argument) {
98                 unset($arguments[$remove_argument]);
99             }
100
101             $inline = '';
102             foreach ($arguments as $argument_id => $argument) {
103                 if (is_array($argument)) {
104                     $argument = implode(" ", $argument);
105                 } elseif (strstr($argument, ' ')) {
106                     $argument = '"' . $argument . '"';
107                 }
108
109                 $inline .= " $argument";
110             }
111
112             // Refactor and remove nested levels. Then apply to arguments.
113             foreach ($options as $optionName => $optionValue) {
114                 if (is_array($optionValue)) {
115                     foreach ($optionValue as $optionItem) {
116                         if (is_array($optionItem)) {
117                             $inlineValue = implode(
118                                 ' ', array_map(
119                                     function ($v, $k) {
120                                         return $k . ':' . $v;
121                                     },
122                                     $optionItem,
123                                     array_keys($optionItem)
124                                 )
125                             );
126                         } else {
127                             $inlineValue = $optionItem;
128                         }
129                         $inline .= ' --' . $optionName . '="' . $inlineValue . '"';
130                     }
131                 } else {
132                     if (is_bool($optionValue)) {
133                         $inline.= ' --' . $optionName;
134                     } else {
135                         $inline.= ' --' . $optionName . '="' . $optionValue . '"';
136                     }
137                 }
138             }
139
140             // Print YML output and message
141             $io->commentBlock(
142                 $this->translator->trans('application.messages.inline.generated')
143             );
144
145             $io->writeln(
146                 sprintf(
147                     '$ drupal %s %s',
148                     $command_name,
149                     $inline
150                 )
151             );
152         }
153     }
154
155     /**
156      * @{@inheritdoc}
157      */
158     public static function getSubscribedEvents()
159     {
160         return [ConsoleEvents::TERMINATE => 'showGenerateInline'];
161     }
162 }