aead3164c12556814f7859c8ba3ebeedef153d6a
[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  * @package Drupal\Console\Core\EventSubscriber
21  */
22 class CallCommandListener implements EventSubscriberInterface
23 {
24     /**
25      * @var ChainQueue
26      */
27     protected $chainQueue;
28
29     /**
30      * CallCommandListener constructor.
31      * @param ChainQueue $chainQueue
32      */
33     public function __construct(ChainQueue $chainQueue)
34     {
35         $this->chainQueue = $chainQueue;
36     }
37
38     /**
39      * @param ConsoleTerminateEvent $event
40      */
41     public function callCommands(ConsoleTerminateEvent $event)
42     {
43         $command = $event->getCommand();
44
45         /* @var DrupalStyle $io */
46         $io = new DrupalStyle($event->getInput(), $event->getOutput());
47
48         if (!$command instanceof Command) {
49             return;
50         }
51
52         $application = $command->getApplication();
53         $commands = $this->chainQueue->getCommands();
54
55         if (!$commands) {
56             return;
57         }
58
59         foreach ($commands as $chainedCommand) {
60             $callCommand = $application->find($chainedCommand['name']);
61
62             if (!$callCommand) {
63                 continue;
64             }
65
66             $input = new ArrayInput($chainedCommand['inputs']);
67             if (!is_null($chainedCommand['interactive'])) {
68                 $input->setInteractive($chainedCommand['interactive']);
69             }
70
71             $io->text($chainedCommand['name']);
72             $callCommand->run($input, $io);
73         }
74     }
75
76     /**
77      * @{@inheritdoc}
78      */
79     public static function getSubscribedEvents()
80     {
81         return [ConsoleEvents::TERMINATE => 'callCommands'];
82     }
83 }