|
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.0.0 |
|
32
|
|
|
* @since 2.0.0 added |
|
33
|
|
|
*/ |
|
34
|
|
|
class EventManager extends ObjectAbstract implements EventManagerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Events managing |
|
38
|
|
|
* |
|
39
|
|
|
* @var EventQueueInterface[] |
|
40
|
|
|
* @access protected |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $events = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function on( |
|
48
|
|
|
/*# string */ $eventName, |
|
49
|
|
|
callable $callable, |
|
50
|
|
|
/*# int */ $priority = 50 |
|
51
|
|
|
) { |
|
52
|
|
|
if (!$this->hasEventQueue($eventName)) { |
|
53
|
|
|
$this->events[$eventName] = $this->newEventQueue(); |
|
54
|
|
|
} |
|
55
|
|
|
$this->events[$eventName]->insert($callable, $priority); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function off( |
|
64
|
|
|
/*# string */ $eventName = '', |
|
65
|
|
|
callable $callable = null |
|
66
|
|
|
) { |
|
67
|
|
|
// remove all events |
|
68
|
|
|
if ('' === $eventName) { |
|
69
|
|
|
$this->events = []; |
|
70
|
|
|
|
|
71
|
|
|
// dealing with one event |
|
72
|
|
|
} elseif ($this->hasEventQueue($eventName)) { |
|
73
|
|
|
$this->removeEvent($eventName, $callable); |
|
74
|
|
|
} |
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function trigger($event, $context = null, array $properties = []) |
|
82
|
|
|
{ |
|
83
|
|
|
// make sure is an event |
|
84
|
|
|
$evt = $this->newEvent($event, $context, $properties); |
|
85
|
|
|
|
|
86
|
|
|
// get handler queue |
|
87
|
|
|
$queue = $this->getMatchedQueue($evt->getName()); |
|
88
|
|
|
|
|
89
|
|
|
// walk thru the queue |
|
90
|
|
|
foreach ($queue as $q) { |
|
91
|
|
|
// execute the handler |
|
92
|
|
|
$q['data']($evt); |
|
93
|
|
|
|
|
94
|
|
|
// break out if event stopped |
|
95
|
|
|
if ($evt->isPropagationStopped()) { |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Has $eventName been defined ? |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $eventName |
|
107
|
|
|
* @return bool |
|
108
|
|
|
* @access protected |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function hasEventQueue(/*# string */ $eventName)/*# : bool */ |
|
111
|
|
|
{ |
|
112
|
|
|
return isset($this->events[$eventName]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Create a new event |
|
117
|
|
|
* |
|
118
|
|
|
* @param string|EventInterface $eventName |
|
119
|
|
|
* @param object|string|null $context |
|
120
|
|
|
* @param array $properties |
|
121
|
|
|
* @return EventInterface |
|
122
|
|
|
* @access protected |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function newEvent( |
|
125
|
|
|
$eventName, |
|
126
|
|
|
$context, |
|
127
|
|
|
array $properties |
|
128
|
|
|
)/*# : EventInterface */ { |
|
129
|
|
|
if (is_object($eventName)) { |
|
130
|
|
|
return $eventName; |
|
131
|
|
|
} else { |
|
132
|
|
|
return new Event($eventName, $context, $properties); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get a new event queue |
|
138
|
|
|
* |
|
139
|
|
|
* @return EventQueueInterface |
|
140
|
|
|
* @access protected |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function newEventQueue()/*# : EventQueueInterface */ |
|
143
|
|
|
{ |
|
144
|
|
|
return new EventQueue(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get related event handler queue for this $eventName |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $eventName |
|
151
|
|
|
* @return EventQueueInterface |
|
152
|
|
|
* @access protected |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function getMatchedQueue( |
|
155
|
|
|
/*# : string */ $eventName |
|
156
|
|
|
)/*# : EventQueueInterface */ { |
|
157
|
|
|
if ($this->hasEventQueue($eventName)) { |
|
158
|
|
|
return $this->events[$eventName]; |
|
159
|
|
|
} else { |
|
160
|
|
|
return $this->newEventQueue(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Remove event or its callable |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $eventName |
|
168
|
|
|
* @param callable|null $callable |
|
169
|
|
|
* @access protected |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function removeEvent( |
|
172
|
|
|
/*# string */ $eventName, |
|
173
|
|
|
$callable |
|
174
|
|
|
) { |
|
175
|
|
|
if (null === $callable) { |
|
176
|
|
|
// remove all callables |
|
177
|
|
|
$this->events[$eventName]->flush(); |
|
178
|
|
|
} else { |
|
179
|
|
|
// remove one callable |
|
180
|
|
|
$this->events[$eventName]->remove($callable); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// when count is zeror, remove queue |
|
184
|
|
|
if (count($this->events[$eventName]) === 0) { |
|
185
|
|
|
unset($this->events[$eventName]); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|