|
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\Traits; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Event\Interfaces\ListenerInterface; |
|
18
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ListenerAwareTrait |
|
22
|
|
|
* |
|
23
|
|
|
* Implementation of ListenerAwareInterface |
|
24
|
|
|
* |
|
25
|
|
|
* @package Phossa2\Event |
|
26
|
|
|
* @author Hong Zhang <[email protected]> |
|
27
|
|
|
* @see ListenerAwareInterface |
|
28
|
|
|
* @version 2.0.0 |
|
29
|
|
|
* @since 2.0.0 added |
|
30
|
|
|
*/ |
|
31
|
|
|
trait ListenerAwareTrait |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* cache for listeners' event handlers |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
* @access protected |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $listener_cache = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function attachListener(ListenerInterface $listener) |
|
45
|
|
|
{ |
|
46
|
|
|
// get the standardized handlers of the $listener |
|
47
|
|
|
$events = $this->listenerEvents($listener); |
|
48
|
|
|
|
|
49
|
|
|
// add to manager's event pool |
|
50
|
|
|
foreach ($events as $handler) { |
|
51
|
|
|
$this->on($handler[0], $handler[1], $handler[2]); |
|
52
|
|
|
} |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function detachListener( |
|
60
|
|
|
ListenerInterface $listener, |
|
61
|
|
|
/*# string */ $eventName = '' |
|
62
|
|
|
) { |
|
63
|
|
|
// get the standardized handlers of the $listener |
|
64
|
|
|
$events = $this->listenerEvents($listener); |
|
65
|
|
|
|
|
66
|
|
|
// try find the match |
|
67
|
|
|
foreach ($events as $handler) { |
|
68
|
|
|
if ('' == $eventName || $handler[0] === $eventName) { |
|
69
|
|
|
$this->off($handler[0], $handler[1]); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get cached listener events (fixed already) |
|
78
|
|
|
* |
|
79
|
|
|
* @param ListenerInterface $listener |
|
80
|
|
|
* @return array |
|
81
|
|
|
* @access protected |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function listenerEvents( |
|
84
|
|
|
ListenerInterface $listener |
|
85
|
|
|
)/*# : array */ { |
|
86
|
|
|
$oid = spl_object_hash($listener); |
|
87
|
|
|
if (!isset($this->listener_cache[$oid])) { |
|
88
|
|
|
$this->listener_cache[$oid] = $this->fixListenerEvents($listener); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->listener_cache[$oid]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* standardize events definition |
|
95
|
|
|
* |
|
96
|
|
|
* @param ListenerInterface $listener |
|
97
|
|
|
* @return array |
|
98
|
|
|
* @access protected |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function fixListenerEvents( |
|
101
|
|
|
ListenerInterface $listener |
|
102
|
|
|
)/*# : array */ { |
|
103
|
|
|
$result = []; |
|
104
|
|
|
foreach ($listener->eventsListening() as $eventName => $data) { |
|
105
|
|
|
$newData = $this->expandToHandler($data); |
|
106
|
|
|
foreach ($newData as $handler) { |
|
107
|
|
|
$result[] = $this->expandWithPriority( |
|
108
|
|
|
$listener, $eventName, $handler |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* standardize to array of 'method1' or ['method1', 20] |
|
117
|
|
|
* |
|
118
|
|
|
* @param mixed $data |
|
119
|
|
|
* @return array |
|
120
|
|
|
* @access protected |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function expandToHandler($data)/*# : array */ |
|
123
|
|
|
{ |
|
124
|
|
|
if (is_callable($data)) { |
|
125
|
|
|
$result = [$data]; |
|
126
|
|
|
} elseif (is_string($data)) { |
|
127
|
|
|
$result = [$data]; |
|
128
|
|
|
} elseif (is_int($data[1])) { |
|
129
|
|
|
$result = [$data]; |
|
130
|
|
|
} else { |
|
131
|
|
|
$result = $data; |
|
132
|
|
|
} |
|
133
|
|
|
return (array) $result; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* standardize one 'method1' or ['method1', 20] |
|
138
|
|
|
* to [eventName, callable, priority] |
|
139
|
|
|
* |
|
140
|
|
|
* @param ListenerInterface $listener |
|
141
|
|
|
* @param string $eventName |
|
142
|
|
|
* @param mixed $data |
|
143
|
|
|
* @return array |
|
144
|
|
|
* @access protected |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function expandWithPriority( |
|
147
|
|
|
ListenerInterface $listener, |
|
148
|
|
|
/*# string */ $eventName, |
|
149
|
|
|
$data |
|
150
|
|
|
)/*# : array */ { |
|
151
|
|
|
if (is_array($data) && is_int($data[1])) { |
|
152
|
|
|
$callable = $this->expandCallable($listener, $data[0]); |
|
153
|
|
|
$priority = $data[1]; |
|
154
|
|
|
} else { |
|
155
|
|
|
$callable = $this->expandCallable($listener, $data); |
|
156
|
|
|
$priority = 50; |
|
157
|
|
|
} |
|
158
|
|
|
return [$eventName, $callable, $priority]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* standardize 'method' or callable to callable |
|
163
|
|
|
* |
|
164
|
|
|
* @param ListenerInterface $listener |
|
165
|
|
|
* @param mixed $callable |
|
166
|
|
|
* @return callable |
|
167
|
|
|
* @access protected |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function expandCallable( |
|
170
|
|
|
ListenerInterface $listener, |
|
171
|
|
|
$callable |
|
172
|
|
|
)/*# : callable */ { |
|
173
|
|
|
if (is_callable($callable)) { |
|
174
|
|
|
return $callable; |
|
175
|
|
|
} else { |
|
176
|
|
|
return [$listener, $callable]; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// from other trait |
|
181
|
|
|
abstract public function on(/*# string */ $eventName, callable $callable, /*# int */ $priority = 50); |
|
182
|
|
|
abstract public function off(/*# string */ $eventName, callable $callable); |
|
183
|
|
|
} |
|
184
|
|
|
|