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\Shared\Shareable\ShareableTrait; |
18
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SharedManagerTrait |
22
|
|
|
* |
23
|
|
|
* Support for shared managers using ShareableTrait |
24
|
|
|
* |
25
|
|
|
* ```php |
26
|
|
|
* // one event manager instance |
27
|
|
|
* $event_dispatcher = new EventDispatcher(); |
28
|
|
|
* |
29
|
|
|
* // global event manager, default scope is '' |
30
|
|
|
* $globalManager = EventDispatcher::getShareable(); |
31
|
|
|
* |
32
|
|
|
* // shared manager for a scope, say 'MVC' |
33
|
|
|
* $MvcManager = EventDispatcher::getShareable('MVC'); |
34
|
|
|
* |
35
|
|
|
* // class/interface level shared manager |
36
|
|
|
* $classManager = EventDispatcher::getShareable('Phossa2\\Config\\Config'); |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* @package Phossa2\Event |
40
|
|
|
* @author Hong Zhang <[email protected]> |
41
|
|
|
* @see SharedManagerInterface |
42
|
|
|
* @see EventManagerInterface |
43
|
|
|
* @version 2.1.0 |
44
|
|
|
* @since 2.0.0 added |
45
|
|
|
* @since 2.1.0 updated |
46
|
|
|
*/ |
47
|
|
|
trait SharedManagerTrait |
48
|
|
|
{ |
49
|
|
|
use ShareableTrait; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public static function onEvent( |
|
|
|
|
55
|
|
|
$scope, |
56
|
|
|
/*# string */ $eventName, |
57
|
|
|
callable $callable, |
58
|
|
|
/*# int */ $priority = 0 |
59
|
|
|
)/*# : bool */ { |
60
|
|
|
foreach ((array) $scope as $sc) { |
61
|
|
|
/* @var $em EventManagerInterface */ |
62
|
|
|
$em = static::getShareable($sc); |
63
|
|
|
$em->attach($eventName, $callable, $priority); |
64
|
|
|
} |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public static function offEvent( |
|
|
|
|
72
|
|
|
$scope, |
73
|
|
|
/*# string */ $eventName = '', |
74
|
|
|
callable $callable = null |
75
|
|
|
)/*# : bool */ { |
76
|
|
|
foreach ((array) $scope as $sc) { |
77
|
|
|
/* @var $em EventManagerInterface */ |
78
|
|
|
$em = static::getShareable($sc); |
79
|
|
|
$em->detach($eventName, $callable); |
80
|
|
|
} |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public static function onGlobalEvent( |
88
|
|
|
/*# string */ $eventName, |
89
|
|
|
callable $callable, |
90
|
|
|
/*# int */ $priority = 0 |
91
|
|
|
)/*# : bool */ { |
92
|
|
|
// scope '' means GLOBAL |
93
|
|
|
return static::onEvent('', $eventName, $callable, $priority); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public static function offGlobalEvent( |
100
|
|
|
/*# string */ $eventName = '', |
101
|
|
|
callable $callable = null |
102
|
|
|
)/*# : bool */ { |
103
|
|
|
// scope '' means GLOBAL |
104
|
|
|
return static::offEvent('', $eventName, $callable); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Override `getOwnScopes()` in ShareableTrait |
109
|
|
|
* |
110
|
|
|
* For $this's own scopes, looking for any parent class or interface in |
111
|
|
|
* all the available scopes. |
112
|
|
|
* |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
protected function getOwnScopes()/*# : array */ |
116
|
|
|
{ |
117
|
|
|
// result |
118
|
|
|
$result = []; |
119
|
|
|
|
120
|
|
|
// all types avaible |
121
|
|
|
$allTypes = $this->getAllTypes(static::getScopes()); |
122
|
|
|
|
123
|
|
|
// loop thru own scopes |
124
|
|
|
foreach ($this->scopes as $scope) { |
125
|
|
|
$result[$scope] = true; |
126
|
|
|
$this->matchParentType($scope, $allTypes, $result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// alway add global scope |
130
|
|
|
$result[''] = true; |
131
|
|
|
|
132
|
|
|
return array_keys($result); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get all types (class or interface) from the given scopes |
137
|
|
|
* |
138
|
|
|
* @param array $scopes |
139
|
|
|
* @return array |
140
|
|
|
* @access protected |
141
|
|
|
*/ |
142
|
|
|
protected function getAllTypes(array $scopes)/*# : array */ |
143
|
|
|
{ |
144
|
|
|
$result = []; |
145
|
|
|
foreach ($scopes as $scope) { |
146
|
|
|
if ($this->isAType($scope)) { |
147
|
|
|
$result[] = $scope; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Is $type a classname or interface name ? |
155
|
|
|
* |
156
|
|
|
* @param string $type |
157
|
|
|
* @return bool |
158
|
|
|
* @access protected |
159
|
|
|
*/ |
160
|
|
|
protected function isAType(/*# string */ $type)/*# : bool */ |
161
|
|
|
{ |
162
|
|
|
return class_exists($type) || interface_exists($type); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* is $childType child type of one of the $typesToCheck. |
167
|
|
|
* |
168
|
|
|
* Returns the matched types |
169
|
|
|
* |
170
|
|
|
* @param string $childType |
171
|
|
|
* @param array $typesToCheck |
172
|
|
|
* @param array &$result |
173
|
|
|
* @access protected |
174
|
|
|
*/ |
175
|
|
|
protected function matchParentType( |
176
|
|
|
/*# string */ $childType, |
177
|
|
|
array $typesToCheck, |
178
|
|
|
array &$result |
179
|
|
|
)/*# : bool */ { |
180
|
|
|
foreach ($typesToCheck as $type) { |
181
|
|
|
if ($this->isAType($childType) && |
182
|
|
|
is_a($childType, $type, true) |
183
|
|
|
) { |
184
|
|
|
$result[$type] = true; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.