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.com |
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
|
|
|
* @deprecated static use is deprecated |
49
|
|
|
*/ |
50
|
31 |
|
public static function setInstance(Event $instance) |
51
|
|
|
{ |
52
|
31 |
|
static::$instance = $instance; |
53
|
31 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Global register |
57
|
|
|
* |
58
|
|
|
* @param string $eventName |
59
|
|
|
* @param EventObserverInterface|callable $object observer |
60
|
|
|
* @deprecated static use is deprecated |
61
|
|
|
*/ |
62
|
3 |
|
public static function registerEvent($eventName, $object) |
63
|
|
|
{ |
64
|
3 |
|
static::$instance->register($eventName, $object); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Global trigger |
69
|
|
|
* |
70
|
|
|
* @param string $eventName |
71
|
|
|
* @param array $data |
72
|
|
|
* @deprecated static use is deprecated |
73
|
|
|
*/ |
74
|
4 |
|
public static function triggerEvent($eventName, $data = null) |
75
|
|
|
{ |
76
|
4 |
|
static::$instance->trigger($eventName, $data); |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* method to register an event |
81
|
|
|
* |
82
|
|
|
* @param string $eventName the event to observe |
83
|
|
|
* @param EventObserverInterface|callable $object observer |
84
|
|
|
*/ |
85
|
36 |
|
public function register(string $eventName, $object): void |
86
|
|
|
{ |
87
|
36 |
|
if ($object instanceof EventObserverInterface) { |
88
|
34 |
|
$object = [$object, 'on']; |
89
|
|
|
} |
90
|
36 |
|
if (!is_callable($object)) { |
91
|
1 |
|
throw new \InvalidArgumentException( |
92
|
1 |
|
"Can't register event. Observer is not callable.", |
93
|
1 |
|
1427814905 |
94
|
|
|
); |
95
|
|
|
} |
96
|
35 |
|
$this->registry[$eventName][] = $object; |
97
|
35 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* method to trigger an event |
101
|
|
|
* |
102
|
|
|
* @param string $eventName the event name (register for this name) |
103
|
|
|
* @param array $data array with some additional data |
104
|
|
|
*/ |
105
|
32 |
|
public function trigger(string $eventName, array $data = null): void |
106
|
|
|
{ |
107
|
32 |
|
if (empty($this->registry[$eventName])) { |
108
|
25 |
|
return; |
109
|
|
|
} |
110
|
32 |
|
foreach ($this->registry[$eventName] as $observer) { |
111
|
32 |
|
call_user_func_array($observer, [$eventName, $data]); |
112
|
|
|
} |
113
|
31 |
|
} |
114
|
|
|
} |
115
|
|
|
|