1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spryker\Zed\Event\Dependency; |
9
|
|
|
|
10
|
|
|
use ArrayIterator; |
11
|
|
|
use SplPriorityQueue; |
12
|
|
|
use Spryker\Zed\Event\Business\Dispatcher\EventListenerContext; |
13
|
|
|
use Spryker\Zed\Event\Business\Exception\EventListenerNotFoundException; |
14
|
|
|
use Spryker\Zed\Event\Dependency\Plugin\EventBaseHandlerInterface; |
15
|
|
|
|
16
|
|
|
class EventCollection implements EventCollectionInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array<\SplPriorityQueue<mixed, \Spryker\Zed\Event\Business\Dispatcher\EventListenerContextInterface>> |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
protected $eventListeners = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $eventName |
25
|
|
|
* @param \Spryker\Zed\Event\Dependency\Plugin\EventBaseHandlerInterface $eventHandler |
26
|
|
|
* @param int|null $priority |
27
|
|
|
* @param string|null $queuePoolName |
28
|
|
|
* @param string|null $eventQueueName |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function addListener($eventName, EventBaseHandlerInterface $eventHandler, $priority = 0, $queuePoolName = null, $eventQueueName = null) |
33
|
|
|
{ |
34
|
|
|
$this->add($eventName, $eventHandler, false, $priority, $queuePoolName, $eventQueueName); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $eventName |
41
|
|
|
* @param \Spryker\Zed\Event\Dependency\Plugin\EventBaseHandlerInterface $eventHandler |
42
|
|
|
* @param int|null $priority |
43
|
|
|
* @param string|null $queuePoolName |
44
|
|
|
* @param string|null $eventQueueName |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function addListenerQueued($eventName, EventBaseHandlerInterface $eventHandler, $priority = 0, $queuePoolName = null, $eventQueueName = null) |
49
|
|
|
{ |
50
|
|
|
$this->add($eventName, $eventHandler, true, $priority, $queuePoolName, $eventQueueName); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $eventName |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function has($eventName) |
61
|
|
|
{ |
62
|
|
|
return isset($this->eventListeners[$eventName]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $eventName |
67
|
|
|
* @param \Spryker\Zed\Event\Dependency\Plugin\EventBaseHandlerInterface $eventHandler |
68
|
|
|
* @param bool $isHandledInQueue |
69
|
|
|
* @param int|null $priority |
70
|
|
|
* @param string|null $queuePoolName |
71
|
|
|
* @param string|null $eventQueueName |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function add( |
76
|
|
|
$eventName, |
77
|
|
|
EventBaseHandlerInterface $eventHandler, |
78
|
|
|
$isHandledInQueue = false, |
79
|
|
|
$priority = 0, |
80
|
|
|
$queuePoolName = null, |
81
|
|
|
$eventQueueName = null |
82
|
|
|
) { |
83
|
|
|
if (!$this->has($eventName)) { |
84
|
|
|
$this->eventListeners[$eventName] = new SplPriorityQueue(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->eventListeners[$eventName]->insert(new EventListenerContext($eventHandler, $isHandledInQueue, $queuePoolName, $eventQueueName), $priority); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $eventName |
92
|
|
|
* |
93
|
|
|
* @throws \Spryker\Zed\Event\Business\Exception\EventListenerNotFoundException |
94
|
|
|
* |
95
|
|
|
* @return \SplPriorityQueue<mixed, \Spryker\Zed\Event\Business\Dispatcher\EventListenerContextInterface> |
96
|
|
|
*/ |
97
|
|
|
public function get($eventName) |
98
|
|
|
{ |
99
|
|
|
if (!isset($this->eventListeners[$eventName]) || count($this->eventListeners[$eventName]) === 0) { |
100
|
|
|
throw new EventListenerNotFoundException( |
101
|
|
|
sprintf( |
102
|
|
|
'Could not find event listeners for event "%s". You have to add it to EventDependencyProvider.', |
103
|
|
|
$eventName, |
104
|
|
|
), |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->eventListeners[$eventName]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Whether a offset exists |
113
|
|
|
* |
114
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
115
|
|
|
* |
116
|
|
|
* @param mixed $offset |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
#[\ReturnTypeWillChange] |
121
|
|
|
public function offsetExists($offset) |
122
|
|
|
{ |
123
|
|
|
return $this->has($offset); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Offset to retrieve |
128
|
|
|
* |
129
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
130
|
|
|
* |
131
|
|
|
* @param mixed $offset |
132
|
|
|
* |
133
|
|
|
* @return \SplPriorityQueue<mixed, \Spryker\Zed\Event\Business\Dispatcher\EventListenerContextInterface>|array |
134
|
|
|
*/ |
135
|
|
|
#[\ReturnTypeWillChange] |
136
|
|
|
public function offsetGet($offset) |
137
|
|
|
{ |
138
|
|
|
return $this->get($offset); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Offset to set |
143
|
|
|
* |
144
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
145
|
|
|
* |
146
|
|
|
* @param mixed $offset |
147
|
|
|
* @param mixed $value |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
#[\ReturnTypeWillChange] |
152
|
|
|
public function offsetSet($offset, $value) |
153
|
|
|
{ |
154
|
|
|
$this->add($value, $offset); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Offset to unset |
159
|
|
|
* |
160
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
161
|
|
|
* |
162
|
|
|
* @param mixed $offset |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
#[\ReturnTypeWillChange] |
167
|
|
|
public function offsetUnset($offset) |
168
|
|
|
{ |
169
|
|
|
unset($this->eventListeners[$offset]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Retrieve an external iterator |
174
|
|
|
* |
175
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
176
|
|
|
* |
177
|
|
|
* @return \Traversable |
178
|
|
|
*/ |
179
|
|
|
#[\ReturnTypeWillChange] |
180
|
|
|
public function getIterator() |
181
|
|
|
{ |
182
|
|
|
return new ArrayIterator($this->eventListeners); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|