|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Bootstrappers\Events; |
|
6
|
|
|
|
|
7
|
|
|
use Opulence\Events\Dispatchers\EventRegistry; |
|
8
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
9
|
|
|
use Opulence\Events\Dispatchers\SynchronousEventDispatcher; |
|
10
|
|
|
use Opulence\Framework\Configuration\Config; |
|
11
|
|
|
use Opulence\Framework\Events\Bootstrappers\EventDispatcherBootstrapper as BaseBootstrapper; |
|
12
|
|
|
use Opulence\Ioc\IContainer; |
|
13
|
|
|
use Opulence\Ioc\IocException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Defines the event dispatcher bootstrapper |
|
17
|
|
|
*/ |
|
18
|
|
|
class EventDispatcherBootstrapper extends BaseBootstrapper |
|
19
|
|
|
{ |
|
20
|
|
|
protected ?array $baseEvents = null; |
|
21
|
|
|
protected ?array $moduleEvents = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getBaseEvents(): array |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->baseEvents === null) { |
|
29
|
|
|
$this->baseEvents = require Config::get('paths', 'config') . '/events.php'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
assert(is_array($this->baseEvents)); |
|
33
|
|
|
|
|
34
|
|
|
return $this->baseEvents; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $baseEvents |
|
39
|
|
|
* |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setBaseEvents(array $baseEvents): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->baseEvents = $baseEvents; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getModuleEvents(): array |
|
53
|
|
|
{ |
|
54
|
|
|
global $abterModuleManager; |
|
55
|
|
|
|
|
56
|
|
|
if ($this->moduleEvents !== null) { |
|
57
|
|
|
return $this->moduleEvents; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$events = $abterModuleManager->getEvents() ?: []; |
|
61
|
|
|
|
|
62
|
|
|
$this->moduleEvents = $events; |
|
63
|
|
|
|
|
64
|
|
|
return $events; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $moduleEvents |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setModuleEvents(array $moduleEvents): self |
|
73
|
|
|
{ |
|
74
|
|
|
$this->moduleEvents = $moduleEvents; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function registerBindings(IContainer $container): void |
|
83
|
|
|
{ |
|
84
|
|
|
$eventRegistry = new EventRegistry(); |
|
85
|
|
|
$container->bindInstance(EventRegistry::class, $eventRegistry); |
|
86
|
|
|
|
|
87
|
|
|
$eventDispatcher = $this->getEventDispatcher($container); |
|
88
|
|
|
$container->bindInstance(IEventDispatcher::class, $eventDispatcher); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets the event dispatcher |
|
93
|
|
|
* |
|
94
|
|
|
* @param IContainer $container The IoC container |
|
95
|
|
|
* |
|
96
|
|
|
* @return IEventDispatcher The event dispatcher |
|
97
|
|
|
* @throws IocException |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getEventDispatcher(IContainer $container): IEventDispatcher |
|
100
|
|
|
{ |
|
101
|
|
|
/** @var EventRegistry $eventRegistry */ |
|
102
|
|
|
$eventRegistry = $container->resolve(EventRegistry::class); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->getEventListenerConfig() as $eventName => $listeners) { |
|
105
|
|
|
foreach ((array)$listeners as $listener) { |
|
106
|
|
|
$eventRegistry->registerListener($eventName, $this->getEventListenerCallback($listener, $container)); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return new SynchronousEventDispatcher($eventRegistry); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
115
|
|
|
* |
|
116
|
|
|
* Gets the list of event names to the list of listeners, which can be callables or "className@classMethod" strings |
|
117
|
|
|
* |
|
118
|
|
|
* @return array The event listener config |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getEventListenerConfig(): array |
|
121
|
|
|
{ |
|
122
|
|
|
$allEvents = $this->getBaseEvents(); |
|
123
|
|
|
|
|
124
|
|
|
foreach ($this->getModuleEvents() as $type => $events) { |
|
125
|
|
|
assert(is_array($events) && count($events) > 0, sprintf('invalid events for "%s"', $type)); |
|
126
|
|
|
|
|
127
|
|
|
if (!isset($allEvents[$type])) { |
|
128
|
|
|
$allEvents[$type] = []; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$allEvents[$type] = array_merge($allEvents[$type], $events); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $allEvents; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|