|
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('Phossa\\Config\\Config'); |
|
37
|
|
|
* ``` |
|
38
|
|
|
* |
|
39
|
|
|
* @package Phossa2\Event |
|
40
|
|
|
* @author Hong Zhang <[email protected]> |
|
41
|
|
|
* @see SharedManagerInterface |
|
42
|
|
|
* @version 2.0.0 |
|
43
|
|
|
* @since 2.0.0 added |
|
44
|
|
|
*/ |
|
45
|
|
|
trait SharedManagerTrait |
|
46
|
|
|
{ |
|
47
|
|
|
use ShareableTrait; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritDoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function onEvent( |
|
53
|
|
|
$scope, |
|
54
|
|
|
/*# string */ $eventName, |
|
55
|
|
|
callable $callable, |
|
56
|
|
|
/*# int */ $priority = 50 |
|
57
|
|
|
) { |
|
58
|
|
|
foreach ((array) $scope as $sc) { |
|
59
|
|
|
/* @var $em EventManagerInterface */ |
|
60
|
|
|
$em = static::getShareable($sc); |
|
61
|
|
|
$em->on($eventName, $callable, $priority); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritDoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function offEvent( |
|
69
|
|
|
$scope, |
|
70
|
|
|
/*# string */ $eventName, |
|
71
|
|
|
callable $callable = null |
|
72
|
|
|
) { |
|
73
|
|
|
foreach ((array) $scope as $sc) { |
|
74
|
|
|
/* @var $em EventManagerInterface */ |
|
75
|
|
|
$em = static::getShareable($sc); |
|
76
|
|
|
$em->off($eventName, $callable); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function onGlobalEvent( |
|
84
|
|
|
/*# string */ $eventName, |
|
85
|
|
|
callable $callable, |
|
86
|
|
|
/*# int */ $priority = 50 |
|
87
|
|
|
) { |
|
88
|
|
|
// scope '' means GLOBAL |
|
89
|
|
|
static::onEvent('', $eventName, $callable, $priority); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function offGlobalEvent( |
|
96
|
|
|
/*# string */ $eventName, |
|
97
|
|
|
callable $callable = null |
|
98
|
|
|
) { |
|
99
|
|
|
// scope '' means GLOBAL |
|
100
|
|
|
static::offEvent('', $eventName, $callable); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Override `getOwnScopes()` in ShareableTrait |
|
105
|
|
|
* |
|
106
|
|
|
* For $this's own scopes, looking for any parent class or interface in |
|
107
|
|
|
* all the available scopes. |
|
108
|
|
|
* |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getOwnScopes()/*# : array */ |
|
112
|
|
|
{ |
|
113
|
|
|
// result |
|
114
|
|
|
$result = []; |
|
115
|
|
|
|
|
116
|
|
|
// all scopes avaible |
|
117
|
|
|
$allScopes = static::getScopes(); |
|
118
|
|
|
|
|
119
|
|
|
// loop thru own scopes |
|
120
|
|
|
foreach ($this->scopes as $scope) { |
|
121
|
|
|
$result[$scope] = true; |
|
122
|
|
|
$this->isSubType($scope, $allScopes, $result); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// alway add global scope |
|
126
|
|
|
$result[''] = true; |
|
127
|
|
|
|
|
128
|
|
|
return array_keys($result); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Is $type a classname or interface name ? |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $type |
|
135
|
|
|
* @return bool |
|
136
|
|
|
* @access protected |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function isAType(/*# string */ $type)/*# : bool */ |
|
139
|
|
|
{ |
|
140
|
|
|
return class_exists($type) || interface_exists($type); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* is $childType child type of one of the $typesToCheck. |
|
145
|
|
|
* |
|
146
|
|
|
* Returns the matched types |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $childType |
|
149
|
|
|
* @param array $typesToCheck |
|
150
|
|
|
* @param array &$result |
|
151
|
|
|
* @access protected |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function isSubType( |
|
154
|
|
|
/*# string */ $childType, |
|
155
|
|
|
array $typesToCheck, |
|
156
|
|
|
array &$result |
|
157
|
|
|
)/*# : bool */ { |
|
158
|
|
|
foreach ($typesToCheck as $type) { |
|
159
|
|
|
if ($this->isAType($childType) && |
|
160
|
|
|
$this->isAType($type) && |
|
161
|
|
|
is_a($childType, $type, true) |
|
162
|
|
|
) { |
|
163
|
|
|
$result[$type] = true; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|