38c05a43e346b2ee0e1df4f71790e6ca3004439f
[yaffs-website] / vendor / drupal / console / src / Command / EventDebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\EventDebugCommand.
6  */
7
8 namespace Drupal\Console\Command;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Console\Core\Style\DrupalStyle;
16
17 /**
18  * Class EventDebugCommand
19  *
20  *  @package Drupal\Console\Command
21  */
22 class EventDebugCommand extends Command
23 {
24     use CommandTrait;
25
26     protected $eventDispatcher;
27
28     /**
29      * EventDebugCommand constructor.
30      *
31      * @param $eventDispatcher
32      */
33     public function __construct($eventDispatcher)
34     {
35         $this->eventDispatcher = $eventDispatcher;
36         parent::__construct();
37     }
38
39     /**
40      * {@inheritdoc}
41      */
42     protected function configure()
43     {
44         $this
45             ->setName('event:debug')
46             ->setDescription($this->trans('commands.event.debug.description'))
47             ->addArgument(
48                 'event',
49                 InputArgument::OPTIONAL,
50                 $this->trans('commands.event.debug.arguments.event'),
51                 null
52             )
53             ->setHelp($this->trans('commands.event.debug.help'));
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     protected function execute(InputInterface $input, OutputInterface $output)
60     {
61         $io = new DrupalStyle($input, $output);
62
63         $events = array_keys($this->eventDispatcher->getListeners());
64         $event = $input->getArgument('event');
65         
66         if ($event) {
67             if (!in_array($event, $events)) {
68                 throw new \Exception(
69                     sprintf(
70                         $this->trans('commands.event.debug.messages.no-events'),
71                         $event
72                     )
73                 );
74             }
75             
76             $dispatcher = $this->eventDispatcher->getListeners($event);
77             $listeners = [];
78             
79             foreach ($dispatcher as $key => $value) {
80                 $reflection = new \ReflectionClass(get_class($value[0]));
81                 $listeners[] = [$reflection->getName(), $value[1]];
82             }
83  
84             $tableHeader = [
85                $this->trans('commands.event.debug.messages.class'),
86                $this->trans('commands.event.debug.messages.method'),
87
88             ];
89
90             $tableRows = [];
91             foreach ($listeners as $key => $element) {
92                 $tableRows[] = [
93                     'class' => $element['0'],
94                     'method' => $element['1']
95                  ];
96             }
97
98             $io->table($tableHeader, $tableRows);
99
100             return 0;
101         }
102        
103         $io->table(
104             [$this->trans('commands.event.debug.messages.event')],
105             $events
106         );
107     }
108 }