|
1
|
|
|
<?php |
|
2
|
|
|
namespace Redbox\Hooks; |
|
3
|
|
|
|
|
4
|
|
|
class Actions |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
private static $actions = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $tag |
|
14
|
|
|
* @param string $callback |
|
15
|
|
|
* @param int $priority |
|
16
|
|
|
* @return bool |
|
17
|
|
|
*/ |
|
18
|
28 |
|
public static function addAction($tag = '', $callback = '', $priority = 10) |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
28 |
|
if (empty($tag) || empty($callback)) { |
|
22
|
8 |
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
20 |
|
if (isset(self::$actions[$tag]) === false) { |
|
26
|
20 |
|
self::$actions[$tag] = new Hook($tag); |
|
27
|
10 |
|
} |
|
28
|
|
|
|
|
29
|
20 |
|
self::$actions[$tag]->addHook($priority, $callback); |
|
30
|
|
|
|
|
31
|
20 |
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $action |
|
36
|
|
|
* @param string $value |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
24 |
|
public static function doAction($action = '', $value = '') |
|
40
|
|
|
{ |
|
41
|
24 |
|
if (is_array($action)) { |
|
42
|
4 |
|
foreach ($action as $single) { |
|
43
|
4 |
|
$value = self::execute($single, $value); |
|
44
|
2 |
|
} |
|
45
|
2 |
|
} else { |
|
46
|
20 |
|
$value = self::execute($action, $value); |
|
47
|
|
|
} |
|
48
|
24 |
|
return $value; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $tag |
|
53
|
|
|
* @param string $value |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
24 |
|
private static function execute($tag, $value = '') |
|
57
|
|
|
{ |
|
58
|
24 |
|
if (isset(self::$actions[$tag]) === false) { |
|
59
|
4 |
|
return $value; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
20 |
|
$hooks = self::$actions[$tag]->getHooks(); |
|
63
|
|
|
|
|
64
|
20 |
|
foreach ($hooks as $priority => $callbacks) { |
|
65
|
|
|
do { |
|
66
|
20 |
|
$entry = current($callbacks); |
|
67
|
20 |
|
if (is_callable($entry['callback'])) { |
|
68
|
20 |
|
$value = call_user_func($entry['callback'], $value); |
|
69
|
10 |
|
} |
|
70
|
20 |
|
} while (next($callbacks)); |
|
71
|
10 |
|
} |
|
72
|
20 |
|
return $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $tag |
|
77
|
|
|
* @param string $callback |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
12 |
|
public static function removeAction($tag, $callback) |
|
81
|
|
|
{ |
|
82
|
12 |
|
if (empty($tag) || empty($callback)) { |
|
83
|
8 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
if (isset(self::$actions[$tag]) === true) { |
|
87
|
|
|
$found = false; |
|
88
|
|
|
$hooks = self::$actions[$tag]->getHooks(); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($hooks as $priority => $callbacks) { |
|
91
|
|
|
foreach ($callbacks as $entry) { |
|
92
|
|
|
if ($entry['callback'] == $callback) { |
|
93
|
|
|
self::$actions[$tag]->removeCallbackWithPriority($priority, $callback); |
|
94
|
|
|
$found = true; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $found; |
|
99
|
|
|
} |
|
100
|
4 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $tag |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
28 |
|
public static function removeAllActions($tag) |
|
108
|
|
|
{ |
|
109
|
28 |
|
if (empty($tag)) { |
|
110
|
4 |
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
24 |
|
if (isset(self::$actions[$tag])) { |
|
114
|
20 |
|
self::$actions[$tag]->removeAllHooks(); |
|
115
|
20 |
|
unset(self::$actions[$tag]); |
|
116
|
20 |
|
return true; |
|
117
|
|
|
} |
|
118
|
4 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|