Skip to content

Commit 8986c38

Browse files
authored
[console] Add allow_failure flag. (#152)
1 parent 3895bd3 commit 8986c38

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/Command/Chain/ChainCommand.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* Class ChainCommand
27+
*
2728
* @package Drupal\Console\Core\Command\Chain
2829
*/
2930
class ChainCommand extends Command
@@ -43,6 +44,7 @@ class ChainCommand extends Command
4344

4445
/**
4546
* ChainCommand constructor.
47+
*
4648
* @param ChainQueue $chainQueue
4749
* @param ChainDiscovery $chainDiscovery
4850
*/
@@ -292,6 +294,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
292294
$commands = $configData['commands'];
293295
}
294296

297+
$parameterOptions = $input->getOptions();
298+
unset($parameterOptions['file']);
299+
295300
foreach ($commands as $command) {
296301
$moduleInputs = [];
297302
$arguments = !empty($command['arguments']) ? $command['arguments'] : [];
@@ -305,11 +310,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
305310
$moduleInputs['--'.$key] = is_null($value) ? '' : $value;
306311
}
307312

308-
$parameterOptions = $input->getOptions();
309-
unset($parameterOptions['file']);
310-
foreach ($parameterOptions as $key => $value) {
311-
if ($value===true) {
312-
$moduleInputs['--' . $key] = true;
313+
foreach ($this->getApplication()->getDefinition()->getOptions() as $option) {
314+
$optionName = $option->getName();
315+
if (array_key_exists($optionName, $parameterOptions)) {
316+
$optionValue = $parameterOptions[$optionName];
317+
if ($optionValue) {
318+
$moduleInputs['--' . $optionName] = $optionValue;
319+
}
313320
}
314321
}
315322

@@ -320,13 +327,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
320327
continue;
321328
}
322329

330+
$io->text($command['command']);
331+
$io->newLine();
332+
323333
$input = new ArrayInput($moduleInputs);
324334
if (!is_null($interactive)) {
325-
$input->setInteractive($interactive);
335+
$input->setInteractive($interactive);
326336
}
327337

328-
$io->text($command['command']);
329-
$callCommand->run($input, $io);
338+
$allowFailure = array_key_exists('allow_failure', $command)?$command['allow_failure']:false;
339+
try {
340+
$callCommand->run($input, $io);
341+
} catch (\Exception $e) {
342+
if (!$allowFailure) {
343+
$io->error($e->getMessage());
344+
return 1;
345+
}
346+
}
330347
}
331348

332349
return 0;

src/EventSubscriber/CallCommandListener.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
/**
1919
* Class CallCommandListener
20+
*
2021
* @package Drupal\Console\Core\EventSubscriber
2122
*/
2223
class CallCommandListener implements EventSubscriberInterface
@@ -28,6 +29,7 @@ class CallCommandListener implements EventSubscriberInterface
2829

2930
/**
3031
* CallCommandListener constructor.
32+
*
3133
* @param ChainQueue $chainQueue
3234
*/
3335
public function __construct(ChainQueue $chainQueue)
@@ -46,14 +48,14 @@ public function callCommands(ConsoleTerminateEvent $event)
4648
$io = new DrupalStyle($event->getInput(), $event->getOutput());
4749

4850
if (!$command instanceof Command) {
49-
return;
51+
return 0;
5052
}
5153

5254
$application = $command->getApplication();
5355
$commands = $this->chainQueue->getCommands();
5456

5557
if (!$commands) {
56-
return;
58+
return 0;
5759
}
5860

5961
foreach ($commands as $chainedCommand) {
@@ -69,7 +71,15 @@ public function callCommands(ConsoleTerminateEvent $event)
6971
}
7072

7173
$io->text($chainedCommand['name']);
72-
$callCommand->run($input, $io);
74+
$allowFailure = array_key_exists('allow_failure', $chainedCommand)?$chainedCommand['allow_failure']:false;
75+
try {
76+
$callCommand->run($input, $io);
77+
} catch (\Exception $e) {
78+
if (!$allowFailure) {
79+
$io->error($e->getMessage());
80+
return 1;
81+
}
82+
}
7383
}
7484
}
7585

0 commit comments

Comments
 (0)