1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Event class |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Core; |
6
|
|
|
|
7
|
|
|
use Phile\Gateway\EventObserverInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* the Event class for implementing a hook/event system |
11
|
|
|
* |
12
|
|
|
* @author PhileCMS |
13
|
|
|
* @link https://philecms.github.io |
14
|
|
|
* @license http://opensource.org/licenses/MIT |
15
|
|
|
* @package Phile\Core |
16
|
|
|
*/ |
17
|
|
|
class Event |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Event global instance |
22
|
|
|
* @deprecated static use is deprecated |
23
|
|
|
*/ |
24
|
|
|
protected static $instance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Registry object provides storage for objects. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $registry = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* get global event instance |
35
|
|
|
* |
36
|
|
|
* @return Event |
37
|
|
|
* @deprecated static use is deprectated |
38
|
|
|
*/ |
39
|
|
|
public static function getInstance() |
40
|
|
|
{ |
41
|
|
|
return static::$instance; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set global event instance |
46
|
|
|
* |
47
|
|
|
* @param Event $instance |
48
|
|
|
* @return void |
49
|
|
|
* @deprecated static use is deprecated |
50
|
|
|
*/ |
51
|
37 |
|
public static function setInstance(Event $instance): void |
52
|
|
|
{ |
53
|
37 |
|
static::$instance = $instance; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Global register |
58
|
|
|
* |
59
|
|
|
* @param string $eventName |
60
|
|
|
* @param EventObserverInterface|callable $object observer |
61
|
|
|
* @return void |
62
|
|
|
* @deprecated static use is deprecated |
63
|
|
|
*/ |
64
|
3 |
|
public static function registerEvent($eventName, $object): void |
65
|
|
|
{ |
66
|
3 |
|
static::$instance->register($eventName, $object); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Global trigger |
71
|
|
|
* |
72
|
|
|
* @param string $eventName |
73
|
|
|
* @param array $data |
74
|
|
|
* @return void |
75
|
|
|
* @deprecated static use is deprecated |
76
|
|
|
*/ |
77
|
2 |
|
public static function triggerEvent($eventName, $data = null): void |
78
|
|
|
{ |
79
|
2 |
|
static::$instance->trigger($eventName, $data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* method to register an event |
84
|
|
|
* |
85
|
|
|
* @param string $eventName the event to observe |
86
|
|
|
* @param EventObserverInterface|callable $object observer |
87
|
|
|
*/ |
88
|
42 |
|
public function register(string $eventName, $object): void |
89
|
|
|
{ |
90
|
42 |
|
if ($object instanceof EventObserverInterface) { |
91
|
40 |
|
$object = [$object, 'on']; |
92
|
|
|
} |
93
|
42 |
|
if (!is_callable($object)) { |
94
|
1 |
|
throw new \InvalidArgumentException( |
95
|
|
|
"Can't register event. Observer is not callable.", |
96
|
|
|
1427814905 |
97
|
|
|
); |
98
|
|
|
} |
99
|
41 |
|
$this->registry[$eventName][] = $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* method to trigger an event |
104
|
|
|
* |
105
|
|
|
* @param string $eventName the event name (register for this name) |
106
|
|
|
* @param array $data array with some additional data |
107
|
|
|
*/ |
108
|
40 |
|
public function trigger(string $eventName, array $data = null): void |
109
|
|
|
{ |
110
|
40 |
|
if (empty($this->registry[$eventName])) { |
111
|
30 |
|
return; |
112
|
|
|
} |
113
|
39 |
|
foreach ($this->registry[$eventName] as $observer) { |
114
|
39 |
|
call_user_func_array($observer, [$eventName, $data]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|