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
|
|
|
if ('' === $eventName) { |
68
|
|
|
$this->events = []; |
69
|
|
|
} elseif ($this->hasEventQueue($eventName)) { |
70
|
|
|
if (null === $callable) { |
71
|
|
|
// remove all |
72
|
|
|
$this->events[$eventName]->flush(); |
73
|
|
|
} else { |
74
|
|
|
// remove callable |
75
|
|
|
$this->events[$eventName]->remove($callable); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// when count is zeror, remove queue |
79
|
|
|
if (count($this->events[$eventName]) === 0) { |
80
|
|
|
unset($this->events[$eventName]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function trigger($event, $context = null, array $properties = []) |
90
|
|
|
{ |
91
|
|
|
// make sure is an event |
92
|
|
|
$evt = $this->newEvent($event, $context, $properties); |
93
|
|
|
|
94
|
|
|
// get handler queue |
95
|
|
|
$queue = $this->getMatchedQueue($evt->getName()); |
96
|
|
|
|
97
|
|
|
// walk thru the queue |
98
|
|
|
foreach ($queue as $q) { |
99
|
|
|
// execute the handler |
100
|
|
|
$q['data']($evt); |
101
|
|
|
|
102
|
|
|
// break out if event stopped |
103
|
|
|
if ($evt->isPropagationStopped()) { |
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Has $eventName been defined ? |
113
|
|
|
* |
114
|
|
|
* @param string $eventName |
115
|
|
|
* @return bool |
116
|
|
|
* @access protected |
117
|
|
|
*/ |
118
|
|
|
protected function hasEventQueue(/*# string */ $eventName)/*# : bool */ |
119
|
|
|
{ |
120
|
|
|
return isset($this->events[$eventName]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create a new event |
125
|
|
|
* |
126
|
|
|
* @param string|EventInterface $eventName |
127
|
|
|
* @param object|string|null $context |
128
|
|
|
* @param array $properties |
129
|
|
|
* @return EventInterface |
130
|
|
|
* @access protected |
131
|
|
|
*/ |
132
|
|
|
protected function newEvent( |
133
|
|
|
$eventName, |
134
|
|
|
$context, |
135
|
|
|
array $properties |
136
|
|
|
)/*# : EventInterface */ { |
137
|
|
|
if (is_object($eventName)) { |
138
|
|
|
return $eventName; |
139
|
|
|
} else { |
140
|
|
|
return new Event($eventName, $context, $properties); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get a new event queue |
146
|
|
|
* |
147
|
|
|
* @return EventQueueInterface |
148
|
|
|
* @access protected |
149
|
|
|
*/ |
150
|
|
|
protected function newEventQueue()/*# : EventQueueInterface */ |
151
|
|
|
{ |
152
|
|
|
return new EventQueue(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get related event handler queue for this $eventName |
157
|
|
|
* |
158
|
|
|
* @param string $eventName |
159
|
|
|
* @return EventQueueInterface |
160
|
|
|
* @access protected |
161
|
|
|
*/ |
162
|
|
|
protected function getMatchedQueue( |
163
|
|
|
/*# : string */ $eventName |
164
|
|
|
)/*# : EventQueueInterface */ { |
165
|
|
|
if ($this->hasEventQueue($eventName)) { |
166
|
|
|
return $this->events[$eventName]; |
167
|
|
|
} else { |
168
|
|
|
return $this->newEventQueue(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|