Yaffs site version 1.1
[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 Symfony\Component\Yaml\Yaml;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class EventDebugCommand
20  *
21  *  @package Drupal\Console\Command
22  */
23 class EventDebugCommand extends Command
24 {
25     use CommandTrait;
26
27     protected $eventDispatcher;
28
29     /**
30      * EventDebugCommand constructor.
31      *
32      * @param $eventDispatcher
33      */
34     public function __construct($eventDispatcher)
35     {
36         $this->eventDispatcher = $eventDispatcher;
37         parent::__construct();
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function configure()
44     {
45         $this
46             ->setName('event:debug')
47             ->setDescription($this->trans('commands.event.debug.description'))
48             ->addArgument(
49                 'event',
50                 InputArgument::OPTIONAL,
51                 $this->trans('commands.event.debug.arguments.event'),
52                 null
53             )
54             ->setHelp($this->trans('commands.event.debug.help'));
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     protected function execute(InputInterface $input, OutputInterface $output)
61     {
62         $io = new DrupalStyle($input, $output);
63
64         $events = array_keys($this->eventDispatcher->getListeners());
65         $event = $input->getArgument('event');
66         
67         if ($event) {
68             if (!in_array($event, $events)) {
69                 throw new \Exception(
70                     sprintf(
71                         $this->trans('commands.event.debug.messages.no-events'),
72                         $event
73                     )
74                 );
75             }
76             
77             $dispatcher = $this->eventDispatcher->getListeners($event);
78             $listeners = [];
79             
80             foreach ($dispatcher as $key => $value) {
81                 $reflection = new \ReflectionClass(get_class($value[0]));
82                 $className = $reflection->getName();
83
84                 if (!$reflection->hasMethod('getSubscribedEvents')) {
85                     $reflection = new \ReflectionClass($reflection->getParentClass());
86                 }
87
88                 $eventObject = $reflection->newInstanceWithoutConstructor();
89                 $reflectionMethod = new \ReflectionMethod(
90                     $reflection->getName(),
91                     'getSubscribedEvents'
92                 );
93
94                 $subscribedEvents = $reflectionMethod->invoke(
95                     $eventObject
96                 );
97
98                 if (!is_array($subscribedEvents[$event])) {
99                     $subscribedEvents[$event] = [$subscribedEvents[$event]];
100                 }
101
102                 $subscribedEventData = [];
103                 foreach ($subscribedEvents[$event] as $subscribedEvent) {
104                     if (!is_array($subscribedEvent)) {
105                         $subscribedEvent = [$subscribedEvent, 0];
106                     }
107                     if ($subscribedEvent[0] == $value[1]) {
108                         $subscribedEventData = [
109                             $subscribedEvent[0] => isset($subscribedEvent[1])?$subscribedEvent[1]:0
110                         ];
111                     }
112                 }
113
114                 $listeners[] = [
115                     'class' => $className,
116                     'method' => $value[1],
117                     'events' => Yaml::dump($subscribedEventData, 4, 2)
118                 ];
119             }
120  
121             $tableHeader = [
122                $this->trans('commands.event.debug.messages.class'),
123                $this->trans('commands.event.debug.messages.method'),
124             ];
125
126             $tableRows = [];
127             foreach ($listeners as $key => $element) {
128                 $tableRows[] = [
129                     'class' => $element['class'],
130                     'events' => $element['events']
131                  ];
132             }
133
134             $io->table($tableHeader, $tableRows);
135
136             return 0;
137         }
138        
139         $io->table(
140             [$this->trans('commands.event.debug.messages.event')],
141             $events
142         );
143     }
144 }