1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\events; |
4
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
6
|
|
|
use Ubiquity\utils\base\UArray; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Manage events |
10
|
|
|
* @author jc |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class EventsManager { |
14
|
|
|
private static $key="events/events"; |
15
|
|
|
/** |
16
|
|
|
* @var array|mixed |
17
|
|
|
*/ |
18
|
|
|
protected static $managedEvents=[]; |
19
|
1 |
|
public static function start(){ |
20
|
1 |
|
if(CacheManager::$cache->exists(self::$key)){ |
21
|
|
|
self::$managedEvents=CacheManager::$cache->fetch(self::$key); |
22
|
|
|
} |
23
|
1 |
|
} |
24
|
|
|
|
25
|
1 |
|
public static function addListener($eventName,$action){ |
26
|
1 |
|
if(!isset(self::$managedEvents[$eventName])){ |
27
|
1 |
|
self::$managedEvents[$eventName]=[]; |
28
|
|
|
} |
29
|
1 |
|
self::$managedEvents[$eventName][]=$action; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
1 |
|
public static function store(){ |
33
|
1 |
|
CacheManager::$cache->store(self::$key, "return ".UArray::asPhpArray(self::$managedEvents,'array').';'); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
18 |
|
public static function trigger($eventName,&...$params){ |
37
|
18 |
|
if(isset(self::$managedEvents[$eventName])){ |
38
|
1 |
|
foreach (self::$managedEvents[$eventName] as $action){ |
39
|
1 |
|
self::triggerOne($action,$params); |
40
|
|
|
} |
41
|
|
|
} |
42
|
18 |
|
} |
43
|
|
|
|
44
|
1 |
|
private static function triggerOne($action,&$params){ |
45
|
1 |
|
if(is_callable($action)){ |
46
|
|
|
call_user_func_array($action, $params); |
47
|
1 |
|
}elseif(is_subclass_of($action, EventListenerInterface::class)){ |
48
|
1 |
|
call_user_func_array([new $action(),"on"], $params); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|