Added the Search API Synonym module to deal specifically with licence and license...
[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         'no-interaction'
47     ];
48
49     /**
50      * @var array
51      */
52     private $skipArguments = [
53         'command',
54         'command_name'
55     ];
56
57     /**
58      * ShowGenerateInlineListener 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 showGenerateInline(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         if ($input->getOption('generate-inline')) {
92             $options = array_filter($input->getOptions());
93             foreach ($this->skipOptions as $remove_option) {
94                 unset($options[$remove_option]);
95             }
96
97             $arguments = array_filter($input->getArguments());
98             foreach ($this->skipArguments as $remove_argument) {
99                 unset($arguments[$remove_argument]);
100             }
101
102             $inline = '';
103             foreach ($arguments as $argument_id => $argument) {
104                 if (is_array($argument)) {
105                     $argument = implode(" ", $argument);
106                 } elseif (strstr($argument, ' ')) {
107                     $argument = '"' . $argument . '"';
108                 }
109
110                 $inline .= " $argument";
111             }
112
113             // Refactor and remove nested levels. Then apply to arguments.
114             foreach ($options as $optionName => $optionValue) {
115                 if (is_array($optionValue)) {
116                     foreach ($optionValue as $optionItem) {
117                         if (is_array($optionItem)) {
118                             $inlineValue = implode(
119                                 ', ', array_map(
120                                     function ($v, $k) {
121                                         return '"'.$k . '":"' . $v . '"';
122                                     },
123                                     $optionItem,
124                                     array_keys($optionItem)
125                                 )
126                             );
127                         } else {
128                             $inlineValue = $optionItem;
129                         }
130                         $inline .= ' --' . $optionName . '=\'' . $inlineValue . '\'';
131                     }
132                 } else {
133                     if (is_bool($optionValue)) {
134                         $inline.= ' --' . $optionName;
135                     } else {
136                         $inline.= ' --' . $optionName . '="' . $optionValue . '"';
137                     }
138                 }
139             }
140
141             // Print YML output and message
142             $io->commentBlock(
143                 $this->translator->trans('application.messages.inline.generated')
144             );
145
146             $io->writeln(
147                 sprintf(
148                     '$ drupal %s %s --no-interaction',
149                     $command_name,
150                     $inline
151                 )
152             );
153         }
154     }
155
156     /**
157      * @{@inheritdoc}
158      */
159     public static function getSubscribedEvents()
160     {
161         return [ConsoleEvents::TERMINATE => 'showGenerateInline'];
162     }
163 }