1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Messaging; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\Messaging\Exception\ListenerNotAvailableException; |
28
|
|
|
use Brickoo\Component\Messaging\Exception\ListenersNotAvailableException; |
29
|
|
|
use Brickoo\Component\Common\Assert; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ListenerCollection |
33
|
|
|
* |
34
|
|
|
* Implements a listener collection. |
35
|
|
|
* @author Celestino Diaz <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class ListenerCollection { |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
private $listenerQueues; |
41
|
|
|
|
42
|
|
|
/** @var array */ |
43
|
|
|
private $listeners; |
44
|
|
|
|
45
|
1 |
|
public function __construct() { |
46
|
1 |
|
$this->listenerQueues = []; |
47
|
1 |
|
$this->listeners = []; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add a listener to the memory. |
52
|
|
|
* @param \Brickoo\Component\Messaging\Listener $listener |
53
|
|
|
* @return string the listener queue unique identifier |
54
|
|
|
*/ |
55
|
1 |
|
public function add(Listener $listener) { |
56
|
1 |
|
if (!$this->hasListeners(($messageName = $listener->getMessageName()))) { |
57
|
1 |
|
$this->listenerQueues[$messageName] = new ListenerPriorityQueue(); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
$listenerUID = spl_object_hash($listener); |
61
|
1 |
|
$this->listeners[$listenerUID] = $listener; |
62
|
1 |
|
$this->listenerQueues[$messageName]->insert($listenerUID, $listener->getPriority()); |
63
|
|
|
|
64
|
1 |
|
return $listenerUID; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the listener matching the unique identifier. |
69
|
|
|
* @param string $listenerUID the listener unique identifier |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
* @throws \Brickoo\Component\Messaging\Exception\ListenerNotAvailableException |
72
|
|
|
* @return \Brickoo\Component\Messaging\MessageListener |
73
|
|
|
*/ |
74
|
2 |
|
public function get($listenerUID) { |
75
|
2 |
|
Assert::isString($listenerUID); |
76
|
|
|
|
77
|
2 |
|
if (!$this->has($listenerUID)) { |
78
|
1 |
|
throw new ListenerNotAvailableException($listenerUID); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $this->listeners[$listenerUID]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if the listener with the unique identifier is available. |
86
|
|
|
* @param string $listenerUID the listener unique identifier to check |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
* @return boolean check result |
89
|
|
|
*/ |
90
|
1 |
|
public function has($listenerUID) { |
91
|
1 |
|
Assert::isString($listenerUID); |
92
|
1 |
|
return isset($this->listeners[$listenerUID]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove the listener by its unique identifier. |
97
|
|
|
* @param string $listenerUID the listener unique identifier |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
|
|
* @throws \Brickoo\Component\Messaging\Exception\ListenerNotAvailableException |
100
|
|
|
* @return \Brickoo\Component\Messaging\ListenerCollection |
101
|
|
|
*/ |
102
|
2 |
|
public function remove($listenerUID) { |
103
|
2 |
|
Assert::isString($listenerUID); |
104
|
|
|
|
105
|
2 |
|
if (!$this->has($listenerUID)) { |
106
|
1 |
|
throw new ListenerNotAvailableException($listenerUID); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$messageName = $this->get($listenerUID)->getMessageName(); |
110
|
1 |
|
unset($this->listeners[$listenerUID]); |
111
|
1 |
|
$this->getListenerPriorityQueue($messageName)->remove($listenerUID); |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the message responsible listeners. |
118
|
|
|
* @param string $messageName the message name to retrieve the queue from |
119
|
|
|
* @throws \InvalidArgumentException |
120
|
|
|
* @throws \Brickoo\Component\Messaging\Exception\ListenersNotAvailableException |
121
|
|
|
* @return array the collected message listeners ordered by priority. |
122
|
|
|
*/ |
123
|
2 |
|
public function getListeners($messageName) { |
124
|
2 |
|
Assert::isString($messageName); |
125
|
|
|
|
126
|
2 |
|
if (!$this->hasListeners($messageName)) { |
127
|
1 |
|
throw new ListenersNotAvailableException($messageName); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this->collectMessageListeners($messageName); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check if the message has listeners listening. |
135
|
|
|
* @param string $messageName the message name to check |
136
|
|
|
* @throws \InvalidArgumentException if an argument is not valid |
137
|
|
|
* @return boolean check result |
138
|
|
|
*/ |
139
|
1 |
|
public function hasListeners($messageName) { |
140
|
1 |
|
Assert::isString($messageName); |
141
|
1 |
|
return (isset($this->listenerQueues[$messageName])); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Collect the message corresponding listeners ordered by priority. |
146
|
|
|
* @param string $messageName the message to collect the listeners for |
147
|
|
|
* @return array the collected message listeners ordered by priority. |
148
|
|
|
*/ |
149
|
1 |
|
private function collectMessageListeners($messageName) { |
150
|
1 |
|
$listeners = []; |
151
|
|
|
|
152
|
1 |
|
foreach ($this->getListenerPriorityQueue($messageName) as $listenerUID) { |
153
|
1 |
|
$listeners[] = $this->get($listenerUID); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
1 |
|
return $listeners; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the message corresponding listener queue. |
161
|
|
|
* @param string $messageName |
162
|
|
|
* @return \Brickoo\Component\Messaging\ListenerPriorityQueue |
163
|
|
|
*/ |
164
|
1 |
|
private function getListenerPriorityQueue($messageName) { |
165
|
1 |
|
return $this->listenerQueues[$messageName]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|