Completed
Push — master ( fcde34...4e146b )
by Henry
06:48
created

Hook::_call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.7666
cc 2
nc 2
nop 3
crap 2
1
<?php
2
namespace Redaxscript\Module;
3
4
use Redaxscript\Config;
5
use Redaxscript\Db;
6
use Redaxscript\Filesystem;
7
use Redaxscript\Language;
8
use Redaxscript\Registry;
9
use Redaxscript\Request;
10
use Redaxscript\Validator;
11
use function array_merge;
12
use function call_user_func_array;
13
use function in_array;
14
use function is_array;
15
use function method_exists;
16
use function strlen;
17
18
/**
19
 * parent class to handle module hooks
20
 *
21
 * @since 2.2.0
22
 *
23
 * @package Redaxscript
24
 * @category Module
25
 * @author Henry Ruhs
26
 */
27
28
class Hook
29
{
30
	/**
31
	 * instance of the registry class
32
	 *
33
	 * @var Registry
34
	 */
35
36
	protected static Registry $_registry;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
37
38
	/**
39
	 * instance of the request class
40
	 *
41
	 * @var Request
42
	 */
43
44
	protected static Request $_request;
45
46
	/**
47
	 * instance of the language class
48
	 *
49
	 * @var Language
50
	 */
51
52
	protected static Language $_language;
53
54
	/**
55
	 * instance of the config class
56
	 *
57
	 * @var Config
58
	 */
59
60
	protected static Config $_config;
61
62
	/**
63
	 * module namespace
64
	 *
65
	 * @var string
66
	 */
67
68
	protected static string $_namespace = 'Redaxscript\Modules';
69
70
	/**
71
	 * array of installed and enabled modules
72
	 *
73
	 * @var array
74
	 */
75
76
	protected static array $_moduleArray = [];
77
78
	/**
79
	 * array of triggered events
80
	 *
81
	 * @var array
82
	 */
83
84
	protected static array $_eventArray = [];
85
86
	/**
87
	 * constructor of the class
88
	 *
89
	 * @since 3.0.0
90
	 *
91
	 * @param Registry $registry instance of the registry class
92
	 * @param Request $request instance of the request class
93
	 * @param Language $language instance of the language class
94
	 * @param Config $config instance of the config class
95
	 */
96
97 6
	public static function construct(Registry $registry, Request $request, Language $language, Config $config) : void
98
	{
99 6
		self::$_registry = $registry;
100 6
		self::$_request = $request;
101 6
		self::$_language = $language;
102 6
		self::$_config = $config;
103 6
	}
104
105
	/**
106
	 * init the class
107
	 *
108
	 * @since 2.6.0
109
	 */
110
111 6
	public static function init() : void
112
	{
113 6
		$accessValidator = new Validator\Access();
114 6
		$modulesFilesystem = new Filesystem\Filesystem();
115 6
		$modulesFilesystem->init('modules');
116 6
		$modulesFilesystemArray = $modulesFilesystem->getSortArray();
117 6
		$modulesInstalled = Db::forTablePrefix('modules')->where('status', 1)->findMany();
118
119
		/* process modules */
120
121 6
		foreach ($modulesInstalled as $module)
122
		{
123
			/* validate access */
124
125 6
			if (in_array($module->alias, $modulesFilesystemArray) && $accessValidator->validate($module->access, self::$_registry->get('myGroups')))
126
			{
127 6
				self::$_moduleArray[$module->alias] = $module->alias;
128
			}
129
		}
130 6
	}
131
132
	/**
133
	 * get the module array
134
	 *
135
	 * @since 2.2.0
136
	 *
137
	 * @return array
138
	 */
139
140 1
	public static function getModuleArray() : array
141
	{
142 1
		return self::$_moduleArray;
143
	}
144
145
	/**
146
	 * get the event array
147
	 *
148
	 * @since 3.0.0
149
	 *
150
	 * @return array
151
	 */
152
153 1
	public static function getEventArray() : array
154
	{
155 1
		return self::$_eventArray;
156
	}
157
158
	/**
159
	 * collect from the module hook
160
	 *
161
	 * @since 3.2.0
162
	 *
163
	 * @param string $eventName name of the module event
164
	 * @param array $parameterArray parameter of the module hook
165
	 *
166
	 * @return array
167
	 */
168
169 2
	public static function collect(string $eventName, array $parameterArray = []) : array
170
	{
171 2
		$collectArray = [];
172
173
		/* process modules */
174
175 2
		foreach (self::$_moduleArray as $moduleName)
176
		{
177 2
			$callArray = self::_call($moduleName, $eventName, $parameterArray);
178 2
			if (is_array($callArray))
179
			{
180 1
				$collectArray = array_merge($collectArray, $callArray);
181
			}
182
		}
183 2
		return $collectArray;
184
	}
185
186
	/**
187
	 * trigger the module hook
188
	 *
189
	 * @since 3.2.0
190
	 *
191
	 * @param string $eventName name of the module event
192
	 * @param array $parameterArray parameter of the module hook
193
	 *
194
	 * @return ?string
195
	 */
196
197 3
	public static function trigger(string $eventName, array $parameterArray = []) : ?string
198
	{
199 3
		$output = null;
200
201
		/* process modules */
202
203 3
		foreach (self::$_moduleArray as $moduleName)
204
		{
205 3
			$outputCall = self::_call($moduleName, $eventName, $parameterArray);
206 3
			if (strlen($outputCall))
207
			{
208 2
				$output .= $outputCall;
209
			}
210
		}
211 3
		return $output;
212
	}
213
214
	/**
215
	 * call the module
216
	 *
217
	 * @since 3.2.0
218
	 *
219
	 * @param string $moduleName
220
	 * @param string $eventName
221
	 * @param array $parameterArray
222
	 *
223
	 * @return mixed
224
	 */
225
226 5
	protected static function _call(string $moduleName, string $eventName, array $parameterArray = []) : mixed
227
	{
228 5
		$moduleClass = self::$_namespace . '\\' . $moduleName . '\\' . $moduleName;
229 5
		if (method_exists($moduleClass, $eventName))
230
		{
231 3
			self::$_eventArray[$eventName][$moduleName] = true;
232 3
			$module = new $moduleClass(self::$_registry, self::$_request, self::$_language, self::$_config);
233
			return call_user_func_array(
234
			[
235 3
				$module,
236 3
				$eventName
237 3
			], $parameterArray);
238
		}
239 2
		return null;
240
	}
241
}
242