1 | <?php |
||
19 | final class EventDispatcherExtension extends CompilerExtension |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | 4 | public function loadConfiguration() |
|
26 | { |
||
27 | 4 | if ($this->isKdybyEventsRegistered()) { |
|
28 | 1 | return; |
|
29 | } |
||
30 | |||
31 | 3 | $containerBuilder = $this->getContainerBuilder(); |
|
32 | 3 | $containerBuilder->addDefinition($this->prefix('eventDispatcher')) |
|
33 | 3 | ->setClass(EventDispatcher::class); |
|
34 | 3 | } |
|
35 | |||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 3 | public function beforeCompile() |
|
41 | { |
||
42 | 3 | $eventDispatcher = $this->getDefinitionByType(EventDispatcherInterface::class); |
|
43 | |||
44 | 3 | if ($this->isKdybyEventsRegistered()) { |
|
45 | 1 | $eventDispatcher->setClass(EventDispatcher::class) |
|
46 | 1 | ->setFactory(NULL); |
|
47 | 1 | } |
|
48 | |||
49 | 3 | $this->addSubscribersToEventDispatcher(); |
|
50 | 3 | $this->bindNetteEvents(); |
|
51 | 3 | $this->bindEventDispatcherToSymfonyConsole(); |
|
52 | 3 | } |
|
53 | |||
54 | |||
55 | /** |
||
56 | * @return bool |
||
57 | */ |
||
58 | 4 | private function isKdybyEventsRegistered() |
|
62 | |||
63 | |||
64 | 3 | private function addSubscribersToEventDispatcher() |
|
65 | { |
||
66 | 3 | $containerBuilder = $this->getContainerBuilder(); |
|
67 | 3 | $eventDispatcher = $this->getDefinitionByType(EventDispatcherInterface::class); |
|
68 | |||
69 | 3 | foreach ($containerBuilder->findByType(EventSubscriberInterface::class) as $eventSubscriberDefinition) { |
|
70 | 2 | $eventDispatcher->addSetup('addSubscriber', ['@' . $eventSubscriberDefinition->getClass()]); |
|
71 | 3 | } |
|
72 | 3 | } |
|
73 | |||
74 | |||
75 | /** |
||
76 | * @param string $type |
||
77 | * @return ServiceDefinition |
||
78 | */ |
||
79 | 3 | private function getDefinitionByType($type) |
|
80 | { |
||
81 | 3 | $containerBuilder = $this->getContainerBuilder(); |
|
82 | 3 | $definitionName = $containerBuilder->getByType($type); |
|
83 | 3 | return $containerBuilder->getDefinition($definitionName); |
|
84 | } |
||
85 | |||
86 | |||
87 | 3 | private function bindNetteEvents() |
|
102 | |||
103 | |||
104 | 2 | private function decorateServiceDefinitionWithNetteEvent( |
|
105 | ServiceDefinition $serviceDefinition, |
||
106 | NetteEventItemInterface $netteEvent |
||
116 | |||
117 | |||
118 | 3 | private function bindEventDispatcherToSymfonyConsole() |
|
126 | |||
127 | } |
||
128 |