1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Event Dispatcher |
5
|
|
|
* |
6
|
|
|
* Platine Event Dispatcher is the minimal implementation of PSR 14 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Event Dispatcher |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file ListenerPriorityQueue.php |
33
|
|
|
* |
34
|
|
|
* The ListenerPriorityQueue class used to manage the listener priorities |
35
|
|
|
* |
36
|
|
|
* @package Platine\Event |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Event\Listener; |
48
|
|
|
|
49
|
|
|
use IteratorAggregate; |
50
|
|
|
use SplObjectStorage; |
51
|
|
|
use SplPriorityQueue; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class ListenerPriorityQueue |
55
|
|
|
* @package Platine\Event\Listener |
56
|
|
|
* |
57
|
|
|
* @implements IteratorAggregate<ListenerInterface> |
58
|
|
|
*/ |
59
|
|
|
class ListenerPriorityQueue implements IteratorAggregate |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* The storage |
63
|
|
|
* @var SplObjectStorage<ListenerInterface, int> |
64
|
|
|
*/ |
65
|
|
|
protected SplObjectStorage $storage; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The priority queue |
69
|
|
|
* @var SplPriorityQueue<int, ListenerInterface> |
70
|
|
|
*/ |
71
|
|
|
protected SplPriorityQueue $queue; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create the new instance |
75
|
|
|
* @param SplObjectStorage<ListenerInterface, int>|null $storage the storage to use |
76
|
|
|
* @param SplPriorityQueue<int, ListenerInterface>|null $queue the priority queue |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
?SplObjectStorage $storage = null, |
80
|
|
|
?SplPriorityQueue $queue = null |
81
|
|
|
) { |
82
|
|
|
$this->storage = $storage ?? new SplObjectStorage(); |
83
|
|
|
$this->queue = $queue ?? new SplPriorityQueue(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Insert an listener to the queue. |
88
|
|
|
* |
89
|
|
|
* @param ListenerInterface $listener |
90
|
|
|
* @param int $priority |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function insert(ListenerInterface $listener, int $priority): void |
95
|
|
|
{ |
96
|
|
|
$this->storage->attach($listener, $priority); |
97
|
|
|
$this->queue->insert($listener, $priority); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Remove an listener from the queue. |
102
|
|
|
* |
103
|
|
|
* @param ListenerInterface $listener |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function detach(ListenerInterface $listener): void |
108
|
|
|
{ |
109
|
|
|
if ($this->storage->contains($listener)) { |
110
|
|
|
$this->storage->detach($listener); |
111
|
|
|
$this->refreshQueue(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Clear the queue |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function clear(): void |
120
|
|
|
{ |
121
|
|
|
$this->storage = new SplObjectStorage(); |
122
|
|
|
$this->queue = new SplPriorityQueue(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Checks whether the queue contains the listener. |
127
|
|
|
* |
128
|
|
|
* @param ListenerInterface $listener |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function contains(ListenerInterface $listener): bool |
132
|
|
|
{ |
133
|
|
|
return $this->storage->contains($listener); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return all listeners |
138
|
|
|
* @return SplPriorityQueue<int, ListenerInterface>[] |
139
|
|
|
*/ |
140
|
|
|
public function all(): array |
141
|
|
|
{ |
142
|
|
|
$listeners = []; |
143
|
|
|
foreach ($this->getIterator() as $listener) { |
144
|
|
|
$listeners[] = $listener; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $listeners; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Clones and returns a iterator. |
152
|
|
|
* |
153
|
|
|
* @return SplPriorityQueue<int, ListenerInterface> |
154
|
|
|
*/ |
155
|
|
|
public function getIterator(): SplPriorityQueue |
156
|
|
|
{ |
157
|
|
|
$queue = clone $this->queue; |
158
|
|
|
if (!$queue->isEmpty()) { |
159
|
|
|
$queue->top(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $queue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Refresh the status of the queue |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
protected function refreshQueue(): void |
170
|
|
|
{ |
171
|
|
|
$this->storage->rewind(); |
172
|
|
|
$this->queue = new SplPriorityQueue(); |
173
|
|
|
foreach ($this->storage as $listener) { |
174
|
|
|
$priority = $this->storage->getInfo(); |
175
|
|
|
$this->queue->insert($listener, $priority); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|