1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpDDD\DomainDrivenDesign\Event; |
4
|
|
|
|
5
|
|
|
use PhpDDD\Utils\ClassUtils; |
6
|
|
|
|
7
|
|
|
final class EventDispatcher implements EventDispatcherInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var EventListener[] |
11
|
|
|
*/ |
12
|
|
|
private $listeners = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param AbstractEvent $event |
16
|
|
|
* @param bool $asynchronous |
17
|
|
|
* |
18
|
|
|
* @return AbstractEvent |
19
|
|
|
*/ |
20
|
|
|
public function publish(AbstractEvent $event, $asynchronous = false) |
21
|
|
|
{ |
22
|
|
|
$eventName = ClassUtils::getCanonicalName($event); |
23
|
|
|
|
24
|
|
|
$this->doPublish($this->getListeners($event, $asynchronous), $eventName, $event); |
25
|
|
|
|
26
|
|
|
return $event; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param EventSubscriberInterface $subscriber |
31
|
|
|
*/ |
32
|
|
|
public function subscribe(EventSubscriberInterface $subscriber) |
33
|
|
|
{ |
34
|
|
|
foreach ($subscriber->getSubscribedEvents() as $subscribedEvent) { |
35
|
|
|
$subscribedEvent->setSubscriber($subscriber); |
36
|
|
|
$this->listeners[] = $subscribedEvent; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param AbstractEvent $event |
42
|
|
|
* @param bool $asynchronous |
43
|
|
|
* |
44
|
|
|
* @return EventListener[] |
45
|
|
|
*/ |
46
|
|
|
public function getListeners(AbstractEvent $event, $asynchronous = false) |
47
|
|
|
{ |
48
|
|
|
return array_filter( |
49
|
|
|
$this->listeners, |
50
|
|
|
function (EventListener $eventSubscriber) use ($asynchronous, $event) { |
51
|
|
|
$supportedEvent = $eventSubscriber->getEventClassName(); |
52
|
|
|
|
53
|
|
|
if ($eventSubscriber->isAsynchronous() !== $asynchronous) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
if (null === $supportedEvent) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $event instanceof $supportedEvent; |
61
|
|
|
} |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Triggers the listeners of an event. |
67
|
|
|
* |
68
|
|
|
* This method can be overridden to add functionality that is executed |
69
|
|
|
* for each listener. |
70
|
|
|
* |
71
|
|
|
* @param EventListener[] $listeners The event listeners. |
72
|
|
|
* @param string $eventName The name of the event to dispatch. |
73
|
|
|
* @param AbstractEvent $event The event object to pass to the event handlers/listeners. |
74
|
|
|
*/ |
75
|
|
|
private function doPublish(array $listeners, $eventName, AbstractEvent $event) |
76
|
|
|
{ |
77
|
|
|
foreach ($listeners as $listener) { |
78
|
|
|
call_user_func($listener->getMethod(), $event, $eventName, $this); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|