Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / Command / Chain / ChainCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Chain\ChainCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Chain;
9
10 use Dflydev\PlaceholderResolver\DataSource\ArrayDataSource;
11 use Dflydev\PlaceholderResolver\RegexPlaceholderResolver;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\ArrayInput;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Symfony\Component\Yaml\Parser;
18 use Drupal\Console\Core\Utils\ChainQueue;
19 use Drupal\Console\Core\Utils\ChainDiscovery;
20 use Drupal\Console\Core\Command\Shared\InputTrait;
21
22 /**
23  * Class ChainCommand
24  *
25  * @package Drupal\Console\Core\Command\Chain
26  */
27 class ChainCommand extends BaseCommand
28 {
29     use InputTrait;
30
31     /**
32      * @var ChainQueue
33      */
34     protected $chainQueue;
35
36     /**
37      * ChainCommand constructor.
38      *
39      * @param ChainQueue     $chainQueue
40      * @param ChainDiscovery $chainDiscovery
41      */
42     public function __construct(
43         ChainQueue $chainQueue,
44         ChainDiscovery $chainDiscovery
45     ) {
46         $this->chainQueue = $chainQueue;
47
48         parent::__construct($chainDiscovery);
49         $this->ignoreValidationErrors();
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     protected function configure()
56     {
57         $this
58             ->setName('chain')
59             ->setDescription($this->trans('commands.chain.description'))
60             ->addOption(
61                 'file',
62                 null,
63                 InputOption::VALUE_OPTIONAL,
64                 $this->trans('commands.chain.options.file')
65             );
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function interact(InputInterface $input, OutputInterface $output)
72     {
73         $file = $this->getFileOption();
74
75         $chainContent = $this->chainDiscovery
76             ->parseContent(
77                 $file,
78                 $this->getOptionsAsArray()
79             );
80
81         $inlinePlaceHolders = $this->chainDiscovery
82             ->extractInlinePlaceHolders($chainContent);
83
84         foreach ($inlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
85             if (is_array($inlinePlaceHolderValue)) {
86                 $placeHolderValue = $this->getIo()->choice(
87                     sprintf(
88                         $this->trans('commands.chain.messages.select-value-for-placeholder'),
89                         $inlinePlaceHolder
90                     ),
91                     $inlinePlaceHolderValue,
92                     current($inlinePlaceHolderValue)
93                 );
94             } else {
95                 $placeHolderValue = $this->getIo()->ask(
96                     sprintf(
97                         $this->trans(
98                             'commands.chain.messages.enter-value-for-placeholder'
99                         ),
100                         $inlinePlaceHolder
101                     ),
102                     $inlinePlaceHolderValue
103                 );
104             }
105
106             if (!$input->hasOption($inlinePlaceHolder)) {
107                 $this->addOption(
108                     $inlinePlaceHolder,
109                     null,
110                     InputOption::VALUE_OPTIONAL,
111                     null,
112                     null
113                 );
114             }
115
116             $input->setOption($inlinePlaceHolder, $placeHolderValue);
117         }
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     protected function execute(InputInterface $input, OutputInterface $output)
124     {
125         $interactive = false;
126
127         $file = $input->getOption('file');
128         if (!$file) {
129             $this->getIo()->error($this->trans('commands.chain.messages.missing-file'));
130
131             return 1;
132         }
133
134         $fileSystem = new Filesystem();
135         $file = calculateRealPath($file);
136
137         if (!$fileSystem->exists($file)) {
138             $this->getIo()->error(
139                 sprintf(
140                     $this->trans('commands.chain.messages.invalid-file'),
141                     $file
142                 )
143             );
144
145             return 1;
146         }
147
148         $chainContent = $this->chainDiscovery
149             ->parseContent(
150                 $file,
151                 $this->getOptionsAsArray()
152             );
153
154         // Resolve inlinePlaceHolders
155         $inlinePlaceHolders = $this->chainDiscovery
156             ->extractInlinePlaceHolders($chainContent);
157
158         if ($inlinePlaceHolders) {
159             $this->getIo()->error(
160                 sprintf(
161                     $this->trans('commands.chain.messages.missing-inline-placeholders'),
162                     implode(', ', array_keys($inlinePlaceHolders))
163                 )
164             );
165
166             $this->getIo()->info(
167                 $this->trans(
168                     'commands.chain.messages.set-inline-placeholders'
169                 )
170             );
171
172             foreach ($inlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
173                 $missingInlinePlaceHoldersMessage = sprintf(
174                     '--%s="%s_VALUE"',
175                     $inlinePlaceHolder,
176                     strtoupper($inlinePlaceHolder)
177                 );
178
179                 $this->getIo()->block($missingInlinePlaceHoldersMessage);
180             }
181
182             return 1;
183         }
184
185         // Resolve environmentPlaceHolders
186         $environmentPlaceHolders = $this->chainDiscovery
187             ->extractEnvironmentPlaceHolders($chainContent);
188         if ($environmentPlaceHolders) {
189             $this->getIo()->error(
190                 sprintf(
191                     $this->trans(
192                         'commands.chain.messages.missing-environment-placeholders'
193                     ),
194                     implode(
195                         ', ',
196                         array_values($environmentPlaceHolders)
197                     )
198                 )
199             );
200
201             $this->getIo()->info(
202                 $this->trans(
203                     'commands.chain.messages.set-environment-placeholders'
204                 )
205             );
206
207             foreach ($environmentPlaceHolders as $envPlaceHolder) {
208                 $missingEnvironmentPlaceHoldersMessage = sprintf(
209                     'export %s=%s_VALUE',
210                     $envPlaceHolder,
211                     strtoupper($envPlaceHolder)
212                 );
213
214                 $this->getIo()->block($missingEnvironmentPlaceHoldersMessage);
215             }
216
217             return 1;
218         }
219
220         $parser = new Parser();
221         $chainData = $parser->parse($chainContent);
222
223         $commands = [];
224         if (array_key_exists('commands', $chainData)) {
225             $commands = $chainData['commands'];
226         }
227
228         $chainInlineOptions = $input->getOptions();
229         unset($chainInlineOptions['file']);
230
231         foreach ($commands as $command) {
232             $moduleInputs = [];
233             $arguments = !empty($command['arguments']) ? $command['arguments'] : [];
234             $options = !empty($command['options']) ? $command['options'] : [];
235
236             foreach ($arguments as $key => $value) {
237                 $moduleInputs[$key] = is_null($value) ? '' : $value;
238             }
239
240             foreach ($options as $key => $value) {
241                 $moduleInputs['--'.$key] = is_null($value) ? '' : $value;
242             }
243
244             // Get application global options
245             foreach ($this->getApplication()->getDefinition()->getOptions() as $option) {
246                 $optionName = $option->getName();
247                 if (array_key_exists($optionName, $chainInlineOptions)) {
248                     $optionValue = $chainInlineOptions[$optionName];
249                     // Set global option only if is not available in command options
250                     if (!isset($moduleInputs['--' . $optionName]) && $optionValue) {
251                         $moduleInputs['--' . $optionName] = $optionValue;
252                     }
253                 }
254             }
255
256             $application = $this->getApplication();
257             $callCommand = $application->find($command['command']);
258
259             if (!$callCommand) {
260                 continue;
261             }
262
263             $this->getIo()->text($command['command']);
264             $this->getIo()->newLine();
265
266             $input = new ArrayInput($moduleInputs);
267             if (!is_null($interactive)) {
268                 $input->setInteractive($interactive);
269             }
270
271             $allowFailure = array_key_exists('allow_failure', $command)?$command['allow_failure']:false;
272             try {
273                 $callCommand->run($input, $this->getIo());
274             } catch (\Exception $e) {
275                 if (!$allowFailure) {
276                     $this->getIo()->error($e->getMessage());
277                     return 1;
278                 }
279             }
280         }
281
282         return 0;
283     }
284 }