-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvent.php
133 lines (119 loc) · 3.62 KB
/
Event.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <[email protected]>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Event;
class Event
{
/**
* The registered listeners.
*
* @var array
* @access protected
*/
private static $listeners = [];
/**
* Add new listener
*
* Event::addListener('event_name', function() { echo 'Test Event 1'; }, 1);
* Event::addListener('event_name', function() { echo 'Test Event 2'; }, 2);
*
* @access public
* @param string $event Event name
* @param mixed $function Function to add
* @param integer $priority Priority. Default is 10
* @param array $args Function aguments
* @return void
*/
public static function addListener(string $event, $function, int $priority = 10, array $args = null) : void
{
Event::$listeners[$event][$priority][] = [
'function' => $function,
'args' => $args
];
}
/**
* Get listeners array
*
* $listeners = Event::getListeners();
*
* @access public
* @return array
*/
public static function getListeners() : array
{
return Event::$listeners;
}
/**
* Remove all listeners for current event.
*
* Event::removeAllListeners('event_name');
*
* @access public
* @param string $event Event name
* @return void
*/
public static function removeAllListeners(string $event) : void
{
if (Event::hasListeners($event)) {
unset(Event::$listeners[$event]);
}
}
/**
* Check is listeners exists for current event.
*
* if (Event::hasListeners('event_name')) {
* // do something...
* }
*
* @access public
* @param string $event Event name
* @return bool $return Return data or not. Default is false
*/
public static function hasListeners(string $event) : bool
{
if (! isset(Event::$listeners[$event]) || count(Event::$listeners[$event]) === 0) {
return false;
}
return true;
}
/**
* Dispatch all listeners of the given event.
*
* Event::dispatch('event_name');
*
* @access public
* @param string $event Event name
* @param array $args Arguments
* @param bool $return Return data or not. Default is false
* @return mixed
*/
public static function dispatch(string $event, array $args = [], bool $return = false)
{
// is there some registered $listeners ?
if (isset(Event::$listeners[$event]) && count(Event::$listeners[$event]) > 0) {
// Get all listeners for given event.
$listeners = Event::$listeners[$event];
// Sort by priority
krsort($listeners);
// Loop through $listeners array
foreach ($listeners as $listener) {
// Loop through $_listener array
foreach ($listener as $_listener) {
// Return or Render specific $_listener results ?
if ($return) {
return call_user_func_array($_listener['function'], (isset($args) ? $args : $_listener['args']));
} else {
call_user_func_array($_listener['function'], (isset($args) ? $args : $_listener['args']));
}
}
}
}
}
}