-
Notifications
You must be signed in to change notification settings - Fork 13
/
HookTrait.php
175 lines (151 loc) · 4.48 KB
/
HookTrait.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace atk4\core;
trait HookTrait
{
/**
* Check this property to see if trait is present in the object.
*
* @var bool
*/
public $_hookTrait = true;
/**
* Contains information about configured hooks (callbacks).
*
* @var array
*/
protected $hooks = [];
/**
* @deprecated use onHook instead
*/
public function addHook($spot, $fx, $args = null, $priority = null)
{
return $this->onHook(...func_get_args());
}
/**
* Add another callback to be executed during hook($hook_spot);.
*
* If priority is negative, then hooks will be executed in reverse order.
*
* @param string $spot Hook identifier to bind on
* @param object|callable $fx Will be called on hook()
* @param array $args Arguments are passed to $fx
* @param int $priority Lower priority is called sooner
*
* @return $this
*/
public function onHook($spot, $fx = null, $args = [], $priority = 5)
{
$fx = $fx ?: $this;
$args = (array) $args;
// multiple hooks can be linked
if (is_string($spot) && strpos($spot, ',') !== false) {
$spot = explode(',', $spot);
}
if (is_array($spot)) {
foreach ($spot as $h) {
$this->onHook($h, $fx, $args, $priority);
}
return $this;
}
// short for onHook('test', $this); to call $this->test();
if (!is_callable($fx)) {
$valid = false;
if (is_object($fx)) {
$valid = (isset($fx->_dynamicMethodTrait) && $fx->hasMethod($spot)) || method_exists($fx, $spot);
}
if (!$valid) {
throw new Exception([
'$fx should be a valid callback',
'fx' => $fx,
]);
}
$fx = [$fx, $spot];
}
if (!isset($this->hooks[$spot][$priority])) {
$this->hooks[$spot][$priority] = [];
}
if ($priority >= 0) {
$this->hooks[$spot][$priority][] = [$fx, $args];
} else {
array_unshift($this->hooks[$spot][$priority], [$fx, $args]);
}
return $this;
}
/**
* Delete all hooks for specified spot.
*
* @param string $spot Hook identifier to bind on
*
* @return $this
*/
public function removeHook($spot)
{
unset($this->hooks[$spot]);
return $this;
}
/**
* Returns true if at least one callback is defined for this hook.
*
* @param string $spot Hook identifier
*
* @return bool
*/
public function hookHasCallbacks($spot)
{
return isset($this->hooks[$spot]);
}
/**
* Execute all callables assigned to $hook_spot.
*
* @param string $spot Hook identifier
* @param array $args Additional arguments to callables
*
* @throws Exception
*
* @return mixed Array of responses or value specified to breakHook
*/
public function hook($spot, $args = null)
{
$args = (array) $args;
$return = [];
try {
if (
isset($this->hooks[$spot])
&& is_array($this->hooks[$spot])
) {
krsort($this->hooks[$spot]); // lower priority is called sooner
$hookBackup = $this->hooks[$spot];
while ($_data = array_pop($this->hooks[$spot])) {
foreach ($_data as &$data) {
$return[] = call_user_func_array(
$data[0],
array_merge(
[$this],
$args,
$data[1]
)
);
}
}
$this->hooks[$spot] = $hookBackup;
}
} catch (HookBreaker $e) {
$this->hooks[$spot] = $hookBackup;
return $e->return_value;
}
return $return;
}
/**
* When called from inside a hook callable, will stop execution of other
* callables on the same hook. The passed argument will be returned by the
* hook method.
*
* @param mixed $return What would hook() return?
*
* @throws HookBreaker
*/
public function breakHook($return)
{
throw new HookBreaker($return);
}
}