1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.7.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Hooks; |
16
|
|
|
|
17
|
|
|
use Quantum\Exceptions\HookException; |
18
|
|
|
use Quantum\Loader\Setup; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class HookManager |
22
|
|
|
* @package Quantum\Hooks |
23
|
|
|
*/ |
24
|
|
|
class HookManager |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Core hooks |
29
|
|
|
*/ |
30
|
|
|
const CORE_HOOKS = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Registered hooks store |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $store = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Quantum\Hooks\HookManager|null |
40
|
|
|
*/ |
41
|
|
|
private static $instance = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* HookManager constructor |
45
|
|
|
*/ |
46
|
|
|
private function __construct() |
47
|
|
|
{ |
48
|
|
|
if (!config()->has('hooks')) { |
49
|
|
|
config()->import(new Setup('config', 'hooks')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$registeredHooks = array_merge(self::CORE_HOOKS, config()->get('hooks') ?: []); |
53
|
|
|
|
54
|
|
|
foreach ($registeredHooks as $hookName) { |
55
|
|
|
$this->register($hookName); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* HookManager instance |
61
|
|
|
* @return \Quantum\Hooks\HookManager|null |
62
|
|
|
*/ |
63
|
|
|
public static function getInstance(): ?HookManager |
64
|
|
|
{ |
65
|
|
|
if (self::$instance == null) { |
66
|
|
|
self::$instance = new self(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return self::$instance; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adds new listener for given hook |
74
|
|
|
* @param string $name |
75
|
|
|
* @param callable $function |
76
|
|
|
* @throws \Quantum\Exceptions\HookException |
77
|
|
|
*/ |
78
|
|
|
public function on(string $name, callable $function) |
79
|
|
|
{ |
80
|
|
|
if (!$this->exists($name)) { |
81
|
|
|
throw HookException::unregisteredHookName($name); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
self::$store[$name][] = $function; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Fires the hook |
89
|
|
|
* @param string $name |
90
|
|
|
* @param array $args |
91
|
|
|
* @throws \Quantum\Exceptions\HookException |
92
|
|
|
*/ |
93
|
|
|
public function fire(string $name, array $args = null) |
94
|
|
|
{ |
95
|
|
|
if (!$this->exists($name)) { |
96
|
|
|
throw HookException::unregisteredHookName($name); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach (self::$store[$name] as $index => $funcion) { |
100
|
|
|
unset(self::$store[$name][$index]); |
101
|
|
|
$funcion($args); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets all registered hooks |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public static function getRegistered(): array |
110
|
|
|
{ |
111
|
|
|
return self::$store; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Registers new hook |
116
|
|
|
* @param string $name |
117
|
|
|
* @throws \Quantum\Exceptions\HookException |
118
|
|
|
*/ |
119
|
|
|
protected function register(string $name) |
120
|
|
|
{ |
121
|
|
|
if ($this->exists($name)) { |
122
|
|
|
throw HookException::hookDuplicateName($name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
self::$store[$name] = []; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks if hooks registered |
130
|
|
|
* @param string $name |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
protected function exists(string $name): bool |
134
|
|
|
{ |
135
|
|
|
return key_exists($name, self::$store); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|