1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BornFree\TacticianDomainEventBundle\Command; |
4
|
|
|
|
5
|
|
|
use BornFree\TacticianDomainEvent\EventDispatcher\EventSubscriberInterface; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
10
|
|
|
use Symfony\Component\DependencyInjection\Container; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
|
13
|
|
|
class DebugCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $mappings; |
19
|
|
|
|
20
|
|
|
public function __construct(ContainerInterface $container, array $listeners, array $subscribers) |
21
|
|
|
{ |
22
|
|
|
parent::__construct(); |
23
|
|
|
|
24
|
|
|
$events = []; |
25
|
|
|
|
26
|
|
|
foreach ($listeners as $serviceId => $tags) { |
27
|
|
|
foreach ($tags as $tag) { |
28
|
|
|
$listener = $container->get($serviceId); |
29
|
|
|
$method = array_key_exists('method', $tag) ? $tag['method'] : '__invoke'; |
30
|
|
|
|
31
|
|
|
$events[$tag['event']][] = get_class($listener) . '::' . $method; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($subscribers as $serviceId => $tags) { |
36
|
|
|
$subscriber = $container->get($serviceId); |
37
|
|
|
|
38
|
|
|
if (!$subscriber instanceof EventSubscriberInterface) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($subscriber->getSubscribedEvents() as $event => $method) { |
43
|
|
|
$events[$event][] = get_class($subscriber) . '::' . $method[1]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
ksort($events); |
48
|
|
|
|
49
|
|
|
$this->mappings = $events; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function configure() |
53
|
|
|
{ |
54
|
|
|
$this->setName('debug:tactician-domain-events'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
$io = new SymfonyStyle($input, $output); |
60
|
|
|
|
61
|
|
|
$io->title('Tactician domain events'); |
62
|
|
|
$headers = ['Order', 'Callable']; |
63
|
|
|
|
64
|
|
|
foreach ($this->mappings as $event => $listeners) { |
65
|
|
|
$io->section('Event: '.$event); |
66
|
|
|
$io->table($headers, $this->mappingToRows($listeners)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$io->comment('Number of events: '. count($this->mappings)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function mappingToRows(array $listeners) |
73
|
|
|
{ |
74
|
|
|
$rows = []; |
75
|
|
|
foreach ($listeners as $idx => $listener) { |
76
|
|
|
$rows[] = ['#'.($idx+1), $listener]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $rows; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|