1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
23
|
|
|
* |
24
|
|
|
* PHP Version 5.3 |
25
|
|
|
* |
26
|
|
|
* @category EventDispatcher |
27
|
|
|
* @package Fwk\Events |
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
29
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
31
|
|
|
* @link http://www.nitronet.org/fwk |
32
|
|
|
*/ |
33
|
|
|
namespace Fwk\Events; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Dispatcher |
37
|
|
|
* |
38
|
|
|
* This class is an Event Dispatcher |
39
|
|
|
* It can be herited by other classes or used as-is. |
40
|
|
|
* |
41
|
|
|
* @category Dispatcher |
42
|
|
|
* @package Fwk\Events |
43
|
|
|
* @author Julien Ballestracci <[email protected]> |
44
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
45
|
|
|
* @link http://www.nitronet.org/fwk |
46
|
|
|
*/ |
47
|
|
|
class Dispatcher |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Listeners attached to this dispatcher |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $listeners; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds a listener |
57
|
|
|
* |
58
|
|
|
* @param string $name Event name |
59
|
|
|
* @param mixed $listener PHP Callable |
60
|
|
|
* |
61
|
|
|
* @return Dispatcher |
62
|
|
|
*/ |
63
|
6 |
|
public function on($name, $listener) |
64
|
|
|
{ |
65
|
6 |
|
$name = strtolower($name); |
66
|
6 |
|
if (!isset($this->listeners[$name])) { |
67
|
6 |
|
$this->listeners[$name] = array(); |
68
|
6 |
|
} |
69
|
|
|
|
70
|
6 |
|
array_push($this->listeners[$name], $listener); |
71
|
|
|
|
72
|
6 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Reflects a class and transform all methods starting by 'on' to |
77
|
|
|
* event callable |
78
|
|
|
* |
79
|
|
|
* @param mixed $listenerObj The listener object |
80
|
|
|
* |
81
|
|
|
* @return Dispatcher |
82
|
|
|
*/ |
83
|
2 |
|
public function addListener($listenerObj) |
84
|
|
|
{ |
85
|
2 |
|
if (!\is_object($listenerObj) || $listenerObj instanceof \Closure) { |
86
|
1 |
|
throw new \InvalidArgumentException("Argument is not an object"); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$reflector = new \ReflectionObject($listenerObj); |
90
|
1 |
|
foreach ($reflector->getMethods() as $method) { |
91
|
1 |
|
$name = $method->getName(); |
92
|
1 |
|
if (\strpos($name, 'on') !== 0) { |
93
|
1 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$eventName = strtolower(\substr($name, 2)); |
97
|
1 |
|
$callable = array($listenerObj, $name); |
98
|
|
|
|
99
|
1 |
|
$this->on($eventName, $callable); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Removes a specific listener |
107
|
|
|
* |
108
|
|
|
* @param string $name Event name |
109
|
|
|
* @param mixed $listener PHP Callable |
110
|
|
|
* |
111
|
|
|
* @return Dispatcher |
112
|
|
|
*/ |
113
|
1 |
|
public function removeListener($name, $listener) |
114
|
|
|
{ |
115
|
1 |
|
$name = strtolower($name); |
116
|
1 |
|
if (!isset($this->listeners[$name])) { |
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
foreach ($this->listeners[$name] as $idx => $callable) { |
121
|
1 |
|
if ($listener === $callable) { |
122
|
1 |
|
unset($this->listeners[$name][$idx]); |
123
|
1 |
|
} |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Removes all listeners for a specific event |
131
|
|
|
* |
132
|
|
|
* @param string $name Event name |
133
|
|
|
* |
134
|
|
|
* @return Dispatcher |
135
|
|
|
*/ |
136
|
1 |
|
public function removeAllListeners($name) |
137
|
|
|
{ |
138
|
1 |
|
$name = strtolower($name); |
139
|
1 |
|
unset($this->listeners[$name]); |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Notify listeners for a given event |
146
|
|
|
* |
147
|
|
|
* @param string|Event $event The Event to be dispatched (or event name) |
148
|
|
|
* @param array $data Shortcut when using $event as a string. If an Event |
149
|
|
|
* instance is provided, this param will be ignored. |
150
|
|
|
* |
151
|
|
|
* @return Event The event |
152
|
|
|
*/ |
153
|
6 |
|
public function notify($event, array $data = array()) |
154
|
|
|
{ |
155
|
6 |
|
if (is_string($event)) { |
156
|
1 |
|
$event = new Event($event, $data); |
157
|
1 |
|
} |
158
|
|
|
|
159
|
6 |
|
$name = strtolower($event->getName()); |
160
|
6 |
|
if (!isset($this->listeners[$name]) |
161
|
6 |
|
|| !is_array($this->listeners[$name]) |
162
|
5 |
|
|| !count($this->listeners[$name]) |
163
|
6 |
|
) { |
164
|
2 |
|
return $event; |
165
|
|
|
} |
166
|
|
|
|
167
|
5 |
|
foreach ($this->listeners[$name] as $callable) { |
168
|
5 |
|
if (!$event->isStopped()) { |
169
|
5 |
|
call_user_func($callable, $event); |
170
|
5 |
|
$event->setProcessed(true); |
171
|
5 |
|
} |
172
|
5 |
|
} |
173
|
|
|
|
174
|
5 |
|
return $event; |
175
|
|
|
} |
176
|
|
|
} |