1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Event |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Event; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
19
|
|
|
use Phossa2\Event\Interfaces\EventQueueInterface; |
20
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* EventManager |
24
|
|
|
* |
25
|
|
|
* A basic implementation of EventManagerInterface |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Event |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see ObjectAbstract |
30
|
|
|
* @see EventManagerInterface |
31
|
|
|
* @version 2.1.0 |
32
|
|
|
* @since 2.0.0 added |
33
|
|
|
* @since 2.1.0 updated to use the new EventManagerInterface |
34
|
|
|
*/ |
35
|
|
|
class EventManager extends ObjectAbstract implements EventManagerInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Events managing |
39
|
|
|
* |
40
|
|
|
* @var EventQueueInterface[] |
41
|
|
|
* @access protected |
42
|
|
|
*/ |
43
|
|
|
protected $events = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function attach($event, $callback, $priority = 0) |
49
|
|
|
{ |
50
|
|
|
if (!$this->hasEvent($event)) { |
51
|
|
|
$this->events[$event] = $this->newEventQueue(); |
52
|
|
|
} |
53
|
|
|
$this->events[$event]->insert($callback, $priority); |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function detach($event, $callback) |
61
|
|
|
{ |
62
|
|
|
if ($this->hasEvent($event)) { |
63
|
|
|
$this->removeEventCallable($event, $callback); |
64
|
|
|
} elseif ('' === $event) { |
65
|
|
|
$this->events = []; // remove all events |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function clearListeners($event) |
74
|
|
|
{ |
75
|
|
|
$this->detach($event, null); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function trigger($event, $target = null, $argv = array()) |
82
|
|
|
{ |
83
|
|
|
// result |
84
|
|
|
$res = true; |
85
|
|
|
|
86
|
|
|
// make sure is an event |
87
|
|
|
$evt = $this->newEvent($event, $target, $argv); |
88
|
|
|
|
89
|
|
|
// get handler queue |
90
|
|
|
$queue = $this->getMatchedQueue($evt->getName()); |
91
|
|
|
foreach ($queue as $q) { |
92
|
|
|
// execute the handler |
93
|
|
|
$res = $q['data']($evt); |
94
|
|
|
|
95
|
|
|
// stopped ? |
96
|
|
|
if ($evt->isPropagationStopped()) { |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $res; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Has $eventName been bound ? |
106
|
|
|
* |
107
|
|
|
* @param string $eventName |
108
|
|
|
* @return bool |
109
|
|
|
* @access protected |
110
|
|
|
*/ |
111
|
|
|
protected function hasEvent(/*# string */ $eventName)/*# : bool */ |
112
|
|
|
{ |
113
|
|
|
return isset($this->events[$eventName]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create a new event |
118
|
|
|
* |
119
|
|
|
* @param string|EventInterface $eventName |
120
|
|
|
* @param object|string|null $target |
121
|
|
|
* @param array $parameters |
122
|
|
|
* @return EventInterface |
123
|
|
|
* @access protected |
124
|
|
|
*/ |
125
|
|
|
protected function newEvent( |
126
|
|
|
$eventName, |
127
|
|
|
$target, |
128
|
|
|
array $parameters |
129
|
|
|
)/*# : EventInterface */ { |
130
|
|
|
if (is_object($eventName)) { |
131
|
|
|
return $eventName; |
132
|
|
|
} else { |
133
|
|
|
return new Event($eventName, $target, $parameters); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get a new event queue |
139
|
|
|
* |
140
|
|
|
* @return EventQueueInterface |
141
|
|
|
* @access protected |
142
|
|
|
*/ |
143
|
|
|
protected function newEventQueue()/*# : EventQueueInterface */ |
144
|
|
|
{ |
145
|
|
|
return new EventQueue(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get related event handler queue for this $eventName |
150
|
|
|
* |
151
|
|
|
* @param string $eventName |
152
|
|
|
* @return EventQueueInterface |
153
|
|
|
* @access protected |
154
|
|
|
*/ |
155
|
|
|
protected function getMatchedQueue( |
156
|
|
|
/*# : string */ $eventName |
157
|
|
|
)/*# : EventQueueInterface */ { |
158
|
|
|
if ($this->hasEvent($eventName)) { |
159
|
|
|
return $this->events[$eventName]; |
160
|
|
|
} else { |
161
|
|
|
return $this->newEventQueue(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Remove event or its callable |
167
|
|
|
* |
168
|
|
|
* @param string $eventName |
169
|
|
|
* @param callable|null $callable |
170
|
|
|
* @access protected |
171
|
|
|
*/ |
172
|
|
|
protected function removeEventCallable( |
173
|
|
|
/*# string */ $eventName, |
174
|
|
|
$callable |
175
|
|
|
) { |
176
|
|
|
if (null === $callable) { |
177
|
|
|
// remove all callables |
178
|
|
|
$this->events[$eventName]->flush(); |
179
|
|
|
} else { |
180
|
|
|
// remove one callable |
181
|
|
|
$this->events[$eventName]->remove($callable); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// when count is zeror, remove queue |
185
|
|
|
if (count($this->events[$eventName]) === 0) { |
186
|
|
|
unset($this->events[$eventName]); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|