|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jellyfish\Event; |
|
6
|
|
|
|
|
7
|
|
|
class EventDispatcher implements EventDispatcherInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \Jellyfish\Event\EventListenerProviderInterface |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $eventListenerProvider; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \Jellyfish\Event\EventQueueProducerInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $eventQueueProducer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param \Jellyfish\Event\EventListenerProviderInterface $eventListenerProvider |
|
21
|
|
|
* @param \Jellyfish\Event\EventQueueProducerInterface $eventQueueProducer |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
EventListenerProviderInterface $eventListenerProvider, |
|
25
|
|
|
EventQueueProducerInterface $eventQueueProducer |
|
26
|
|
|
) { |
|
27
|
|
|
$this->eventListenerProvider = $eventListenerProvider; |
|
28
|
|
|
$this->eventQueueProducer = $eventQueueProducer; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Jellyfish\Event\EventDispatcherInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public function dispatch(EventInterface $event): EventDispatcherInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$this->dispatchSync($event); |
|
39
|
|
|
$this->dispatchAsync($event); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Jellyfish\Event\EventDispatcherInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function dispatchSync(EventInterface $event): EventDispatcherInterface |
|
50
|
|
|
{ |
|
51
|
|
|
$listeners = $this->eventListenerProvider->getListenersByTypeAndEventName( |
|
52
|
|
|
EventListenerInterface::TYPE_SYNC, |
|
53
|
|
|
$event->getName() |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($listeners as $listener) { |
|
57
|
|
|
/** @var \Jellyfish\Event\EventListenerInterface $listener */ |
|
58
|
|
|
$listener->handle($event); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Jellyfish\Event\EventDispatcherInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function dispatchAsync(EventInterface $event): EventDispatcherInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$listeners = $this->eventListenerProvider->getListenersByTypeAndEventName( |
|
72
|
|
|
EventListenerInterface::TYPE_ASYNC, |
|
73
|
|
|
$event->getName() |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($listeners as $listener) { |
|
77
|
|
|
$this->eventQueueProducer->enqueueEvent($event, $listener); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return \Jellyfish\Event\EventListenerProviderInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getEventListenerProvider(): EventListenerProviderInterface |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->eventListenerProvider; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|