1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventyClassic; |
4
|
|
|
|
5
|
|
|
class Events |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Holds all registered actions. |
10
|
|
|
* |
11
|
|
|
* @var Action |
12
|
|
|
*/ |
13
|
|
|
protected $action; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Holds all registered filters. |
17
|
|
|
* |
18
|
|
|
* @var Filter |
19
|
|
|
*/ |
20
|
|
|
protected $filter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructs the class. |
24
|
|
|
* |
25
|
|
|
* @param null|Action $action |
26
|
|
|
* @param null|Filter $filter |
27
|
|
|
*/ |
28
|
17 |
|
public function __construct(Action $action = null, Filter $filter = null) |
29
|
|
|
{ |
30
|
17 |
|
if (!$action) { |
31
|
17 |
|
$this->action = new Action; |
32
|
|
|
} |
33
|
17 |
|
if (!$filter) { |
34
|
17 |
|
$this->filter = new Filter; |
35
|
|
|
} |
36
|
17 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets the action instance. |
40
|
|
|
* |
41
|
|
|
* @return Action |
42
|
|
|
*/ |
43
|
4 |
|
public function getAction() |
44
|
|
|
{ |
45
|
4 |
|
return $this->action; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the action instance. |
51
|
|
|
* |
52
|
|
|
* @return Filter |
53
|
|
|
*/ |
54
|
4 |
|
public function getFilter() |
55
|
|
|
{ |
56
|
4 |
|
return $this->filter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adds an action. |
61
|
|
|
* |
62
|
|
|
* @param string $hook Hook name. |
63
|
|
|
* @param mixed $callback Function to execute. |
64
|
|
|
* @param integer $priority Priority of the action. |
65
|
|
|
* @param integer $arguments Number of arguments to accept. |
66
|
|
|
* |
67
|
|
|
* @return Events |
68
|
|
|
*/ |
69
|
9 |
|
public function addAction($hook, $callback, $priority = 20, $arguments = 1) |
70
|
|
|
{ |
71
|
9 |
|
$this->action->listen($hook, $callback, $priority, $arguments); |
72
|
|
|
|
73
|
9 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Removes an action. |
78
|
|
|
* |
79
|
|
|
* @param string $hook Hook name. |
80
|
|
|
* @param mixed $callback Function to execute. |
81
|
|
|
* @param int $priority Priority of the action. |
82
|
|
|
*/ |
83
|
1 |
|
public function removeAction($hook, $callback, $priority = 20) |
84
|
|
|
{ |
85
|
1 |
|
$this->action->remove($hook, $callback, $priority); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Removes all actions. |
90
|
|
|
* |
91
|
|
|
* @param string $hook Hook name. |
92
|
|
|
*/ |
93
|
2 |
|
public function removeAllActions($hook = null) |
94
|
|
|
{ |
95
|
2 |
|
$this->action->removeAll($hook); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adds a filter. |
100
|
|
|
* |
101
|
|
|
* @param string $hook Hook name. |
102
|
|
|
* @param mixed $callback Function to execute. |
103
|
|
|
* @param integer $priority Priority of the action. |
104
|
|
|
* @param integer $arguments Number of arguments to accept. |
105
|
|
|
* |
106
|
|
|
* @return Events |
107
|
|
|
*/ |
108
|
8 |
|
public function addFilter($hook, $callback, $priority = 20, $arguments = 1) |
109
|
|
|
{ |
110
|
8 |
|
$this->filter->listen($hook, $callback, $priority, $arguments); |
111
|
|
|
|
112
|
8 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Removes a filter. |
117
|
|
|
* |
118
|
|
|
* @param string $hook Hook name. |
119
|
|
|
* @param mixed $callback Function to execute. |
120
|
|
|
* @param int $priority Priority of the action. |
121
|
|
|
*/ |
122
|
1 |
|
public function removeFilter($hook, $callback, $priority = 20) |
123
|
|
|
{ |
124
|
1 |
|
$this->filter->remove($hook, $callback, $priority); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Removes all filters. |
129
|
|
|
* |
130
|
|
|
* @param string $hook Hook name |
131
|
|
|
*/ |
132
|
2 |
|
public function removeAllFilters($hook = null) |
133
|
|
|
{ |
134
|
2 |
|
$this->filter->removeAll($hook); |
135
|
2 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Runs an action. |
139
|
|
|
* |
140
|
|
|
* Actions never return anything. It is merely a way of executing code at a specific time in your code. |
141
|
|
|
* You can add as many parameters as you'd like. |
142
|
|
|
* |
143
|
|
|
* @param array ...$args First argument will be the name of the hook, and the rest will be args for the hook. |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
5 |
|
public function runAction() |
148
|
|
|
{ |
149
|
5 |
|
$hook = $this->createHook(func_get_args()); |
150
|
5 |
|
$this->action->fire($hook->name, $hook->args); |
151
|
4 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Runs a filter. |
155
|
|
|
* |
156
|
|
|
* Filters should always return something. The first parameter will always be the default value. |
157
|
|
|
* You can add as many parameters as you'd like. |
158
|
|
|
* |
159
|
|
|
* @param array ...$args First argument will be the name of the hook, and the rest will be args for the hook. |
160
|
|
|
* |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
4 |
|
public function runFilter() |
164
|
|
|
{ |
165
|
4 |
|
$hook = $this->createHook(func_get_args()); |
166
|
4 |
|
return $this->filter->fire($hook->name, $hook->args); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Figures out the hook. |
171
|
|
|
* |
172
|
|
|
* Will return an object with two keys. One for the name and one for the arguments that will be |
173
|
|
|
* passed to the hook itself. |
174
|
|
|
* |
175
|
|
|
* @param mixed ...$args |
176
|
|
|
* |
177
|
|
|
* @return \stdClass |
178
|
|
|
*/ |
179
|
9 |
|
protected function createHook($args) |
180
|
|
|
{ |
181
|
|
|
return (object) [ |
182
|
9 |
|
'name' => $args[0], |
183
|
9 |
|
'args' => array_values(array_slice($args, 1)), |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|