Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / CallCommandListener.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\EventSubscriber\CallCommandListener.
6  */
7
8 namespace Drupal\Console\Core\EventSubscriber;
9
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
12 use Symfony\Component\Console\Input\ArrayInput;
13 use Symfony\Component\Console\ConsoleEvents;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Utils\ChainQueue;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class CallCommandListener
20  *
21  * @package Drupal\Console\Core\EventSubscriber
22  */
23 class CallCommandListener implements EventSubscriberInterface
24 {
25     /**
26      * @var ChainQueue
27      */
28     protected $chainQueue;
29
30     /**
31      * CallCommandListener constructor.
32      *
33      * @param ChainQueue $chainQueue
34      */
35     public function __construct(ChainQueue $chainQueue)
36     {
37         $this->chainQueue = $chainQueue;
38     }
39
40     /**
41      * @param ConsoleTerminateEvent $event
42      */
43     public function callCommands(ConsoleTerminateEvent $event)
44     {
45         $command = $event->getCommand();
46
47         /* @var DrupalStyle $io */
48         $io = new DrupalStyle($event->getInput(), $event->getOutput());
49
50         if (!$command instanceof Command) {
51             return 0;
52         }
53
54         $application = $command->getApplication();
55         $commands = $this->chainQueue->getCommands();
56
57         if (!$commands) {
58             return 0;
59         }
60
61         foreach ($commands as $chainedCommand) {
62             $callCommand = $application->find($chainedCommand['name']);
63
64             if (!$callCommand) {
65                 continue;
66             }
67
68             $input = new ArrayInput($chainedCommand['inputs']);
69             if (!is_null($chainedCommand['interactive'])) {
70                 $input->setInteractive($chainedCommand['interactive']);
71             }
72
73             $io->text($chainedCommand['name']);
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             }
83         }
84     }
85
86     /**
87      * @{@inheritdoc}
88      */
89     public static function getSubscribedEvents()
90     {
91         return [ConsoleEvents::TERMINATE => 'callCommands'];
92     }
93 }