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