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
|
29 |
|
public static function setInstance(Event $instance) |
51
|
|
|
{ |
52
|
29 |
|
static::$instance = $instance; |
|
|
|
|
53
|
29 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Global register |
57
|
|
|
* |
58
|
|
|
* @param $eventName |
59
|
|
|
* @param $object |
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 $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
|
34 |
|
public function register($eventName, $object) |
86
|
|
|
{ |
87
|
34 |
|
if ($object instanceof EventObserverInterface) { |
88
|
32 |
|
$object = [$object, 'on']; |
89
|
|
|
} |
90
|
34 |
|
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
|
33 |
|
$this->registry[$eventName][] = $object; |
97
|
33 |
|
} |
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
|
30 |
|
public function trigger($eventName, $data = null) |
106
|
|
|
{ |
107
|
30 |
|
if (empty($this->registry[$eventName])) { |
108
|
24 |
|
return; |
109
|
|
|
} |
110
|
30 |
|
foreach ($this->registry[$eventName] as $observer) { |
111
|
30 |
|
call_user_func_array($observer, [$eventName, $data]); |
112
|
|
|
} |
113
|
29 |
|
} |
114
|
|
|
} |
115
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.