1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/event-dispatcher package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Slince\EventDispatcher; |
13
|
|
|
|
14
|
|
|
class ListenerPriorityQueue implements \IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \SplObjectStorage |
18
|
|
|
*/ |
19
|
|
|
protected $storage; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \SplPriorityQueue |
23
|
|
|
*/ |
24
|
|
|
protected $queue; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->storage = new \SplObjectStorage(); |
29
|
|
|
$this->queue = new \SplPriorityQueue(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Insert an listener to the queue. |
34
|
|
|
* |
35
|
|
|
* @param ListenerInterface $listener |
36
|
|
|
* @param int $priority |
37
|
|
|
*/ |
38
|
|
|
public function insert(ListenerInterface $listener, $priority) |
39
|
|
|
{ |
40
|
|
|
$this->storage->attach($listener, $priority); |
41
|
|
|
$this->queue->insert($listener, $priority); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Removes an listener from the queue. |
46
|
|
|
* |
47
|
|
|
* @param ListenerInterface $listener |
48
|
|
|
*/ |
49
|
|
|
public function detach(ListenerInterface $listener) |
50
|
|
|
{ |
51
|
|
|
if ($this->storage->contains($listener)) { |
52
|
|
|
$this->storage->detach($listener); |
53
|
|
|
$this->refreshQueue(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Clears the queue. |
59
|
|
|
*/ |
60
|
|
|
public function clear() |
61
|
|
|
{ |
62
|
|
|
$this->storage = new \SplObjectStorage(); |
63
|
|
|
$this->queue = new \SplPriorityQueue(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Clears the queue. |
68
|
|
|
* |
69
|
|
|
* @deprecated use clear instead |
70
|
|
|
*/ |
71
|
|
|
public function flush() |
72
|
|
|
{ |
73
|
|
|
$this->clear(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks whether the queue contains the listener. |
78
|
|
|
* |
79
|
|
|
* @param ListenerInterface $listener |
80
|
|
|
* |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function contains(ListenerInterface $listener) |
84
|
|
|
{ |
85
|
|
|
return $this->storage->contains($listener); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets all listeners. |
90
|
|
|
* |
91
|
|
|
* @return ListenerInterface[] |
92
|
|
|
*/ |
93
|
|
|
public function all() |
94
|
|
|
{ |
95
|
|
|
$listeners = []; |
96
|
|
|
foreach ($this->getIterator() as $listener) { |
97
|
|
|
$listeners[] = $listener; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $listeners; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets all listeners. |
105
|
|
|
* |
106
|
|
|
* @deprecated use all instead |
107
|
|
|
*/ |
108
|
|
|
public function getAll() |
109
|
|
|
{ |
110
|
|
|
return $this->all(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Clones and returns a iterator. |
115
|
|
|
* |
116
|
|
|
* @return \SplPriorityQueue |
117
|
|
|
*/ |
118
|
|
|
public function getIterator() |
119
|
|
|
{ |
120
|
|
|
$queue = clone $this->queue; |
121
|
|
|
if (!$queue->isEmpty()) { |
122
|
|
|
$queue->top(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $queue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Refreshes the status of the queue. |
130
|
|
|
*/ |
131
|
|
|
protected function refreshQueue() |
132
|
|
|
{ |
133
|
|
|
$this->storage->rewind(); |
134
|
|
|
$this->queue = new \SplPriorityQueue(); |
135
|
|
|
foreach ($this->storage as $listener) { |
136
|
|
|
$priority = $this->storage->getInfo(); |
137
|
|
|
$this->queue->insert($listener, $priority); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|