|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Itkg\DelayEventBundle\EventDispatcher; |
|
4
|
|
|
|
|
5
|
|
|
use Itkg\DelayEventBundle\Event\DelayableEvent; |
|
6
|
|
|
use Itkg\DelayEventBundle\Event\DelayableEvents; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
8
|
|
|
use Itkg\DelayEventBundle\Model\Event as DelayEvent; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* class DelayEventDispatcher |
|
14
|
|
|
*/ |
|
15
|
|
|
class DelayEventDispatcher implements EventDispatcherInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $eligibleEventNames = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventDispatcherInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dispatcher; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
29
|
|
|
* @param array $eligibleEventNames |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, array $eligibleEventNames) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->dispatcher = $dispatcher; |
|
34
|
|
|
$this->eligibleEventNames = $eligibleEventNames; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function dispatch($eventName, Event $event = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$event instanceof DelayEvent) { |
|
43
|
|
|
$this->dispatcher->dispatch($eventName, $event); |
|
44
|
|
|
|
|
45
|
|
|
return $event; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->isEventEligible($eventName, $event)) { |
|
49
|
|
|
$event->setOriginalName($eventName); |
|
50
|
|
|
|
|
51
|
|
|
// Override event name to dispatch a delayed event |
|
52
|
|
|
$event = new DelayableEvent($event); |
|
53
|
|
|
$eventName = DelayableEvents::DELAY; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->dispatcher->dispatch($eventName, $event); |
|
57
|
|
|
|
|
58
|
|
|
return $event; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function addListener($eventName, $listener, $priority = 0) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->dispatcher->addListener($eventName, $listener, $priority); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->dispatcher->addSubscriber($subscriber); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritDoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function removeListener($eventName, $listener) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->dispatcher->removeListener($eventName, $listener); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritDoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->dispatcher->removeSubscriber($subscriber); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritDoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getListeners($eventName = null) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->dispatcher->getListeners($eventName); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function hasListeners($eventName = null) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->dispatcher->hasListeners($eventName); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Proxies all method calls to the original event dispatcher. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $method The method name |
|
113
|
|
|
* @param array $arguments The method arguments |
|
114
|
|
|
* |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function __call($method, $arguments) |
|
118
|
|
|
{ |
|
119
|
|
|
return call_user_func_array(array($this->dispatcher, $method), $arguments); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param String $eventName |
|
124
|
|
|
* @param DelayEvent $event |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
private function isEventEligible($eventName, DelayEvent $event) |
|
129
|
|
|
{ |
|
130
|
|
|
return $event->isDelayed() && in_array($eventName, $this->eligibleEventNames); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|