1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CacheManager |
19
|
|
|
* @package phpFastCache |
20
|
|
|
* |
21
|
|
|
* @method Void onCacheGetItem() onCacheGetItem(Callable $callable) |
22
|
|
|
* @method Void onCacheSaveItem() onCacheSaveItem(Callable $callable) |
23
|
|
|
* @method Void onCacheSaveDeferredItem() onCacheSaveDeferredItem(Callable $callable) |
24
|
|
|
* @method Void onCacheCommitItem() onCacheCommitItem(Callable $callable) |
25
|
|
|
* @method Void onCacheClearItem() onCacheClearItem(Callable $callable) |
26
|
|
|
* |
27
|
|
|
* |
28
|
|
|
* @method Void onCacheItemSet() onCacheItemSet(Callable $callable) |
29
|
|
|
* @method Void onCacheItemExpireAt() onCacheItemExpireAt(Callable $callable) |
30
|
|
|
* @method Void onCacheItemExpireAfter() onCacheItemExpireAfter(Callable $callable) |
31
|
|
|
* |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
class EventManager |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var $this |
38
|
|
|
*/ |
39
|
|
|
protected static $instance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $events = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return \phpFastCache\EventManager |
48
|
|
|
*/ |
49
|
|
|
public static function getInstance() |
50
|
|
|
{ |
51
|
|
|
return (self::$instance ?: self::$instance = new self); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* EventManager constructor. |
56
|
|
|
*/ |
57
|
|
|
protected function __construct() |
58
|
|
|
{ |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $eventName |
63
|
|
|
* @param array ...$args |
64
|
|
|
*/ |
65
|
|
|
public function dispatch($eventName, ...$args) |
66
|
|
|
{ |
67
|
|
|
$eventName = 'on' . ucfirst($eventName); |
68
|
|
|
if(array_key_exists($eventName, $this->events)){ |
69
|
|
|
foreach ($this->events[ $eventName ] as $event) { |
70
|
|
|
call_user_func_array($event, $args); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @param array $arguments |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
* @throws \BadMethodCallException |
80
|
|
|
*/ |
81
|
|
|
public function __call($name, $arguments) |
82
|
|
|
{ |
83
|
|
|
if(strpos($name, 'on') === 0){ |
84
|
|
|
if(is_callable($arguments[0])){ |
85
|
|
|
if(isset($arguments[1]) && is_string($arguments[0])){ |
86
|
|
|
$this->events[$name][$arguments[1]] = $arguments[0]; |
87
|
|
|
}else { |
88
|
|
|
$this->events[$name][] = $arguments[0]; |
89
|
|
|
} |
90
|
|
|
}else{ |
91
|
|
|
throw new \InvalidArgumentException(sprintf('Expected Callable, got "%s"', gettype($arguments[0]))); |
92
|
|
|
} |
93
|
|
|
}else{ |
94
|
|
|
throw new \BadMethodCallException('An event must start with "on" such as "onCacheGetItem"'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $eventName |
100
|
|
|
* @param $callbackName |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function unbindEventCallback($eventName, $callbackName) |
104
|
|
|
{ |
105
|
|
|
if(isset($this->events[$eventName][$callbackName])){ |
106
|
|
|
unset($this->events[$eventName][$callbackName]); |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|