1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Framework\Services; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Containers\SplClosureContainer; |
||
19 | |||
20 | /** |
||
21 | * Class Hooks |
||
22 | * |
||
23 | * @package O2System\Framework |
||
24 | */ |
||
25 | class Hooks extends SplClosureContainer |
||
26 | { |
||
27 | /** |
||
28 | * Hooks::PRE_SYSTEM |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | const PRE_SYSTEM = 'PRE_SYSTEM'; |
||
33 | |||
34 | /** |
||
35 | * Hooks::POST_SYSTEM |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | const POST_SYSTEM = 'POST_SYSTEM'; |
||
40 | |||
41 | /** |
||
42 | * Hooks::PRE_CONTROLLER |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | const PRE_CONTROLLER = 'PRE_CONTROLLER'; |
||
47 | |||
48 | /** |
||
49 | * Hooks::PRE_COMMANDER |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | const PRE_COMMANDER = 'PRE_COMMANDER'; |
||
54 | |||
55 | /** |
||
56 | * Hooks::POST_CONTROLLER |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | const POST_CONTROLLER = 'POST_CONTROLLER'; |
||
61 | |||
62 | /** |
||
63 | * Hooks::POST_COMMANDER |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const POST_COMMANDER = 'POST_COMMANDER'; |
||
68 | |||
69 | // ------------------------------------------------------------------------ |
||
70 | |||
71 | /** |
||
72 | * Hooks::__construct |
||
73 | */ |
||
74 | public function __construct() |
||
75 | { |
||
76 | if (is_file( |
||
77 | $filePath = PATH_APP . 'Config' . DIRECTORY_SEPARATOR . strtolower( |
||
78 | ENVIRONMENT |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
79 | ) . DIRECTORY_SEPARATOR . 'Hooks.php' |
||
80 | )) { |
||
81 | include($filePath); |
||
82 | } elseif (is_file($filePath = PATH_APP . 'Config' . DIRECTORY_SEPARATOR . 'Hooks.php')) { |
||
83 | include($filePath); |
||
84 | } |
||
85 | |||
86 | if (isset($hooks) AND is_array($hooks)) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
87 | foreach ($hooks as $event => $closures) { |
||
88 | if (is_array($closures)) { |
||
89 | foreach ($closures as $closure) { |
||
90 | $this->attach($event, $closure); |
||
91 | } |
||
92 | } elseif ($closures instanceof \Closure) { |
||
93 | $this->attach($event, $closures); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | unset($hooks); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | // ------------------------------------------------------------------------ |
||
102 | |||
103 | /** |
||
104 | * SplClosureContainer::attach |
||
105 | * |
||
106 | * Adds an closure object in the registry. |
||
107 | * |
||
108 | * @param string $offset |
||
109 | * @param \Closure $closure |
||
110 | */ |
||
111 | public function attach($offset, \Closure $closure) |
||
112 | { |
||
113 | if (in_array(strtoupper($offset), [ |
||
114 | self::PRE_SYSTEM, |
||
115 | self::POST_SYSTEM, |
||
116 | self::PRE_CONTROLLER, |
||
117 | self::PRE_COMMANDER, |
||
118 | self::POST_CONTROLLER, |
||
119 | self::POST_COMMANDER, |
||
120 | ])) { |
||
121 | $this->closures[ $offset ][ spl_object_hash($closure) ] = $closure; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // ------------------------------------------------------------------------ |
||
126 | |||
127 | /** |
||
128 | * Hooks::callEvent |
||
129 | * |
||
130 | * @param string $event |
||
131 | */ |
||
132 | public function callEvent($event) |
||
133 | { |
||
134 | if (array_key_exists($event, $this->closures)) { |
||
135 | foreach ($this->closures[ $event ] as $closure) { |
||
136 | call_user_func($closure); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | } |