@@ -12,98 +12,98 @@ |
||
12 | 12 | */ |
13 | 13 | class DataType |
14 | 14 | { |
15 | - const ARRAY = 'array'; |
|
16 | - |
|
17 | - const BOOL = 'bool'; |
|
18 | - |
|
19 | - const BOOLEAN = 'bool'; |
|
20 | - |
|
21 | - const DOUBLE = 'float'; |
|
22 | - |
|
23 | - const FLOAT = 'float'; |
|
24 | - |
|
25 | - const EDITOR = 'editor'; |
|
26 | - |
|
27 | - const EMAIL = 'email'; |
|
28 | - |
|
29 | - const FQCN = 'fqcn'; |
|
30 | - |
|
31 | - const HTML = 'html'; |
|
32 | - |
|
33 | - const INT = 'int'; |
|
34 | - |
|
35 | - const INTEGER = 'int'; |
|
36 | - |
|
37 | - const KEY = 'key'; |
|
38 | - |
|
39 | - const OBJECT = 'object'; |
|
40 | - |
|
41 | - const NULL = 'null'; |
|
42 | - |
|
43 | - const TITLE = 'title'; |
|
44 | - |
|
45 | - const URL = 'url'; |
|
46 | - |
|
47 | - const STRING = 'string'; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * @param mixed $param |
|
52 | - * @param string $type |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public static function setDataType($param, string $type) |
|
56 | - { |
|
57 | - switch ($type) { |
|
58 | - case DataType::NULL: |
|
59 | - return null; |
|
60 | - case DataType::OBJECT: |
|
61 | - return $param; |
|
62 | - case DataType::EDITOR: |
|
63 | - case DataType::EMAIL: |
|
64 | - case DataType::FQCN: |
|
65 | - case DataType::HTML: |
|
66 | - case DataType::KEY: |
|
67 | - case DataType::STRING: |
|
68 | - case DataType::TITLE: |
|
69 | - case DataType::URL: |
|
70 | - settype($param, DataType::STRING); |
|
71 | - break; |
|
72 | - default: |
|
73 | - settype($param, $type); |
|
74 | - } |
|
75 | - return $param; |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - public static function convertModelFieldSchemaType($schema_type): ?string |
|
80 | - { |
|
81 | - if (is_array($schema_type)) { |
|
82 | - foreach ($schema_type as $type) { |
|
83 | - $data_type = self::convertModelFieldSchemaType($type); |
|
84 | - if ($data_type) { |
|
85 | - return $data_type; |
|
86 | - } |
|
87 | - } |
|
88 | - $schema_type = reset($schema_type); |
|
89 | - } |
|
90 | - switch ($schema_type) { |
|
91 | - case 'array': |
|
92 | - return DataType::ARRAY; |
|
93 | - case 'boolean': |
|
94 | - return DataType::BOOL; |
|
95 | - case 'integer': |
|
96 | - return DataType::INT; |
|
97 | - case 'null': |
|
98 | - return DataType::NULL; |
|
99 | - case 'number': |
|
100 | - return DataType::FLOAT; |
|
101 | - case 'object': |
|
102 | - return DataType::OBJECT; |
|
103 | - case 'string': |
|
104 | - return DataType::STRING; |
|
105 | - default: |
|
106 | - return null; |
|
107 | - } |
|
108 | - } |
|
15 | + const ARRAY = 'array'; |
|
16 | + |
|
17 | + const BOOL = 'bool'; |
|
18 | + |
|
19 | + const BOOLEAN = 'bool'; |
|
20 | + |
|
21 | + const DOUBLE = 'float'; |
|
22 | + |
|
23 | + const FLOAT = 'float'; |
|
24 | + |
|
25 | + const EDITOR = 'editor'; |
|
26 | + |
|
27 | + const EMAIL = 'email'; |
|
28 | + |
|
29 | + const FQCN = 'fqcn'; |
|
30 | + |
|
31 | + const HTML = 'html'; |
|
32 | + |
|
33 | + const INT = 'int'; |
|
34 | + |
|
35 | + const INTEGER = 'int'; |
|
36 | + |
|
37 | + const KEY = 'key'; |
|
38 | + |
|
39 | + const OBJECT = 'object'; |
|
40 | + |
|
41 | + const NULL = 'null'; |
|
42 | + |
|
43 | + const TITLE = 'title'; |
|
44 | + |
|
45 | + const URL = 'url'; |
|
46 | + |
|
47 | + const STRING = 'string'; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * @param mixed $param |
|
52 | + * @param string $type |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public static function setDataType($param, string $type) |
|
56 | + { |
|
57 | + switch ($type) { |
|
58 | + case DataType::NULL: |
|
59 | + return null; |
|
60 | + case DataType::OBJECT: |
|
61 | + return $param; |
|
62 | + case DataType::EDITOR: |
|
63 | + case DataType::EMAIL: |
|
64 | + case DataType::FQCN: |
|
65 | + case DataType::HTML: |
|
66 | + case DataType::KEY: |
|
67 | + case DataType::STRING: |
|
68 | + case DataType::TITLE: |
|
69 | + case DataType::URL: |
|
70 | + settype($param, DataType::STRING); |
|
71 | + break; |
|
72 | + default: |
|
73 | + settype($param, $type); |
|
74 | + } |
|
75 | + return $param; |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + public static function convertModelFieldSchemaType($schema_type): ?string |
|
80 | + { |
|
81 | + if (is_array($schema_type)) { |
|
82 | + foreach ($schema_type as $type) { |
|
83 | + $data_type = self::convertModelFieldSchemaType($type); |
|
84 | + if ($data_type) { |
|
85 | + return $data_type; |
|
86 | + } |
|
87 | + } |
|
88 | + $schema_type = reset($schema_type); |
|
89 | + } |
|
90 | + switch ($schema_type) { |
|
91 | + case 'array': |
|
92 | + return DataType::ARRAY; |
|
93 | + case 'boolean': |
|
94 | + return DataType::BOOL; |
|
95 | + case 'integer': |
|
96 | + return DataType::INT; |
|
97 | + case 'null': |
|
98 | + return DataType::NULL; |
|
99 | + case 'number': |
|
100 | + return DataType::FLOAT; |
|
101 | + case 'object': |
|
102 | + return DataType::OBJECT; |
|
103 | + case 'string': |
|
104 | + return DataType::STRING; |
|
105 | + default: |
|
106 | + return null; |
|
107 | + } |
|
108 | + } |
|
109 | 109 | } |
@@ -16,544 +16,544 @@ |
||
16 | 16 | */ |
17 | 17 | class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * $_COOKIE parameters |
|
21 | - * |
|
22 | - * @var array|null |
|
23 | - */ |
|
24 | - protected ?array $cookies; |
|
25 | - |
|
26 | - /** |
|
27 | - * $_FILES parameters |
|
28 | - * |
|
29 | - * @var array|null |
|
30 | - */ |
|
31 | - protected ?array $files; |
|
32 | - |
|
33 | - /** |
|
34 | - * true if current user appears to be some kind of bot |
|
35 | - * |
|
36 | - * @var bool |
|
37 | - */ |
|
38 | - protected bool $is_bot; |
|
39 | - |
|
40 | - protected RequestParams $request_params; |
|
41 | - |
|
42 | - protected ?RequestTypeContextCheckerInterface $request_type = null; |
|
43 | - |
|
44 | - protected ServerParams $server_params; |
|
45 | - |
|
46 | - |
|
47 | - public function __construct( |
|
48 | - RequestParams $request_params, |
|
49 | - ServerParams $server_params, |
|
50 | - array $cookies = [], |
|
51 | - array $files = [] |
|
52 | - ) { |
|
53 | - $this->cookies = ! empty($cookies) |
|
54 | - ? $cookies |
|
55 | - : filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW); |
|
56 | - $this->files = ! empty($files) ? $files : $_FILES; |
|
57 | - $this->request_params = $request_params; |
|
58 | - $this->server_params = $server_params; |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * @param RequestTypeContextCheckerInterface $type |
|
64 | - */ |
|
65 | - public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
66 | - { |
|
67 | - $this->request_type = $type; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - public function getParams() |
|
75 | - { |
|
76 | - return $this->request_params->getParams(); |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function postParams() |
|
84 | - { |
|
85 | - return $this->request_params->postParams(); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - public function cookieParams() |
|
93 | - { |
|
94 | - return $this->cookies; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - public function serverParams() |
|
102 | - { |
|
103 | - return $this->server_params->getAllServerParams(); |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * @param string $key |
|
109 | - * @param mixed|null $default |
|
110 | - * @return array|int|float|string |
|
111 | - */ |
|
112 | - public function getServerParam($key, $default = null) |
|
113 | - { |
|
114 | - return $this->server_params->getServerParam($key, $default); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $key |
|
120 | - * @param array|int|float|string $value |
|
121 | - * @param bool $set_global_too |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - public function setServerParam(string $key, $value, bool $set_global_too = false) |
|
125 | - { |
|
126 | - $this->server_params->setServerParam($key, $value, $set_global_too); |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * @param string $key |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function serverParamIsSet($key) |
|
135 | - { |
|
136 | - return $this->server_params->serverParamIsSet($key); |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * @return array |
|
142 | - */ |
|
143 | - public function filesParams() |
|
144 | - { |
|
145 | - return $this->files; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * returns sanitized contents of $_REQUEST |
|
151 | - * |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - public function requestParams() |
|
155 | - { |
|
156 | - return $this->request_params->requestParams(); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $key |
|
162 | - * @param mixed|null $value |
|
163 | - * @param bool $override_ee |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function setRequestParam($key, $value, $override_ee = false) |
|
167 | - { |
|
168 | - $this->request_params->setRequestParam($key, $value, $override_ee); |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * merges the incoming array of parameters into the existing request parameters |
|
174 | - * |
|
175 | - * @param array $request_params |
|
176 | - * @return void |
|
177 | - * @since 4.10.24.p |
|
178 | - */ |
|
179 | - public function mergeRequestParams(array $request_params) |
|
180 | - { |
|
181 | - $this->request_params->mergeRequestParams($request_params); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * returns sanitized value for a request param if the given key exists |
|
187 | - * |
|
188 | - * @param string $key |
|
189 | - * @param mixed|null $default |
|
190 | - * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
191 | - * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
192 | - * @param string $delimiter for CSV type strings that should be returned as an array |
|
193 | - * @return array|bool|float|int|string |
|
194 | - */ |
|
195 | - public function getRequestParam($key, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '') |
|
196 | - { |
|
197 | - return $this->request_params->getRequestParam($key, $default, $type, $is_array, $delimiter); |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * check if param exists |
|
203 | - * |
|
204 | - * @param string $key |
|
205 | - * @return bool |
|
206 | - */ |
|
207 | - public function requestParamIsSet($key) |
|
208 | - { |
|
209 | - return $this->request_params->requestParamIsSet($key); |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
215 | - * and return the sanitized value for the first match found |
|
216 | - * wildcards can be either of the following: |
|
217 | - * ? to represent a single character of any type |
|
218 | - * * to represent one or more characters of any type |
|
219 | - * |
|
220 | - * @param string $pattern |
|
221 | - * @param mixed|null $default |
|
222 | - * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
223 | - * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
224 | - * @param string $delimiter for CSV type strings that should be returned as an array |
|
225 | - * @return array|bool|float|int|string |
|
226 | - */ |
|
227 | - public function getMatch($pattern, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '') |
|
228 | - { |
|
229 | - return $this->request_params->getMatch($pattern, $default, $type, $is_array, $delimiter); |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
235 | - * wildcards can be either of the following: |
|
236 | - * ? to represent a single character of any type |
|
237 | - * * to represent one or more characters of any type |
|
238 | - * returns true if a match is found or false if not |
|
239 | - * |
|
240 | - * @param string $pattern |
|
241 | - * @return bool |
|
242 | - */ |
|
243 | - public function matches($pattern) |
|
244 | - { |
|
245 | - return $this->request_params->matches($pattern); |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * remove param |
|
251 | - * |
|
252 | - * @param $key |
|
253 | - * @param bool $unset_from_global_too |
|
254 | - */ |
|
255 | - public function unSetRequestParam($key, $unset_from_global_too = false) |
|
256 | - { |
|
257 | - $this->request_params->unSetRequestParam($key, $unset_from_global_too); |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * remove params |
|
263 | - * |
|
264 | - * @param array $keys |
|
265 | - * @param bool $unset_from_global_too |
|
266 | - */ |
|
267 | - public function unSetRequestParams(array $keys, $unset_from_global_too = false) |
|
268 | - { |
|
269 | - $this->request_params->unSetRequestParams($keys, $unset_from_global_too); |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * @param string $key |
|
275 | - * @param bool $unset_from_global_too |
|
276 | - * @return void |
|
277 | - */ |
|
278 | - public function unSetServerParam(string $key, bool $unset_from_global_too = false) |
|
279 | - { |
|
280 | - $this->server_params->unSetServerParam($key, $unset_from_global_too); |
|
281 | - } |
|
282 | - |
|
283 | - |
|
284 | - /** |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - public function ipAddress() |
|
288 | - { |
|
289 | - return $this->server_params->ipAddress(); |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Gets the request's literal URI. Related to `requestUriAfterSiteHomeUri`, see its description for a comparison. |
|
295 | - * |
|
296 | - * @param boolean $relativeToWpRoot If home_url() is "http://mysite.com/wp/", and a request comes to |
|
297 | - * "http://mysite.com/wp/wp-json", setting $relativeToWpRoot=true will return |
|
298 | - * "/wp-json", whereas $relativeToWpRoot=false will return "/wp/wp-json/". |
|
299 | - * @param boolean $remove_query_params whether to return the uri with all query params removed. |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function requestUri($relativeToWpRoot = false, $remove_query_params = false) |
|
303 | - { |
|
304 | - return $this->server_params->requestUri($relativeToWpRoot); |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * @return string |
|
310 | - */ |
|
311 | - public function userAgent() |
|
312 | - { |
|
313 | - return $this->server_params->userAgent(); |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * @param string $user_agent |
|
319 | - */ |
|
320 | - public function setUserAgent($user_agent = '') |
|
321 | - { |
|
322 | - $this->server_params->setUserAgent($user_agent); |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * @return bool |
|
328 | - */ |
|
329 | - public function isBot() |
|
330 | - { |
|
331 | - return $this->is_bot; |
|
332 | - } |
|
333 | - |
|
334 | - |
|
335 | - /** |
|
336 | - * @param bool $is_bot |
|
337 | - */ |
|
338 | - public function setIsBot($is_bot) |
|
339 | - { |
|
340 | - $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - public function isActivation() |
|
348 | - { |
|
349 | - return $this->request_type->isActivation(); |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * @param $is_activation |
|
355 | - * @return bool |
|
356 | - */ |
|
357 | - public function setIsActivation($is_activation) |
|
358 | - { |
|
359 | - return $this->request_type->setIsActivation($is_activation); |
|
360 | - } |
|
361 | - |
|
362 | - |
|
363 | - /** |
|
364 | - * @return bool |
|
365 | - */ |
|
366 | - public function isAdmin() |
|
367 | - { |
|
368 | - return $this->request_type->isAdmin(); |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * @return bool |
|
374 | - */ |
|
375 | - public function isAdminAjax() |
|
376 | - { |
|
377 | - return $this->request_type->isAdminAjax(); |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * @return bool |
|
383 | - */ |
|
384 | - public function isAjax() |
|
385 | - { |
|
386 | - return $this->request_type->isAjax(); |
|
387 | - } |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * @return bool |
|
392 | - */ |
|
393 | - public function isEeAjax() |
|
394 | - { |
|
395 | - return $this->request_type->isEeAjax(); |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - /** |
|
400 | - * @return bool |
|
401 | - */ |
|
402 | - public function isOtherAjax() |
|
403 | - { |
|
404 | - return $this->request_type->isOtherAjax(); |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * @return bool |
|
410 | - */ |
|
411 | - public function isApi() |
|
412 | - { |
|
413 | - return $this->request_type->isApi(); |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * @return bool |
|
419 | - */ |
|
420 | - public function isCli() |
|
421 | - { |
|
422 | - return $this->request_type->isCli(); |
|
423 | - } |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * @return bool |
|
428 | - */ |
|
429 | - public function isCron() |
|
430 | - { |
|
431 | - return $this->request_type->isCron(); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * @return bool |
|
437 | - */ |
|
438 | - public function isFeed() |
|
439 | - { |
|
440 | - return $this->request_type->isFeed(); |
|
441 | - } |
|
442 | - |
|
443 | - |
|
444 | - /** |
|
445 | - * @return bool |
|
446 | - */ |
|
447 | - public function isFrontend() |
|
448 | - { |
|
449 | - return $this->request_type->isFrontend(); |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * @return bool |
|
455 | - */ |
|
456 | - public function isFrontAjax() |
|
457 | - { |
|
458 | - return $this->request_type->isFrontAjax(); |
|
459 | - } |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * @return bool |
|
464 | - */ |
|
465 | - public function isGQL() |
|
466 | - { |
|
467 | - return $this->request_type->isGQL(); |
|
468 | - } |
|
469 | - |
|
470 | - |
|
471 | - /** |
|
472 | - * @return bool |
|
473 | - */ |
|
474 | - public function isIframe() |
|
475 | - { |
|
476 | - return $this->request_type->isIframe(); |
|
477 | - } |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * @return bool |
|
482 | - */ |
|
483 | - public function isUnitTesting() |
|
484 | - { |
|
485 | - return $this->request_type->isUnitTesting(); |
|
486 | - } |
|
487 | - |
|
488 | - |
|
489 | - /** |
|
490 | - * @return bool |
|
491 | - */ |
|
492 | - public function isWordPressApi() |
|
493 | - { |
|
494 | - return $this->request_type->isWordPressApi(); |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * @return bool |
|
500 | - */ |
|
501 | - public function isWordPressHeartbeat() |
|
502 | - { |
|
503 | - return $this->request_type->isWordPressHeartbeat(); |
|
504 | - } |
|
505 | - |
|
506 | - |
|
507 | - /** |
|
508 | - * @return bool |
|
509 | - */ |
|
510 | - public function isWordPressScrape() |
|
511 | - { |
|
512 | - return $this->request_type->isWordPressScrape(); |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - /** |
|
517 | - * @return string |
|
518 | - */ |
|
519 | - public function slug() |
|
520 | - { |
|
521 | - return $this->request_type->slug(); |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * returns the path portion of the current request URI with both the WP Root (home_url()) and query params removed |
|
527 | - * |
|
528 | - * @return string |
|
529 | - * @since 5.0.0.p |
|
530 | - */ |
|
531 | - public function requestPath() |
|
532 | - { |
|
533 | - return $this->requestUri(true, true); |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * returns true if the last segment of the current request path (without params) matches the provided string |
|
539 | - * |
|
540 | - * @param string $uri_segment |
|
541 | - * @return bool |
|
542 | - * @since 5.0.0.p |
|
543 | - */ |
|
544 | - public function currentPageIs($uri_segment) |
|
545 | - { |
|
546 | - $request_path = $this->requestPath(); |
|
547 | - $current_page = explode('/', $request_path); |
|
548 | - return end($current_page) === $uri_segment; |
|
549 | - } |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * @return RequestTypeContextCheckerInterface |
|
554 | - */ |
|
555 | - public function getRequestType(): RequestTypeContextCheckerInterface |
|
556 | - { |
|
557 | - return $this->request_type; |
|
558 | - } |
|
19 | + /** |
|
20 | + * $_COOKIE parameters |
|
21 | + * |
|
22 | + * @var array|null |
|
23 | + */ |
|
24 | + protected ?array $cookies; |
|
25 | + |
|
26 | + /** |
|
27 | + * $_FILES parameters |
|
28 | + * |
|
29 | + * @var array|null |
|
30 | + */ |
|
31 | + protected ?array $files; |
|
32 | + |
|
33 | + /** |
|
34 | + * true if current user appears to be some kind of bot |
|
35 | + * |
|
36 | + * @var bool |
|
37 | + */ |
|
38 | + protected bool $is_bot; |
|
39 | + |
|
40 | + protected RequestParams $request_params; |
|
41 | + |
|
42 | + protected ?RequestTypeContextCheckerInterface $request_type = null; |
|
43 | + |
|
44 | + protected ServerParams $server_params; |
|
45 | + |
|
46 | + |
|
47 | + public function __construct( |
|
48 | + RequestParams $request_params, |
|
49 | + ServerParams $server_params, |
|
50 | + array $cookies = [], |
|
51 | + array $files = [] |
|
52 | + ) { |
|
53 | + $this->cookies = ! empty($cookies) |
|
54 | + ? $cookies |
|
55 | + : filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW); |
|
56 | + $this->files = ! empty($files) ? $files : $_FILES; |
|
57 | + $this->request_params = $request_params; |
|
58 | + $this->server_params = $server_params; |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * @param RequestTypeContextCheckerInterface $type |
|
64 | + */ |
|
65 | + public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
66 | + { |
|
67 | + $this->request_type = $type; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + public function getParams() |
|
75 | + { |
|
76 | + return $this->request_params->getParams(); |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function postParams() |
|
84 | + { |
|
85 | + return $this->request_params->postParams(); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + public function cookieParams() |
|
93 | + { |
|
94 | + return $this->cookies; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + public function serverParams() |
|
102 | + { |
|
103 | + return $this->server_params->getAllServerParams(); |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * @param string $key |
|
109 | + * @param mixed|null $default |
|
110 | + * @return array|int|float|string |
|
111 | + */ |
|
112 | + public function getServerParam($key, $default = null) |
|
113 | + { |
|
114 | + return $this->server_params->getServerParam($key, $default); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $key |
|
120 | + * @param array|int|float|string $value |
|
121 | + * @param bool $set_global_too |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + public function setServerParam(string $key, $value, bool $set_global_too = false) |
|
125 | + { |
|
126 | + $this->server_params->setServerParam($key, $value, $set_global_too); |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * @param string $key |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function serverParamIsSet($key) |
|
135 | + { |
|
136 | + return $this->server_params->serverParamIsSet($key); |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * @return array |
|
142 | + */ |
|
143 | + public function filesParams() |
|
144 | + { |
|
145 | + return $this->files; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * returns sanitized contents of $_REQUEST |
|
151 | + * |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + public function requestParams() |
|
155 | + { |
|
156 | + return $this->request_params->requestParams(); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $key |
|
162 | + * @param mixed|null $value |
|
163 | + * @param bool $override_ee |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function setRequestParam($key, $value, $override_ee = false) |
|
167 | + { |
|
168 | + $this->request_params->setRequestParam($key, $value, $override_ee); |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * merges the incoming array of parameters into the existing request parameters |
|
174 | + * |
|
175 | + * @param array $request_params |
|
176 | + * @return void |
|
177 | + * @since 4.10.24.p |
|
178 | + */ |
|
179 | + public function mergeRequestParams(array $request_params) |
|
180 | + { |
|
181 | + $this->request_params->mergeRequestParams($request_params); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * returns sanitized value for a request param if the given key exists |
|
187 | + * |
|
188 | + * @param string $key |
|
189 | + * @param mixed|null $default |
|
190 | + * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
191 | + * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
192 | + * @param string $delimiter for CSV type strings that should be returned as an array |
|
193 | + * @return array|bool|float|int|string |
|
194 | + */ |
|
195 | + public function getRequestParam($key, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '') |
|
196 | + { |
|
197 | + return $this->request_params->getRequestParam($key, $default, $type, $is_array, $delimiter); |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * check if param exists |
|
203 | + * |
|
204 | + * @param string $key |
|
205 | + * @return bool |
|
206 | + */ |
|
207 | + public function requestParamIsSet($key) |
|
208 | + { |
|
209 | + return $this->request_params->requestParamIsSet($key); |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
215 | + * and return the sanitized value for the first match found |
|
216 | + * wildcards can be either of the following: |
|
217 | + * ? to represent a single character of any type |
|
218 | + * * to represent one or more characters of any type |
|
219 | + * |
|
220 | + * @param string $pattern |
|
221 | + * @param mixed|null $default |
|
222 | + * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
223 | + * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
224 | + * @param string $delimiter for CSV type strings that should be returned as an array |
|
225 | + * @return array|bool|float|int|string |
|
226 | + */ |
|
227 | + public function getMatch($pattern, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '') |
|
228 | + { |
|
229 | + return $this->request_params->getMatch($pattern, $default, $type, $is_array, $delimiter); |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
235 | + * wildcards can be either of the following: |
|
236 | + * ? to represent a single character of any type |
|
237 | + * * to represent one or more characters of any type |
|
238 | + * returns true if a match is found or false if not |
|
239 | + * |
|
240 | + * @param string $pattern |
|
241 | + * @return bool |
|
242 | + */ |
|
243 | + public function matches($pattern) |
|
244 | + { |
|
245 | + return $this->request_params->matches($pattern); |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * remove param |
|
251 | + * |
|
252 | + * @param $key |
|
253 | + * @param bool $unset_from_global_too |
|
254 | + */ |
|
255 | + public function unSetRequestParam($key, $unset_from_global_too = false) |
|
256 | + { |
|
257 | + $this->request_params->unSetRequestParam($key, $unset_from_global_too); |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * remove params |
|
263 | + * |
|
264 | + * @param array $keys |
|
265 | + * @param bool $unset_from_global_too |
|
266 | + */ |
|
267 | + public function unSetRequestParams(array $keys, $unset_from_global_too = false) |
|
268 | + { |
|
269 | + $this->request_params->unSetRequestParams($keys, $unset_from_global_too); |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * @param string $key |
|
275 | + * @param bool $unset_from_global_too |
|
276 | + * @return void |
|
277 | + */ |
|
278 | + public function unSetServerParam(string $key, bool $unset_from_global_too = false) |
|
279 | + { |
|
280 | + $this->server_params->unSetServerParam($key, $unset_from_global_too); |
|
281 | + } |
|
282 | + |
|
283 | + |
|
284 | + /** |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + public function ipAddress() |
|
288 | + { |
|
289 | + return $this->server_params->ipAddress(); |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Gets the request's literal URI. Related to `requestUriAfterSiteHomeUri`, see its description for a comparison. |
|
295 | + * |
|
296 | + * @param boolean $relativeToWpRoot If home_url() is "http://mysite.com/wp/", and a request comes to |
|
297 | + * "http://mysite.com/wp/wp-json", setting $relativeToWpRoot=true will return |
|
298 | + * "/wp-json", whereas $relativeToWpRoot=false will return "/wp/wp-json/". |
|
299 | + * @param boolean $remove_query_params whether to return the uri with all query params removed. |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function requestUri($relativeToWpRoot = false, $remove_query_params = false) |
|
303 | + { |
|
304 | + return $this->server_params->requestUri($relativeToWpRoot); |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * @return string |
|
310 | + */ |
|
311 | + public function userAgent() |
|
312 | + { |
|
313 | + return $this->server_params->userAgent(); |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * @param string $user_agent |
|
319 | + */ |
|
320 | + public function setUserAgent($user_agent = '') |
|
321 | + { |
|
322 | + $this->server_params->setUserAgent($user_agent); |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * @return bool |
|
328 | + */ |
|
329 | + public function isBot() |
|
330 | + { |
|
331 | + return $this->is_bot; |
|
332 | + } |
|
333 | + |
|
334 | + |
|
335 | + /** |
|
336 | + * @param bool $is_bot |
|
337 | + */ |
|
338 | + public function setIsBot($is_bot) |
|
339 | + { |
|
340 | + $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + public function isActivation() |
|
348 | + { |
|
349 | + return $this->request_type->isActivation(); |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * @param $is_activation |
|
355 | + * @return bool |
|
356 | + */ |
|
357 | + public function setIsActivation($is_activation) |
|
358 | + { |
|
359 | + return $this->request_type->setIsActivation($is_activation); |
|
360 | + } |
|
361 | + |
|
362 | + |
|
363 | + /** |
|
364 | + * @return bool |
|
365 | + */ |
|
366 | + public function isAdmin() |
|
367 | + { |
|
368 | + return $this->request_type->isAdmin(); |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * @return bool |
|
374 | + */ |
|
375 | + public function isAdminAjax() |
|
376 | + { |
|
377 | + return $this->request_type->isAdminAjax(); |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * @return bool |
|
383 | + */ |
|
384 | + public function isAjax() |
|
385 | + { |
|
386 | + return $this->request_type->isAjax(); |
|
387 | + } |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * @return bool |
|
392 | + */ |
|
393 | + public function isEeAjax() |
|
394 | + { |
|
395 | + return $this->request_type->isEeAjax(); |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + /** |
|
400 | + * @return bool |
|
401 | + */ |
|
402 | + public function isOtherAjax() |
|
403 | + { |
|
404 | + return $this->request_type->isOtherAjax(); |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * @return bool |
|
410 | + */ |
|
411 | + public function isApi() |
|
412 | + { |
|
413 | + return $this->request_type->isApi(); |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * @return bool |
|
419 | + */ |
|
420 | + public function isCli() |
|
421 | + { |
|
422 | + return $this->request_type->isCli(); |
|
423 | + } |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * @return bool |
|
428 | + */ |
|
429 | + public function isCron() |
|
430 | + { |
|
431 | + return $this->request_type->isCron(); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * @return bool |
|
437 | + */ |
|
438 | + public function isFeed() |
|
439 | + { |
|
440 | + return $this->request_type->isFeed(); |
|
441 | + } |
|
442 | + |
|
443 | + |
|
444 | + /** |
|
445 | + * @return bool |
|
446 | + */ |
|
447 | + public function isFrontend() |
|
448 | + { |
|
449 | + return $this->request_type->isFrontend(); |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * @return bool |
|
455 | + */ |
|
456 | + public function isFrontAjax() |
|
457 | + { |
|
458 | + return $this->request_type->isFrontAjax(); |
|
459 | + } |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * @return bool |
|
464 | + */ |
|
465 | + public function isGQL() |
|
466 | + { |
|
467 | + return $this->request_type->isGQL(); |
|
468 | + } |
|
469 | + |
|
470 | + |
|
471 | + /** |
|
472 | + * @return bool |
|
473 | + */ |
|
474 | + public function isIframe() |
|
475 | + { |
|
476 | + return $this->request_type->isIframe(); |
|
477 | + } |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * @return bool |
|
482 | + */ |
|
483 | + public function isUnitTesting() |
|
484 | + { |
|
485 | + return $this->request_type->isUnitTesting(); |
|
486 | + } |
|
487 | + |
|
488 | + |
|
489 | + /** |
|
490 | + * @return bool |
|
491 | + */ |
|
492 | + public function isWordPressApi() |
|
493 | + { |
|
494 | + return $this->request_type->isWordPressApi(); |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * @return bool |
|
500 | + */ |
|
501 | + public function isWordPressHeartbeat() |
|
502 | + { |
|
503 | + return $this->request_type->isWordPressHeartbeat(); |
|
504 | + } |
|
505 | + |
|
506 | + |
|
507 | + /** |
|
508 | + * @return bool |
|
509 | + */ |
|
510 | + public function isWordPressScrape() |
|
511 | + { |
|
512 | + return $this->request_type->isWordPressScrape(); |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + /** |
|
517 | + * @return string |
|
518 | + */ |
|
519 | + public function slug() |
|
520 | + { |
|
521 | + return $this->request_type->slug(); |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * returns the path portion of the current request URI with both the WP Root (home_url()) and query params removed |
|
527 | + * |
|
528 | + * @return string |
|
529 | + * @since 5.0.0.p |
|
530 | + */ |
|
531 | + public function requestPath() |
|
532 | + { |
|
533 | + return $this->requestUri(true, true); |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * returns true if the last segment of the current request path (without params) matches the provided string |
|
539 | + * |
|
540 | + * @param string $uri_segment |
|
541 | + * @return bool |
|
542 | + * @since 5.0.0.p |
|
543 | + */ |
|
544 | + public function currentPageIs($uri_segment) |
|
545 | + { |
|
546 | + $request_path = $this->requestPath(); |
|
547 | + $current_page = explode('/', $request_path); |
|
548 | + return end($current_page) === $uri_segment; |
|
549 | + } |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * @return RequestTypeContextCheckerInterface |
|
554 | + */ |
|
555 | + public function getRequestType(): RequestTypeContextCheckerInterface |
|
556 | + { |
|
557 | + return $this->request_type; |
|
558 | + } |
|
559 | 559 | } |
@@ -14,119 +14,119 @@ |
||
14 | 14 | */ |
15 | 15 | class Loader implements LoaderInterface |
16 | 16 | { |
17 | - private LoaderDecoratorInterface $new_loader; |
|
18 | - |
|
19 | - private LoaderDecoratorInterface $shared_loader; |
|
20 | - |
|
21 | - private ClassInterfaceCache $class_cache; |
|
22 | - |
|
23 | - /** |
|
24 | - * Loader constructor. |
|
25 | - * |
|
26 | - * @param LoaderDecoratorInterface $new_loader |
|
27 | - * @param CachingLoaderDecoratorInterface $shared_loader |
|
28 | - * @param ClassInterfaceCache $class_cache |
|
29 | - */ |
|
30 | - public function __construct( |
|
31 | - LoaderDecoratorInterface $new_loader, |
|
32 | - CachingLoaderDecoratorInterface $shared_loader, |
|
33 | - ClassInterfaceCache $class_cache |
|
34 | - ) { |
|
35 | - $this->new_loader = $new_loader; |
|
36 | - $this->shared_loader = $shared_loader; |
|
37 | - $this->class_cache = $class_cache; |
|
38 | - } |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * @return LoaderDecoratorInterface |
|
43 | - */ |
|
44 | - public function getNewLoader() |
|
45 | - { |
|
46 | - return $this->new_loader; |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * @return CachingLoaderDecoratorInterface |
|
52 | - */ |
|
53 | - public function getSharedLoader() |
|
54 | - { |
|
55 | - return $this->shared_loader; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @param FullyQualifiedName|string $fqcn |
|
61 | - * @param array $arguments |
|
62 | - * @param bool $shared |
|
63 | - * @return mixed |
|
64 | - */ |
|
65 | - public function load($fqcn, array $arguments = array(), $shared = true) |
|
66 | - { |
|
67 | - $fqcn = $this->class_cache->getFqn($fqcn); |
|
68 | - if ($this->class_cache->hasInterface($fqcn, 'EventEspresso\core\interfaces\ReservedInstanceInterface')) { |
|
69 | - $shared = true; |
|
70 | - } |
|
71 | - return $shared |
|
72 | - ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
73 | - : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @param FullyQualifiedName|string $fqcn |
|
79 | - * @param array $arguments |
|
80 | - * @return mixed |
|
81 | - */ |
|
82 | - public function getNew($fqcn, array $arguments = array()) |
|
83 | - { |
|
84 | - return $this->load($fqcn, $arguments, false); |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * @param FullyQualifiedName|string $fqcn |
|
90 | - * @param array $arguments |
|
91 | - * @return mixed |
|
92 | - */ |
|
93 | - public function getShared($fqcn, array $arguments = array()) |
|
94 | - { |
|
95 | - return $this->load($fqcn, $arguments); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * @param FullyQualifiedName|string $fqcn |
|
101 | - * @param mixed $object |
|
102 | - * @param array $arguments |
|
103 | - * @return bool |
|
104 | - * @throws InvalidArgumentException |
|
105 | - */ |
|
106 | - public function share($fqcn, $object, array $arguments = []) |
|
107 | - { |
|
108 | - $fqcn = $this->class_cache->getFqn($fqcn); |
|
109 | - return $this->getSharedLoader()->share($fqcn, $object, $arguments); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @param FullyQualifiedName|string $fqcn |
|
115 | - * @return bool |
|
116 | - * @throws InvalidArgumentException |
|
117 | - */ |
|
118 | - public function remove($fqcn, array $arguments = []) |
|
119 | - { |
|
120 | - $fqcn = $this->class_cache->getFqn($fqcn); |
|
121 | - return $this->getSharedLoader()->remove($fqcn, $arguments); |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - /** |
|
126 | - * calls reset() on loaders if that method exists |
|
127 | - */ |
|
128 | - public function reset() |
|
129 | - { |
|
130 | - $this->shared_loader->reset(); |
|
131 | - } |
|
17 | + private LoaderDecoratorInterface $new_loader; |
|
18 | + |
|
19 | + private LoaderDecoratorInterface $shared_loader; |
|
20 | + |
|
21 | + private ClassInterfaceCache $class_cache; |
|
22 | + |
|
23 | + /** |
|
24 | + * Loader constructor. |
|
25 | + * |
|
26 | + * @param LoaderDecoratorInterface $new_loader |
|
27 | + * @param CachingLoaderDecoratorInterface $shared_loader |
|
28 | + * @param ClassInterfaceCache $class_cache |
|
29 | + */ |
|
30 | + public function __construct( |
|
31 | + LoaderDecoratorInterface $new_loader, |
|
32 | + CachingLoaderDecoratorInterface $shared_loader, |
|
33 | + ClassInterfaceCache $class_cache |
|
34 | + ) { |
|
35 | + $this->new_loader = $new_loader; |
|
36 | + $this->shared_loader = $shared_loader; |
|
37 | + $this->class_cache = $class_cache; |
|
38 | + } |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * @return LoaderDecoratorInterface |
|
43 | + */ |
|
44 | + public function getNewLoader() |
|
45 | + { |
|
46 | + return $this->new_loader; |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * @return CachingLoaderDecoratorInterface |
|
52 | + */ |
|
53 | + public function getSharedLoader() |
|
54 | + { |
|
55 | + return $this->shared_loader; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @param FullyQualifiedName|string $fqcn |
|
61 | + * @param array $arguments |
|
62 | + * @param bool $shared |
|
63 | + * @return mixed |
|
64 | + */ |
|
65 | + public function load($fqcn, array $arguments = array(), $shared = true) |
|
66 | + { |
|
67 | + $fqcn = $this->class_cache->getFqn($fqcn); |
|
68 | + if ($this->class_cache->hasInterface($fqcn, 'EventEspresso\core\interfaces\ReservedInstanceInterface')) { |
|
69 | + $shared = true; |
|
70 | + } |
|
71 | + return $shared |
|
72 | + ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
73 | + : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @param FullyQualifiedName|string $fqcn |
|
79 | + * @param array $arguments |
|
80 | + * @return mixed |
|
81 | + */ |
|
82 | + public function getNew($fqcn, array $arguments = array()) |
|
83 | + { |
|
84 | + return $this->load($fqcn, $arguments, false); |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * @param FullyQualifiedName|string $fqcn |
|
90 | + * @param array $arguments |
|
91 | + * @return mixed |
|
92 | + */ |
|
93 | + public function getShared($fqcn, array $arguments = array()) |
|
94 | + { |
|
95 | + return $this->load($fqcn, $arguments); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * @param FullyQualifiedName|string $fqcn |
|
101 | + * @param mixed $object |
|
102 | + * @param array $arguments |
|
103 | + * @return bool |
|
104 | + * @throws InvalidArgumentException |
|
105 | + */ |
|
106 | + public function share($fqcn, $object, array $arguments = []) |
|
107 | + { |
|
108 | + $fqcn = $this->class_cache->getFqn($fqcn); |
|
109 | + return $this->getSharedLoader()->share($fqcn, $object, $arguments); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @param FullyQualifiedName|string $fqcn |
|
115 | + * @return bool |
|
116 | + * @throws InvalidArgumentException |
|
117 | + */ |
|
118 | + public function remove($fqcn, array $arguments = []) |
|
119 | + { |
|
120 | + $fqcn = $this->class_cache->getFqn($fqcn); |
|
121 | + return $this->getSharedLoader()->remove($fqcn, $arguments); |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + /** |
|
126 | + * calls reset() on loaders if that method exists |
|
127 | + */ |
|
128 | + public function reset() |
|
129 | + { |
|
130 | + $this->shared_loader->reset(); |
|
131 | + } |
|
132 | 132 | } |
@@ -16,191 +16,191 @@ |
||
16 | 16 | */ |
17 | 17 | class CachingLoader extends CachingLoaderDecorator |
18 | 18 | { |
19 | - protected bool $bypass = false; |
|
20 | - |
|
21 | - protected CollectionInterface $cache; |
|
22 | - |
|
23 | - protected string $identifier; |
|
24 | - |
|
25 | - private ObjectIdentifier $object_identifier; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * CachingLoader constructor. |
|
30 | - * |
|
31 | - * @param LoaderDecoratorInterface $loader |
|
32 | - * @param CollectionInterface $cache |
|
33 | - * @param ObjectIdentifier $object_identifier |
|
34 | - * @param string $identifier |
|
35 | - * @throws InvalidDataTypeException |
|
36 | - */ |
|
37 | - public function __construct( |
|
38 | - LoaderDecoratorInterface $loader, |
|
39 | - CollectionInterface $cache, |
|
40 | - ObjectIdentifier $object_identifier, |
|
41 | - string $identifier = '' |
|
42 | - ) { |
|
43 | - parent::__construct($loader); |
|
44 | - $this->cache = $cache; |
|
45 | - $this->object_identifier = $object_identifier; |
|
46 | - $this->setIdentifier($identifier); |
|
47 | - if ($this->identifier !== '') { |
|
48 | - // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
49 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
50 | - // where "IDENTIFIER" = the string that was set during construction |
|
51 | - add_action( |
|
52 | - "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__$identifier", |
|
53 | - [$this, 'reset'] |
|
54 | - ); |
|
55 | - } |
|
56 | - // to clear ALL caches, simply do the following: |
|
57 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
58 | - add_action( |
|
59 | - 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
60 | - [$this, 'reset'] |
|
61 | - ); |
|
62 | - // caching can be turned off via the following code: |
|
63 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
64 | - $this->bypass = filter_var( |
|
65 | - apply_filters( |
|
66 | - 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
67 | - false, |
|
68 | - $this |
|
69 | - ), |
|
70 | - FILTER_VALIDATE_BOOLEAN |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function identifier(): string |
|
79 | - { |
|
80 | - return $this->identifier; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string|null $identifier |
|
86 | - * @throws InvalidDataTypeException |
|
87 | - */ |
|
88 | - private function setIdentifier(?string $identifier) |
|
89 | - { |
|
90 | - if (! is_string($identifier)) { |
|
91 | - throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
92 | - } |
|
93 | - $this->identifier = $identifier; |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * @param FullyQualifiedName|string $fqcn |
|
99 | - * @param mixed $object |
|
100 | - * @param array $arguments |
|
101 | - * @return bool |
|
102 | - * @throws InvalidArgumentException |
|
103 | - */ |
|
104 | - public function share($fqcn, $object, array $arguments = []): bool |
|
105 | - { |
|
106 | - if ($object instanceof $fqcn) { |
|
107 | - $added = false; |
|
108 | - if ($arguments) { |
|
109 | - // first cache the object using the identifier that includes the arguments |
|
110 | - $added = $this->cache->add($object, $this->object_identifier->getIdentifier($fqcn, $arguments)); |
|
111 | - } |
|
112 | - // then again using the identifier without the arguments |
|
113 | - // WHY? because most requests for the object will not include arguments |
|
114 | - $object_identifier = $this->object_identifier->getIdentifier($fqcn); |
|
115 | - // but only if the object is not already in the cache |
|
116 | - if (! $this->cache->has($object_identifier)) { |
|
117 | - $added = $this->cache->add($object, $object_identifier) |
|
118 | - ? true |
|
119 | - : $added; |
|
120 | - } |
|
121 | - return $added; |
|
122 | - } |
|
123 | - throw new InvalidArgumentException( |
|
124 | - sprintf( |
|
125 | - esc_html__( |
|
126 | - 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
127 | - 'event_espresso' |
|
128 | - ), |
|
129 | - $fqcn |
|
130 | - ) |
|
131 | - ); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * @param FullyQualifiedName|string $fqcn |
|
137 | - * @param array $arguments |
|
138 | - * @param bool $shared |
|
139 | - * @param array $interfaces |
|
140 | - * @return mixed |
|
141 | - */ |
|
142 | - public function load($fqcn, $arguments = [], $shared = true, array $interfaces = []) |
|
143 | - { |
|
144 | - $fqcn = ltrim($fqcn, '\\'); |
|
145 | - // caching can be turned off via the following code: |
|
146 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
147 | - if ($this->bypass) { |
|
148 | - // even though $shared might be true, caching could be bypassed for whatever reason, |
|
149 | - // so we don't want the core loader to cache anything, therefore caching is turned off |
|
150 | - return $this->loader->load($fqcn, $arguments, false); |
|
151 | - } |
|
152 | - $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
153 | - if ($this->cache->has($object_identifier)) { |
|
154 | - return $this->cache->get($object_identifier); |
|
155 | - } |
|
156 | - $object = $this->loader->load($fqcn, $arguments, $shared); |
|
157 | - if ($object instanceof $fqcn) { |
|
158 | - $this->cache->add($object, $object_identifier); |
|
159 | - } |
|
160 | - return $object; |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * empties cache and calls reset() on loader if method exists |
|
166 | - */ |
|
167 | - public function reset() |
|
168 | - { |
|
169 | - $this->clearCache(); |
|
170 | - $this->loader->reset(); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * unsets and detaches ALL objects from the cache |
|
176 | - * |
|
177 | - * @since 4.9.62.p |
|
178 | - */ |
|
179 | - public function clearCache() |
|
180 | - { |
|
181 | - $cache_class = get_class($this->cache); |
|
182 | - $collection_interface = $this->cache->collectionInterface(); |
|
183 | - $this->cache->trashAndDetachAll(); |
|
184 | - $this->cache = new $cache_class($collection_interface); |
|
185 | - if (! $this->cache instanceof CollectionInterface) { |
|
186 | - throw new InvalidDataTypeException('CachingLoader::$cache', $this->cache, 'CollectionInterface'); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * @param string $fqcn |
|
193 | - * @param array $arguments |
|
194 | - * @return bool |
|
195 | - * @throws InvalidArgumentException |
|
196 | - */ |
|
197 | - public function remove($fqcn, array $arguments = []): bool |
|
198 | - { |
|
199 | - $fqcn = ltrim($fqcn, '\\'); |
|
200 | - $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
201 | - $object = $this->cache->has($object_identifier) |
|
202 | - ? $this->cache->get($object_identifier) |
|
203 | - : $this->loader->load($fqcn, $arguments); |
|
204 | - return $this->cache->remove($object); |
|
205 | - } |
|
19 | + protected bool $bypass = false; |
|
20 | + |
|
21 | + protected CollectionInterface $cache; |
|
22 | + |
|
23 | + protected string $identifier; |
|
24 | + |
|
25 | + private ObjectIdentifier $object_identifier; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * CachingLoader constructor. |
|
30 | + * |
|
31 | + * @param LoaderDecoratorInterface $loader |
|
32 | + * @param CollectionInterface $cache |
|
33 | + * @param ObjectIdentifier $object_identifier |
|
34 | + * @param string $identifier |
|
35 | + * @throws InvalidDataTypeException |
|
36 | + */ |
|
37 | + public function __construct( |
|
38 | + LoaderDecoratorInterface $loader, |
|
39 | + CollectionInterface $cache, |
|
40 | + ObjectIdentifier $object_identifier, |
|
41 | + string $identifier = '' |
|
42 | + ) { |
|
43 | + parent::__construct($loader); |
|
44 | + $this->cache = $cache; |
|
45 | + $this->object_identifier = $object_identifier; |
|
46 | + $this->setIdentifier($identifier); |
|
47 | + if ($this->identifier !== '') { |
|
48 | + // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
49 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
50 | + // where "IDENTIFIER" = the string that was set during construction |
|
51 | + add_action( |
|
52 | + "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__$identifier", |
|
53 | + [$this, 'reset'] |
|
54 | + ); |
|
55 | + } |
|
56 | + // to clear ALL caches, simply do the following: |
|
57 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
58 | + add_action( |
|
59 | + 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
60 | + [$this, 'reset'] |
|
61 | + ); |
|
62 | + // caching can be turned off via the following code: |
|
63 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
64 | + $this->bypass = filter_var( |
|
65 | + apply_filters( |
|
66 | + 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
67 | + false, |
|
68 | + $this |
|
69 | + ), |
|
70 | + FILTER_VALIDATE_BOOLEAN |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function identifier(): string |
|
79 | + { |
|
80 | + return $this->identifier; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string|null $identifier |
|
86 | + * @throws InvalidDataTypeException |
|
87 | + */ |
|
88 | + private function setIdentifier(?string $identifier) |
|
89 | + { |
|
90 | + if (! is_string($identifier)) { |
|
91 | + throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
92 | + } |
|
93 | + $this->identifier = $identifier; |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * @param FullyQualifiedName|string $fqcn |
|
99 | + * @param mixed $object |
|
100 | + * @param array $arguments |
|
101 | + * @return bool |
|
102 | + * @throws InvalidArgumentException |
|
103 | + */ |
|
104 | + public function share($fqcn, $object, array $arguments = []): bool |
|
105 | + { |
|
106 | + if ($object instanceof $fqcn) { |
|
107 | + $added = false; |
|
108 | + if ($arguments) { |
|
109 | + // first cache the object using the identifier that includes the arguments |
|
110 | + $added = $this->cache->add($object, $this->object_identifier->getIdentifier($fqcn, $arguments)); |
|
111 | + } |
|
112 | + // then again using the identifier without the arguments |
|
113 | + // WHY? because most requests for the object will not include arguments |
|
114 | + $object_identifier = $this->object_identifier->getIdentifier($fqcn); |
|
115 | + // but only if the object is not already in the cache |
|
116 | + if (! $this->cache->has($object_identifier)) { |
|
117 | + $added = $this->cache->add($object, $object_identifier) |
|
118 | + ? true |
|
119 | + : $added; |
|
120 | + } |
|
121 | + return $added; |
|
122 | + } |
|
123 | + throw new InvalidArgumentException( |
|
124 | + sprintf( |
|
125 | + esc_html__( |
|
126 | + 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
127 | + 'event_espresso' |
|
128 | + ), |
|
129 | + $fqcn |
|
130 | + ) |
|
131 | + ); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * @param FullyQualifiedName|string $fqcn |
|
137 | + * @param array $arguments |
|
138 | + * @param bool $shared |
|
139 | + * @param array $interfaces |
|
140 | + * @return mixed |
|
141 | + */ |
|
142 | + public function load($fqcn, $arguments = [], $shared = true, array $interfaces = []) |
|
143 | + { |
|
144 | + $fqcn = ltrim($fqcn, '\\'); |
|
145 | + // caching can be turned off via the following code: |
|
146 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
147 | + if ($this->bypass) { |
|
148 | + // even though $shared might be true, caching could be bypassed for whatever reason, |
|
149 | + // so we don't want the core loader to cache anything, therefore caching is turned off |
|
150 | + return $this->loader->load($fqcn, $arguments, false); |
|
151 | + } |
|
152 | + $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
153 | + if ($this->cache->has($object_identifier)) { |
|
154 | + return $this->cache->get($object_identifier); |
|
155 | + } |
|
156 | + $object = $this->loader->load($fqcn, $arguments, $shared); |
|
157 | + if ($object instanceof $fqcn) { |
|
158 | + $this->cache->add($object, $object_identifier); |
|
159 | + } |
|
160 | + return $object; |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * empties cache and calls reset() on loader if method exists |
|
166 | + */ |
|
167 | + public function reset() |
|
168 | + { |
|
169 | + $this->clearCache(); |
|
170 | + $this->loader->reset(); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * unsets and detaches ALL objects from the cache |
|
176 | + * |
|
177 | + * @since 4.9.62.p |
|
178 | + */ |
|
179 | + public function clearCache() |
|
180 | + { |
|
181 | + $cache_class = get_class($this->cache); |
|
182 | + $collection_interface = $this->cache->collectionInterface(); |
|
183 | + $this->cache->trashAndDetachAll(); |
|
184 | + $this->cache = new $cache_class($collection_interface); |
|
185 | + if (! $this->cache instanceof CollectionInterface) { |
|
186 | + throw new InvalidDataTypeException('CachingLoader::$cache', $this->cache, 'CollectionInterface'); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * @param string $fqcn |
|
193 | + * @param array $arguments |
|
194 | + * @return bool |
|
195 | + * @throws InvalidArgumentException |
|
196 | + */ |
|
197 | + public function remove($fqcn, array $arguments = []): bool |
|
198 | + { |
|
199 | + $fqcn = ltrim($fqcn, '\\'); |
|
200 | + $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
201 | + $object = $this->cache->has($object_identifier) |
|
202 | + ? $this->cache->get($object_identifier) |
|
203 | + : $this->loader->load($fqcn, $arguments); |
|
204 | + return $this->cache->remove($object); |
|
205 | + } |
|
206 | 206 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | */ |
88 | 88 | private function setIdentifier(?string $identifier) |
89 | 89 | { |
90 | - if (! is_string($identifier)) { |
|
90 | + if ( ! is_string($identifier)) { |
|
91 | 91 | throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
92 | 92 | } |
93 | 93 | $this->identifier = $identifier; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | // WHY? because most requests for the object will not include arguments |
114 | 114 | $object_identifier = $this->object_identifier->getIdentifier($fqcn); |
115 | 115 | // but only if the object is not already in the cache |
116 | - if (! $this->cache->has($object_identifier)) { |
|
116 | + if ( ! $this->cache->has($object_identifier)) { |
|
117 | 117 | $added = $this->cache->add($object, $object_identifier) |
118 | 118 | ? true |
119 | 119 | : $added; |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $collection_interface = $this->cache->collectionInterface(); |
183 | 183 | $this->cache->trashAndDetachAll(); |
184 | 184 | $this->cache = new $cache_class($collection_interface); |
185 | - if (! $this->cache instanceof CollectionInterface) { |
|
185 | + if ( ! $this->cache instanceof CollectionInterface) { |
|
186 | 186 | throw new InvalidDataTypeException('CachingLoader::$cache', $this->cache, 'CollectionInterface'); |
187 | 187 | } |
188 | 188 | } |
@@ -16,112 +16,112 @@ |
||
16 | 16 | */ |
17 | 17 | class ObjectIdentifier |
18 | 18 | { |
19 | - /** |
|
20 | - * used to separate the FQCN from the class's arguments identifier |
|
21 | - */ |
|
22 | - const DELIMITER = '____'; |
|
19 | + /** |
|
20 | + * used to separate the FQCN from the class's arguments identifier |
|
21 | + */ |
|
22 | + const DELIMITER = '____'; |
|
23 | 23 | |
24 | - private ClassInterfaceCache $class_cache; |
|
24 | + private ClassInterfaceCache $class_cache; |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * ObjectIdentifier constructor. |
|
29 | - * |
|
30 | - * @param ClassInterfaceCache $class_cache |
|
31 | - */ |
|
32 | - public function __construct(ClassInterfaceCache $class_cache) |
|
33 | - { |
|
34 | - $this->class_cache = $class_cache; |
|
35 | - } |
|
27 | + /** |
|
28 | + * ObjectIdentifier constructor. |
|
29 | + * |
|
30 | + * @param ClassInterfaceCache $class_cache |
|
31 | + */ |
|
32 | + public function __construct(ClassInterfaceCache $class_cache) |
|
33 | + { |
|
34 | + $this->class_cache = $class_cache; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * Returns true if the supplied $object_identifier contains |
|
40 | - * the delimiter used to separate a fqcn from the arguments hash |
|
41 | - * |
|
42 | - * @param string $object_identifier |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function hasArguments(string $object_identifier): bool |
|
46 | - { |
|
47 | - // not using strpos() !== false |
|
48 | - // because an object identifier should never begin with the delimiter |
|
49 | - // therefore the delimiter should NOT be found at position 0 |
|
50 | - return strpos($object_identifier, ObjectIdentifier::DELIMITER) > 0; |
|
51 | - } |
|
38 | + /** |
|
39 | + * Returns true if the supplied $object_identifier contains |
|
40 | + * the delimiter used to separate a fqcn from the arguments hash |
|
41 | + * |
|
42 | + * @param string $object_identifier |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function hasArguments(string $object_identifier): bool |
|
46 | + { |
|
47 | + // not using strpos() !== false |
|
48 | + // because an object identifier should never begin with the delimiter |
|
49 | + // therefore the delimiter should NOT be found at position 0 |
|
50 | + return strpos($object_identifier, ObjectIdentifier::DELIMITER) > 0; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Returns true if the supplied FQCN equals the supplied $object_identifier |
|
56 | - * OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier |
|
57 | - * AND that $object_identifier is for an object with arguments. |
|
58 | - * This allows a request for an object using a FQCN to match |
|
59 | - * a previously instantiated object with arguments |
|
60 | - * without having to know those arguments. |
|
61 | - * |
|
62 | - * @param string $fqcn |
|
63 | - * @param string $object_identifier |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - public function fqcnMatchesObjectIdentifier(string $fqcn, string $object_identifier): bool |
|
67 | - { |
|
68 | - $fqcn = str_replace('\\', '_', $fqcn); |
|
69 | - return $fqcn === $object_identifier |
|
70 | - || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0; |
|
71 | - } |
|
54 | + /** |
|
55 | + * Returns true if the supplied FQCN equals the supplied $object_identifier |
|
56 | + * OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier |
|
57 | + * AND that $object_identifier is for an object with arguments. |
|
58 | + * This allows a request for an object using a FQCN to match |
|
59 | + * a previously instantiated object with arguments |
|
60 | + * without having to know those arguments. |
|
61 | + * |
|
62 | + * @param string $fqcn |
|
63 | + * @param string $object_identifier |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + public function fqcnMatchesObjectIdentifier(string $fqcn, string $object_identifier): bool |
|
67 | + { |
|
68 | + $fqcn = str_replace('\\', '_', $fqcn); |
|
69 | + return $fqcn === $object_identifier |
|
70 | + || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0; |
|
71 | + } |
|
72 | 72 | |
73 | 73 | |
74 | - /** |
|
75 | - * build a string representation of an object's FQCN and arguments |
|
76 | - * |
|
77 | - * @param string $fqcn |
|
78 | - * @param array $arguments |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function getIdentifier(string $fqcn, array $arguments = []): string |
|
82 | - { |
|
83 | - // only build identifier from arguments if class is not ReservedInstanceInterface |
|
84 | - $identifier = ! $this->class_cache->hasInterface($fqcn, ReservedInstanceInterface::class) |
|
85 | - ? $this->getIdentifierForArguments($arguments) |
|
86 | - : ''; |
|
87 | - if (! empty($identifier)) { |
|
88 | - $fqcn .= ObjectIdentifier::DELIMITER . md5($identifier); |
|
89 | - } |
|
90 | - return str_replace('\\', '_', $fqcn); |
|
91 | - } |
|
74 | + /** |
|
75 | + * build a string representation of an object's FQCN and arguments |
|
76 | + * |
|
77 | + * @param string $fqcn |
|
78 | + * @param array $arguments |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function getIdentifier(string $fqcn, array $arguments = []): string |
|
82 | + { |
|
83 | + // only build identifier from arguments if class is not ReservedInstanceInterface |
|
84 | + $identifier = ! $this->class_cache->hasInterface($fqcn, ReservedInstanceInterface::class) |
|
85 | + ? $this->getIdentifierForArguments($arguments) |
|
86 | + : ''; |
|
87 | + if (! empty($identifier)) { |
|
88 | + $fqcn .= ObjectIdentifier::DELIMITER . md5($identifier); |
|
89 | + } |
|
90 | + return str_replace('\\', '_', $fqcn); |
|
91 | + } |
|
92 | 92 | |
93 | 93 | |
94 | - /** |
|
95 | - * build a string representation of an object's arguments |
|
96 | - * (mostly because Closures can't be serialized) |
|
97 | - * |
|
98 | - * @param array $arguments |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - protected function getIdentifierForArguments(array $arguments): string |
|
102 | - { |
|
103 | - if (empty($arguments)) { |
|
104 | - return ''; |
|
105 | - } |
|
106 | - $identifier = ''; |
|
107 | - foreach ($arguments as $key => $argument) { |
|
108 | - // don't include arguments used to assist with loading legacy classes |
|
109 | - if (is_string($key) && strpos($key, 'EE_Registry::create') === 0) { |
|
110 | - continue; |
|
111 | - } |
|
112 | - switch (true) { |
|
113 | - case is_object($argument): |
|
114 | - case $argument instanceof Closure: |
|
115 | - $identifier .= spl_object_hash($argument); |
|
116 | - break; |
|
117 | - case is_array($argument): |
|
118 | - $identifier .= $this->getIdentifierForArguments($argument); |
|
119 | - break; |
|
120 | - default: |
|
121 | - $identifier .= sanitize_key($argument); |
|
122 | - break; |
|
123 | - } |
|
124 | - } |
|
125 | - return $identifier; |
|
126 | - } |
|
94 | + /** |
|
95 | + * build a string representation of an object's arguments |
|
96 | + * (mostly because Closures can't be serialized) |
|
97 | + * |
|
98 | + * @param array $arguments |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + protected function getIdentifierForArguments(array $arguments): string |
|
102 | + { |
|
103 | + if (empty($arguments)) { |
|
104 | + return ''; |
|
105 | + } |
|
106 | + $identifier = ''; |
|
107 | + foreach ($arguments as $key => $argument) { |
|
108 | + // don't include arguments used to assist with loading legacy classes |
|
109 | + if (is_string($key) && strpos($key, 'EE_Registry::create') === 0) { |
|
110 | + continue; |
|
111 | + } |
|
112 | + switch (true) { |
|
113 | + case is_object($argument): |
|
114 | + case $argument instanceof Closure: |
|
115 | + $identifier .= spl_object_hash($argument); |
|
116 | + break; |
|
117 | + case is_array($argument): |
|
118 | + $identifier .= $this->getIdentifierForArguments($argument); |
|
119 | + break; |
|
120 | + default: |
|
121 | + $identifier .= sanitize_key($argument); |
|
122 | + break; |
|
123 | + } |
|
124 | + } |
|
125 | + return $identifier; |
|
126 | + } |
|
127 | 127 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | { |
68 | 68 | $fqcn = str_replace('\\', '_', $fqcn); |
69 | 69 | return $fqcn === $object_identifier |
70 | - || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0; |
|
70 | + || strpos($object_identifier, $fqcn.ObjectIdentifier::DELIMITER) === 0; |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | |
@@ -84,8 +84,8 @@ discard block |
||
84 | 84 | $identifier = ! $this->class_cache->hasInterface($fqcn, ReservedInstanceInterface::class) |
85 | 85 | ? $this->getIdentifierForArguments($arguments) |
86 | 86 | : ''; |
87 | - if (! empty($identifier)) { |
|
88 | - $fqcn .= ObjectIdentifier::DELIMITER . md5($identifier); |
|
87 | + if ( ! empty($identifier)) { |
|
88 | + $fqcn .= ObjectIdentifier::DELIMITER.md5($identifier); |
|
89 | 89 | } |
90 | 90 | return str_replace('\\', '_', $fqcn); |
91 | 91 | } |
@@ -24,106 +24,106 @@ |
||
24 | 24 | */ |
25 | 25 | class AddonManager |
26 | 26 | { |
27 | - private AddonCollection $addons; |
|
27 | + private AddonCollection $addons; |
|
28 | 28 | |
29 | - private EE_Dependency_Map $dependency_map; |
|
29 | + private EE_Dependency_Map $dependency_map; |
|
30 | 30 | |
31 | - private IncompatibleAddonHandler $incompatible_addon_handler; |
|
31 | + private IncompatibleAddonHandler $incompatible_addon_handler; |
|
32 | 32 | |
33 | - private RegisterV1Addon $register_v1_addon; |
|
33 | + private RegisterV1Addon $register_v1_addon; |
|
34 | 34 | |
35 | - private ThirdPartyPluginHandler $third_party_plugin_handler; |
|
35 | + private ThirdPartyPluginHandler $third_party_plugin_handler; |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * AddonManager constructor. |
|
40 | - * |
|
41 | - * @param AddonCollection $addons |
|
42 | - * @param EE_Dependency_Map $dependency_map |
|
43 | - * @param IncompatibleAddonHandler $incompatible_addon_handler |
|
44 | - * @param RegisterV1Addon $register_v1_addon |
|
45 | - * @param ThirdPartyPluginHandler $third_party_plugin_handler |
|
46 | - */ |
|
47 | - public function __construct( |
|
48 | - AddonCollection $addons, |
|
49 | - EE_Dependency_Map $dependency_map, |
|
50 | - IncompatibleAddonHandler $incompatible_addon_handler, |
|
51 | - RegisterV1Addon $register_v1_addon, |
|
52 | - ThirdPartyPluginHandler $third_party_plugin_handler |
|
53 | - ) { |
|
54 | - $this->addons = $addons; |
|
55 | - $this->dependency_map = $dependency_map; |
|
56 | - $this->incompatible_addon_handler = $incompatible_addon_handler; |
|
57 | - $this->register_v1_addon = $register_v1_addon; |
|
58 | - $this->third_party_plugin_handler = $third_party_plugin_handler; |
|
59 | - } |
|
38 | + /** |
|
39 | + * AddonManager constructor. |
|
40 | + * |
|
41 | + * @param AddonCollection $addons |
|
42 | + * @param EE_Dependency_Map $dependency_map |
|
43 | + * @param IncompatibleAddonHandler $incompatible_addon_handler |
|
44 | + * @param RegisterV1Addon $register_v1_addon |
|
45 | + * @param ThirdPartyPluginHandler $third_party_plugin_handler |
|
46 | + */ |
|
47 | + public function __construct( |
|
48 | + AddonCollection $addons, |
|
49 | + EE_Dependency_Map $dependency_map, |
|
50 | + IncompatibleAddonHandler $incompatible_addon_handler, |
|
51 | + RegisterV1Addon $register_v1_addon, |
|
52 | + ThirdPartyPluginHandler $third_party_plugin_handler |
|
53 | + ) { |
|
54 | + $this->addons = $addons; |
|
55 | + $this->dependency_map = $dependency_map; |
|
56 | + $this->incompatible_addon_handler = $incompatible_addon_handler; |
|
57 | + $this->register_v1_addon = $register_v1_addon; |
|
58 | + $this->third_party_plugin_handler = $third_party_plugin_handler; |
|
59 | + } |
|
60 | 60 | |
61 | 61 | |
62 | - /** |
|
63 | - * @throws Exception |
|
64 | - */ |
|
65 | - public function initialize() |
|
66 | - { |
|
67 | - // set autoloaders for all the classes implementing the legacy EEI_Plugin_API |
|
68 | - // which provide helpers for EE plugin authors to more easily register certain components with EE. |
|
69 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api'); |
|
70 | - $this->dependency_map->registerDependencies( |
|
71 | - AddonApiVersion::class, |
|
72 | - [ |
|
73 | - 'EventEspresso\core\services\addon\api\AddonRoutes' => EE_Dependency_Map::load_from_cache, |
|
74 | - 'EventEspresso\core\services\addon\api\DependencyHandlers' => EE_Dependency_Map::load_from_cache, |
|
75 | - 'EventEspresso\core\Psr4Autoloader' => EE_Dependency_Map::load_from_cache, |
|
76 | - ] |
|
77 | - ); |
|
78 | - $this->dependency_map->registerDependencies( |
|
79 | - 'EventEspresso\core\services\addon\api\v1\AddonApi', |
|
80 | - [ |
|
81 | - 'EventEspresso\core\services\addon\api\AddonRoutes' => EE_Dependency_Map::load_from_cache, |
|
82 | - 'EventEspresso\core\services\addon\api\DependencyHandlers' => EE_Dependency_Map::load_from_cache, |
|
83 | - 'EventEspresso\core\Psr4Autoloader' => EE_Dependency_Map::load_from_cache, |
|
84 | - 'EventEspresso\core\services\addon\api\v1\DataMigrationApi' => EE_Dependency_Map::load_from_cache, |
|
85 | - 'EventEspresso\core\services\addon\api\v1\LegacyModelApi' => EE_Dependency_Map::load_from_cache, |
|
86 | - ] |
|
87 | - ); |
|
88 | - } |
|
62 | + /** |
|
63 | + * @throws Exception |
|
64 | + */ |
|
65 | + public function initialize() |
|
66 | + { |
|
67 | + // set autoloaders for all the classes implementing the legacy EEI_Plugin_API |
|
68 | + // which provide helpers for EE plugin authors to more easily register certain components with EE. |
|
69 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api'); |
|
70 | + $this->dependency_map->registerDependencies( |
|
71 | + AddonApiVersion::class, |
|
72 | + [ |
|
73 | + 'EventEspresso\core\services\addon\api\AddonRoutes' => EE_Dependency_Map::load_from_cache, |
|
74 | + 'EventEspresso\core\services\addon\api\DependencyHandlers' => EE_Dependency_Map::load_from_cache, |
|
75 | + 'EventEspresso\core\Psr4Autoloader' => EE_Dependency_Map::load_from_cache, |
|
76 | + ] |
|
77 | + ); |
|
78 | + $this->dependency_map->registerDependencies( |
|
79 | + 'EventEspresso\core\services\addon\api\v1\AddonApi', |
|
80 | + [ |
|
81 | + 'EventEspresso\core\services\addon\api\AddonRoutes' => EE_Dependency_Map::load_from_cache, |
|
82 | + 'EventEspresso\core\services\addon\api\DependencyHandlers' => EE_Dependency_Map::load_from_cache, |
|
83 | + 'EventEspresso\core\Psr4Autoloader' => EE_Dependency_Map::load_from_cache, |
|
84 | + 'EventEspresso\core\services\addon\api\v1\DataMigrationApi' => EE_Dependency_Map::load_from_cache, |
|
85 | + 'EventEspresso\core\services\addon\api\v1\LegacyModelApi' => EE_Dependency_Map::load_from_cache, |
|
86 | + ] |
|
87 | + ); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * @throws Exception |
|
93 | - * @throws Throwable |
|
94 | - */ |
|
95 | - public function loadAddons() |
|
96 | - { |
|
97 | - try { |
|
98 | - $this->incompatible_addon_handler->deactivateIncompatibleAddons(); |
|
99 | - // legacy add-on API |
|
100 | - do_action('AHEE__EE_System__load_espresso_addons'); |
|
101 | - // new add-on API that uses versioning |
|
102 | - do_action('AHEE__EventEspresso_core_services_addon_AddonManager__loadAddons'); |
|
103 | - // addons are responsible for loading their AddonApiVersion into the AddonCollection |
|
104 | - foreach ($this->addons as $addon) { |
|
105 | - if ($addon instanceof AddonApiVersion) { |
|
106 | - $this->registerAddon($addon); |
|
107 | - } |
|
108 | - } |
|
109 | - $this->third_party_plugin_handler->loadPlugins(); |
|
110 | - do_action('AHEE__EE_System__load_espresso_addons__complete'); |
|
111 | - } catch (Exception $exception) { |
|
112 | - new ExceptionStackTraceDisplay($exception); |
|
113 | - } |
|
114 | - } |
|
91 | + /** |
|
92 | + * @throws Exception |
|
93 | + * @throws Throwable |
|
94 | + */ |
|
95 | + public function loadAddons() |
|
96 | + { |
|
97 | + try { |
|
98 | + $this->incompatible_addon_handler->deactivateIncompatibleAddons(); |
|
99 | + // legacy add-on API |
|
100 | + do_action('AHEE__EE_System__load_espresso_addons'); |
|
101 | + // new add-on API that uses versioning |
|
102 | + do_action('AHEE__EventEspresso_core_services_addon_AddonManager__loadAddons'); |
|
103 | + // addons are responsible for loading their AddonApiVersion into the AddonCollection |
|
104 | + foreach ($this->addons as $addon) { |
|
105 | + if ($addon instanceof AddonApiVersion) { |
|
106 | + $this->registerAddon($addon); |
|
107 | + } |
|
108 | + } |
|
109 | + $this->third_party_plugin_handler->loadPlugins(); |
|
110 | + do_action('AHEE__EE_System__load_espresso_addons__complete'); |
|
111 | + } catch (Exception $exception) { |
|
112 | + new ExceptionStackTraceDisplay($exception); |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | 116 | |
117 | - /** |
|
118 | - * @param AddonApiVersion $addon |
|
119 | - * @throws EE_Error |
|
120 | - */ |
|
121 | - private function registerAddon(AddonApiVersion $addon) |
|
122 | - { |
|
123 | - $addon->initialize(); |
|
124 | - // now register each addon based on it's API version |
|
125 | - if ($addon instanceof AddonApiV1) { |
|
126 | - $this->register_v1_addon->register($addon); |
|
127 | - } |
|
128 | - } |
|
117 | + /** |
|
118 | + * @param AddonApiVersion $addon |
|
119 | + * @throws EE_Error |
|
120 | + */ |
|
121 | + private function registerAddon(AddonApiVersion $addon) |
|
122 | + { |
|
123 | + $addon->initialize(); |
|
124 | + // now register each addon based on it's API version |
|
125 | + if ($addon instanceof AddonApiV1) { |
|
126 | + $this->register_v1_addon->register($addon); |
|
127 | + } |
|
128 | + } |
|
129 | 129 | } |
@@ -66,7 +66,7 @@ |
||
66 | 66 | { |
67 | 67 | // set autoloaders for all the classes implementing the legacy EEI_Plugin_API |
68 | 68 | // which provide helpers for EE plugin authors to more easily register certain components with EE. |
69 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api'); |
|
69 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES.'plugin_api'); |
|
70 | 70 | $this->dependency_map->registerDependencies( |
71 | 71 | AddonApiVersion::class, |
72 | 72 | [ |
@@ -17,46 +17,46 @@ |
||
17 | 17 | */ |
18 | 18 | class DependencyHandlers |
19 | 19 | { |
20 | - private EE_Dependency_Map $dependency_map; |
|
21 | - |
|
22 | - /** |
|
23 | - * array of fully qualified class names for dependency handlers where keys are add-on slugs |
|
24 | - * @var string[][] |
|
25 | - */ |
|
26 | - private array $dependency_handlers = []; |
|
27 | - |
|
28 | - |
|
29 | - public function __construct(EE_Dependency_Map $dependency_map) |
|
30 | - { |
|
31 | - $this->dependency_map = $dependency_map; |
|
32 | - } |
|
33 | - |
|
34 | - |
|
35 | - public function addDependencyHandlerFor(string $dependency_handler_fqcn, string $addon_slug) |
|
36 | - { |
|
37 | - if (! isset($this->dependency_handlers[ $addon_slug ])) { |
|
38 | - $this->dependency_handlers[ $addon_slug ] = []; |
|
39 | - } |
|
40 | - $this->dependency_handlers[ $addon_slug ][] = $dependency_handler_fqcn; |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - public function registerDependenciesFor(string $addon_slug) |
|
45 | - { |
|
46 | - if (isset($this->dependency_handlers[ $addon_slug ])) { |
|
47 | - foreach ($this->dependency_handlers[ $addon_slug ] as $dependency_handler_fqcn) { |
|
48 | - $dependency_handler = LoaderFactory::getNew($dependency_handler_fqcn, [$this->dependency_map]); |
|
49 | - if (! $dependency_handler instanceof DependencyHandlerInterface) { |
|
50 | - throw new DomainException( |
|
51 | - sprintf( |
|
52 | - esc_html__('The dependency handler class %1$s must implement %2$s', 'event_espresso'), |
|
53 | - $dependency_handler_fqcn, |
|
54 | - DependencyHandlerInterface::class |
|
55 | - ) |
|
56 | - ); |
|
57 | - } |
|
58 | - $dependency_handler->registerDependencies(); |
|
59 | - } |
|
60 | - } |
|
61 | - } |
|
20 | + private EE_Dependency_Map $dependency_map; |
|
21 | + |
|
22 | + /** |
|
23 | + * array of fully qualified class names for dependency handlers where keys are add-on slugs |
|
24 | + * @var string[][] |
|
25 | + */ |
|
26 | + private array $dependency_handlers = []; |
|
27 | + |
|
28 | + |
|
29 | + public function __construct(EE_Dependency_Map $dependency_map) |
|
30 | + { |
|
31 | + $this->dependency_map = $dependency_map; |
|
32 | + } |
|
33 | + |
|
34 | + |
|
35 | + public function addDependencyHandlerFor(string $dependency_handler_fqcn, string $addon_slug) |
|
36 | + { |
|
37 | + if (! isset($this->dependency_handlers[ $addon_slug ])) { |
|
38 | + $this->dependency_handlers[ $addon_slug ] = []; |
|
39 | + } |
|
40 | + $this->dependency_handlers[ $addon_slug ][] = $dependency_handler_fqcn; |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + public function registerDependenciesFor(string $addon_slug) |
|
45 | + { |
|
46 | + if (isset($this->dependency_handlers[ $addon_slug ])) { |
|
47 | + foreach ($this->dependency_handlers[ $addon_slug ] as $dependency_handler_fqcn) { |
|
48 | + $dependency_handler = LoaderFactory::getNew($dependency_handler_fqcn, [$this->dependency_map]); |
|
49 | + if (! $dependency_handler instanceof DependencyHandlerInterface) { |
|
50 | + throw new DomainException( |
|
51 | + sprintf( |
|
52 | + esc_html__('The dependency handler class %1$s must implement %2$s', 'event_espresso'), |
|
53 | + $dependency_handler_fqcn, |
|
54 | + DependencyHandlerInterface::class |
|
55 | + ) |
|
56 | + ); |
|
57 | + } |
|
58 | + $dependency_handler->registerDependencies(); |
|
59 | + } |
|
60 | + } |
|
61 | + } |
|
62 | 62 | } |
@@ -34,19 +34,19 @@ |
||
34 | 34 | |
35 | 35 | public function addDependencyHandlerFor(string $dependency_handler_fqcn, string $addon_slug) |
36 | 36 | { |
37 | - if (! isset($this->dependency_handlers[ $addon_slug ])) { |
|
38 | - $this->dependency_handlers[ $addon_slug ] = []; |
|
37 | + if ( ! isset($this->dependency_handlers[$addon_slug])) { |
|
38 | + $this->dependency_handlers[$addon_slug] = []; |
|
39 | 39 | } |
40 | - $this->dependency_handlers[ $addon_slug ][] = $dependency_handler_fqcn; |
|
40 | + $this->dependency_handlers[$addon_slug][] = $dependency_handler_fqcn; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | |
44 | 44 | public function registerDependenciesFor(string $addon_slug) |
45 | 45 | { |
46 | - if (isset($this->dependency_handlers[ $addon_slug ])) { |
|
47 | - foreach ($this->dependency_handlers[ $addon_slug ] as $dependency_handler_fqcn) { |
|
46 | + if (isset($this->dependency_handlers[$addon_slug])) { |
|
47 | + foreach ($this->dependency_handlers[$addon_slug] as $dependency_handler_fqcn) { |
|
48 | 48 | $dependency_handler = LoaderFactory::getNew($dependency_handler_fqcn, [$this->dependency_map]); |
49 | - if (! $dependency_handler instanceof DependencyHandlerInterface) { |
|
49 | + if ( ! $dependency_handler instanceof DependencyHandlerInterface) { |
|
50 | 50 | throw new DomainException( |
51 | 51 | sprintf( |
52 | 52 | esc_html__('The dependency handler class %1$s must implement %2$s', 'event_espresso'), |
@@ -23,344 +23,344 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class AddonApiVersion |
25 | 25 | { |
26 | - const V1 = 1; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var int one of the V# API version constants from above |
|
30 | - */ |
|
31 | - private int $api_version; |
|
32 | - |
|
33 | - private DomainInterface $domain; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var Version|null minimum version of EE core that the add-on will work with |
|
37 | - */ |
|
38 | - private ?Version $min_core_version = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var Version|null minimum version of WP core that the add-on will work with |
|
42 | - */ |
|
43 | - private ?Version $min_wp_version = null; |
|
26 | + const V1 = 1; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var int one of the V# API version constants from above |
|
30 | + */ |
|
31 | + private int $api_version; |
|
32 | + |
|
33 | + private DomainInterface $domain; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var Version|null minimum version of EE core that the add-on will work with |
|
37 | + */ |
|
38 | + private ?Version $min_core_version = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var Version|null minimum version of WP core that the add-on will work with |
|
42 | + */ |
|
43 | + private ?Version $min_wp_version = null; |
|
44 | 44 | |
45 | - private int $id; |
|
45 | + private int $id; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var string PascalCase identifier for the add-on. |
|
49 | - * IMPORTANT! there must be a class of the same name in the root of the add-ons /src/domain/ folder |
|
50 | - */ |
|
51 | - private string $name; |
|
47 | + /** |
|
48 | + * @var string PascalCase identifier for the add-on. |
|
49 | + * IMPORTANT! there must be a class of the same name in the root of the add-ons /src/domain/ folder |
|
50 | + */ |
|
51 | + private string $name; |
|
52 | 52 | |
53 | - private string $display_name; |
|
53 | + private string $display_name; |
|
54 | 54 | |
55 | - private string $addon_namespace; |
|
55 | + private string $addon_namespace; |
|
56 | 56 | |
57 | - private FilePath $main_file; |
|
58 | - |
|
59 | - private string $slug; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var Version the current add-on version |
|
63 | - */ |
|
64 | - private Version $version; |
|
65 | - |
|
66 | - private AddonRoutes $addon_routes; |
|
67 | - |
|
68 | - private DependencyHandlers $dependency_handlers; |
|
69 | - |
|
70 | - private Psr4Autoloader $psr4_loader; |
|
57 | + private FilePath $main_file; |
|
58 | + |
|
59 | + private string $slug; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var Version the current add-on version |
|
63 | + */ |
|
64 | + private Version $version; |
|
65 | + |
|
66 | + private AddonRoutes $addon_routes; |
|
67 | + |
|
68 | + private DependencyHandlers $dependency_handlers; |
|
69 | + |
|
70 | + private Psr4Autoloader $psr4_loader; |
|
71 | 71 | |
72 | - private bool $dependencies_registered = false; |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * Bootstrap constructor. |
|
77 | - * |
|
78 | - * @param AddonRoutes $addon_routes |
|
79 | - * @param DependencyHandlers $dependency_handlers |
|
80 | - * @param Psr4Autoloader $psr4_loader |
|
81 | - * @param int $api_version |
|
82 | - */ |
|
83 | - protected function __construct(AddonRoutes $addon_routes, DependencyHandlers $dependency_handlers, Psr4Autoloader $psr4_loader, int $api_version) { |
|
84 | - $this->addon_routes = LoaderFactory::getShared(AddonRoutes::class); |
|
85 | - $this->dependency_handlers = LoaderFactory::getShared(DependencyHandlers::class); |
|
86 | - $this->psr4_loader = LoaderFactory::getShared(Psr4Autoloader::class); |
|
87 | - $this->setApiVersion($api_version); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function initialize(): void |
|
95 | - { |
|
96 | - $this->domain = DomainFactory::create( |
|
97 | - "$this->addon_namespace\\domain\\Domain", |
|
98 | - $this->main_file, |
|
99 | - $this->version |
|
100 | - ); |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * @return int[] |
|
106 | - */ |
|
107 | - private function validApiVersions(): array |
|
108 | - { |
|
109 | - return [ |
|
110 | - AddonApiVersion::V1, |
|
111 | - ]; |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * @param int $api_version |
|
117 | - */ |
|
118 | - private function setApiVersion(int $api_version): void |
|
119 | - { |
|
120 | - if (! in_array($api_version, $this->validApiVersions())) { |
|
121 | - throw new DomainException( |
|
122 | - esc_html__( |
|
123 | - 'Invalid Add-on API Version! Please use one of the EventEspresso\core\domain\entities\addon\Bootstrap class constants', |
|
124 | - 'event_espresso' |
|
125 | - ) |
|
126 | - ); |
|
127 | - } |
|
128 | - $this->api_version = $api_version; |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - public function setDisplayName(string $display_name): void |
|
133 | - { |
|
134 | - $this->display_name = $display_name; |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - public function setID(int $id = 0): void |
|
139 | - { |
|
140 | - $this->id = $id; |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $main_file |
|
147 | - */ |
|
148 | - public function setMainFile(string $main_file): void |
|
149 | - { |
|
150 | - $this->main_file = new FilePath($main_file); |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @param string $min_core_version |
|
156 | - */ |
|
157 | - public function setMinCoreVersion(string $min_core_version): void |
|
158 | - { |
|
159 | - $this->min_core_version = Version::fromString($min_core_version); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * @param string $min_wp_version |
|
165 | - */ |
|
166 | - public function setMinWpVersion(string $min_wp_version = EE_MIN_WP_VER_REQUIRED): void |
|
167 | - { |
|
168 | - $this->min_wp_version = Version::fromString($min_wp_version); |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $name |
|
174 | - */ |
|
175 | - public function setName(string $name): void |
|
176 | - { |
|
177 | - $this->name = $name; |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * @param string $namespace |
|
183 | - */ |
|
184 | - public function setNamespace(string $namespace): void |
|
185 | - { |
|
186 | - $this->addon_namespace = $namespace; |
|
187 | - // register addon namespace immediately so that FQCNs resolve correctly |
|
188 | - $this->psr4_loader->addNamespace($namespace, dirname($this->main_file) . '/src/'); |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * @param string $slug |
|
194 | - */ |
|
195 | - public function setSlug(string $slug): void |
|
196 | - { |
|
197 | - $valid_slug = sanitize_key($slug); |
|
198 | - if ($slug !== $valid_slug) { |
|
199 | - throw new DomainException( |
|
200 | - esc_html__( |
|
201 | - 'Invalid Add-on "slug"! Please ensure that slug only uses lowercase characters and dashes.', |
|
202 | - 'event_espresso' |
|
203 | - ) |
|
204 | - ); |
|
205 | - } |
|
206 | - $this->slug = $valid_slug; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * @param string $version |
|
212 | - */ |
|
213 | - public function setVersion(string $version): void |
|
214 | - { |
|
215 | - $this->version = Version::fromString($version); |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * @return int |
|
221 | - */ |
|
222 | - public function apiVersion(): int |
|
223 | - { |
|
224 | - return $this->api_version; |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - public function displayName(): string |
|
229 | - { |
|
230 | - return $this->display_name; |
|
231 | - } |
|
232 | - |
|
233 | - |
|
234 | - /** |
|
235 | - * @return DomainInterface |
|
236 | - */ |
|
237 | - public function domain(): DomainInterface |
|
238 | - { |
|
239 | - return $this->domain; |
|
240 | - } |
|
241 | - |
|
242 | - |
|
243 | - public function ID(): int |
|
244 | - { |
|
245 | - return $this->id; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @return Version|null |
|
252 | - */ |
|
253 | - public function minCoreVersion(): ?Version |
|
254 | - { |
|
255 | - return $this->min_core_version; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * @return Version|null |
|
261 | - */ |
|
262 | - public function minWpVersion(): ?Version |
|
263 | - { |
|
264 | - return $this->min_wp_version; |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * @return string |
|
270 | - */ |
|
271 | - public function name(): string |
|
272 | - { |
|
273 | - return $this->name; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * FQCN for the domain's EE_Addon class |
|
279 | - * |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - public function fqcn(): string |
|
283 | - { |
|
284 | - return "$this->addon_namespace\\domain\\$this->name"; |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * @return string |
|
290 | - */ |
|
291 | - public function getNamespace(): string |
|
292 | - { |
|
293 | - return $this->addon_namespace; |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - /** |
|
298 | - * @return FilePath |
|
299 | - */ |
|
300 | - public function mainFile(): FilePath |
|
301 | - { |
|
302 | - return $this->main_file; |
|
303 | - } |
|
304 | - |
|
305 | - |
|
306 | - /** |
|
307 | - * @return string |
|
308 | - */ |
|
309 | - public function slug(): string |
|
310 | - { |
|
311 | - return $this->slug; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * @return Version |
|
317 | - */ |
|
318 | - public function version(): Version |
|
319 | - { |
|
320 | - return $this->version; |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - /** |
|
325 | - * @throws Exception |
|
326 | - */ |
|
327 | - public function addRoute(string $route_fqcn, string $hook_name = 'AHEE__EE_System__core_loaded_and_ready'): void |
|
328 | - { |
|
329 | - $this->addon_routes->addRouteFor($route_fqcn, $this->slug()); |
|
330 | - add_action($hook_name, [$this, 'loadRoutes']); |
|
331 | - } |
|
332 | - |
|
333 | - |
|
334 | - /** |
|
335 | - * @throws Exception|Throwable |
|
336 | - */ |
|
337 | - public function loadRoutes() |
|
338 | - { |
|
339 | - $this->addon_routes->loadRoutesFor($this->slug()); |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * @param string $dependency_handler_fqcn FQCN of a DependencyHandler class |
|
345 | - * @return void |
|
346 | - * @since 5.0.30.p |
|
347 | - */ |
|
348 | - public function addDependencyHandler(string $dependency_handler_fqcn): void |
|
349 | - { |
|
350 | - $this->dependency_handlers->addDependencyHandlerFor($dependency_handler_fqcn, $this->slug()); |
|
351 | - if (! $this->dependencies_registered) { |
|
352 | - add_action('AHEE__EE_System__load_espresso_addons__complete', [$this, 'registerDependencies'], 999); |
|
353 | - $this->dependencies_registered = true; |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * @return void |
|
360 | - * @since 5.0.30.p |
|
361 | - */ |
|
362 | - public function registerDependencies() |
|
363 | - { |
|
364 | - $this->dependency_handlers->registerDependenciesFor($this->slug()); |
|
365 | - } |
|
72 | + private bool $dependencies_registered = false; |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * Bootstrap constructor. |
|
77 | + * |
|
78 | + * @param AddonRoutes $addon_routes |
|
79 | + * @param DependencyHandlers $dependency_handlers |
|
80 | + * @param Psr4Autoloader $psr4_loader |
|
81 | + * @param int $api_version |
|
82 | + */ |
|
83 | + protected function __construct(AddonRoutes $addon_routes, DependencyHandlers $dependency_handlers, Psr4Autoloader $psr4_loader, int $api_version) { |
|
84 | + $this->addon_routes = LoaderFactory::getShared(AddonRoutes::class); |
|
85 | + $this->dependency_handlers = LoaderFactory::getShared(DependencyHandlers::class); |
|
86 | + $this->psr4_loader = LoaderFactory::getShared(Psr4Autoloader::class); |
|
87 | + $this->setApiVersion($api_version); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function initialize(): void |
|
95 | + { |
|
96 | + $this->domain = DomainFactory::create( |
|
97 | + "$this->addon_namespace\\domain\\Domain", |
|
98 | + $this->main_file, |
|
99 | + $this->version |
|
100 | + ); |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * @return int[] |
|
106 | + */ |
|
107 | + private function validApiVersions(): array |
|
108 | + { |
|
109 | + return [ |
|
110 | + AddonApiVersion::V1, |
|
111 | + ]; |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * @param int $api_version |
|
117 | + */ |
|
118 | + private function setApiVersion(int $api_version): void |
|
119 | + { |
|
120 | + if (! in_array($api_version, $this->validApiVersions())) { |
|
121 | + throw new DomainException( |
|
122 | + esc_html__( |
|
123 | + 'Invalid Add-on API Version! Please use one of the EventEspresso\core\domain\entities\addon\Bootstrap class constants', |
|
124 | + 'event_espresso' |
|
125 | + ) |
|
126 | + ); |
|
127 | + } |
|
128 | + $this->api_version = $api_version; |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + public function setDisplayName(string $display_name): void |
|
133 | + { |
|
134 | + $this->display_name = $display_name; |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + public function setID(int $id = 0): void |
|
139 | + { |
|
140 | + $this->id = $id; |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $main_file |
|
147 | + */ |
|
148 | + public function setMainFile(string $main_file): void |
|
149 | + { |
|
150 | + $this->main_file = new FilePath($main_file); |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @param string $min_core_version |
|
156 | + */ |
|
157 | + public function setMinCoreVersion(string $min_core_version): void |
|
158 | + { |
|
159 | + $this->min_core_version = Version::fromString($min_core_version); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * @param string $min_wp_version |
|
165 | + */ |
|
166 | + public function setMinWpVersion(string $min_wp_version = EE_MIN_WP_VER_REQUIRED): void |
|
167 | + { |
|
168 | + $this->min_wp_version = Version::fromString($min_wp_version); |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $name |
|
174 | + */ |
|
175 | + public function setName(string $name): void |
|
176 | + { |
|
177 | + $this->name = $name; |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * @param string $namespace |
|
183 | + */ |
|
184 | + public function setNamespace(string $namespace): void |
|
185 | + { |
|
186 | + $this->addon_namespace = $namespace; |
|
187 | + // register addon namespace immediately so that FQCNs resolve correctly |
|
188 | + $this->psr4_loader->addNamespace($namespace, dirname($this->main_file) . '/src/'); |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * @param string $slug |
|
194 | + */ |
|
195 | + public function setSlug(string $slug): void |
|
196 | + { |
|
197 | + $valid_slug = sanitize_key($slug); |
|
198 | + if ($slug !== $valid_slug) { |
|
199 | + throw new DomainException( |
|
200 | + esc_html__( |
|
201 | + 'Invalid Add-on "slug"! Please ensure that slug only uses lowercase characters and dashes.', |
|
202 | + 'event_espresso' |
|
203 | + ) |
|
204 | + ); |
|
205 | + } |
|
206 | + $this->slug = $valid_slug; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * @param string $version |
|
212 | + */ |
|
213 | + public function setVersion(string $version): void |
|
214 | + { |
|
215 | + $this->version = Version::fromString($version); |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * @return int |
|
221 | + */ |
|
222 | + public function apiVersion(): int |
|
223 | + { |
|
224 | + return $this->api_version; |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + public function displayName(): string |
|
229 | + { |
|
230 | + return $this->display_name; |
|
231 | + } |
|
232 | + |
|
233 | + |
|
234 | + /** |
|
235 | + * @return DomainInterface |
|
236 | + */ |
|
237 | + public function domain(): DomainInterface |
|
238 | + { |
|
239 | + return $this->domain; |
|
240 | + } |
|
241 | + |
|
242 | + |
|
243 | + public function ID(): int |
|
244 | + { |
|
245 | + return $this->id; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @return Version|null |
|
252 | + */ |
|
253 | + public function minCoreVersion(): ?Version |
|
254 | + { |
|
255 | + return $this->min_core_version; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * @return Version|null |
|
261 | + */ |
|
262 | + public function minWpVersion(): ?Version |
|
263 | + { |
|
264 | + return $this->min_wp_version; |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * @return string |
|
270 | + */ |
|
271 | + public function name(): string |
|
272 | + { |
|
273 | + return $this->name; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * FQCN for the domain's EE_Addon class |
|
279 | + * |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + public function fqcn(): string |
|
283 | + { |
|
284 | + return "$this->addon_namespace\\domain\\$this->name"; |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * @return string |
|
290 | + */ |
|
291 | + public function getNamespace(): string |
|
292 | + { |
|
293 | + return $this->addon_namespace; |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + /** |
|
298 | + * @return FilePath |
|
299 | + */ |
|
300 | + public function mainFile(): FilePath |
|
301 | + { |
|
302 | + return $this->main_file; |
|
303 | + } |
|
304 | + |
|
305 | + |
|
306 | + /** |
|
307 | + * @return string |
|
308 | + */ |
|
309 | + public function slug(): string |
|
310 | + { |
|
311 | + return $this->slug; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * @return Version |
|
317 | + */ |
|
318 | + public function version(): Version |
|
319 | + { |
|
320 | + return $this->version; |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + /** |
|
325 | + * @throws Exception |
|
326 | + */ |
|
327 | + public function addRoute(string $route_fqcn, string $hook_name = 'AHEE__EE_System__core_loaded_and_ready'): void |
|
328 | + { |
|
329 | + $this->addon_routes->addRouteFor($route_fqcn, $this->slug()); |
|
330 | + add_action($hook_name, [$this, 'loadRoutes']); |
|
331 | + } |
|
332 | + |
|
333 | + |
|
334 | + /** |
|
335 | + * @throws Exception|Throwable |
|
336 | + */ |
|
337 | + public function loadRoutes() |
|
338 | + { |
|
339 | + $this->addon_routes->loadRoutesFor($this->slug()); |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * @param string $dependency_handler_fqcn FQCN of a DependencyHandler class |
|
345 | + * @return void |
|
346 | + * @since 5.0.30.p |
|
347 | + */ |
|
348 | + public function addDependencyHandler(string $dependency_handler_fqcn): void |
|
349 | + { |
|
350 | + $this->dependency_handlers->addDependencyHandlerFor($dependency_handler_fqcn, $this->slug()); |
|
351 | + if (! $this->dependencies_registered) { |
|
352 | + add_action('AHEE__EE_System__load_espresso_addons__complete', [$this, 'registerDependencies'], 999); |
|
353 | + $this->dependencies_registered = true; |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * @return void |
|
360 | + * @since 5.0.30.p |
|
361 | + */ |
|
362 | + public function registerDependencies() |
|
363 | + { |
|
364 | + $this->dependency_handlers->registerDependenciesFor($this->slug()); |
|
365 | + } |
|
366 | 366 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | private function setApiVersion(int $api_version): void |
119 | 119 | { |
120 | - if (! in_array($api_version, $this->validApiVersions())) { |
|
120 | + if ( ! in_array($api_version, $this->validApiVersions())) { |
|
121 | 121 | throw new DomainException( |
122 | 122 | esc_html__( |
123 | 123 | 'Invalid Add-on API Version! Please use one of the EventEspresso\core\domain\entities\addon\Bootstrap class constants', |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | { |
186 | 186 | $this->addon_namespace = $namespace; |
187 | 187 | // register addon namespace immediately so that FQCNs resolve correctly |
188 | - $this->psr4_loader->addNamespace($namespace, dirname($this->main_file) . '/src/'); |
|
188 | + $this->psr4_loader->addNamespace($namespace, dirname($this->main_file).'/src/'); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | public function addDependencyHandler(string $dependency_handler_fqcn): void |
349 | 349 | { |
350 | 350 | $this->dependency_handlers->addDependencyHandlerFor($dependency_handler_fqcn, $this->slug()); |
351 | - if (! $this->dependencies_registered) { |
|
351 | + if ( ! $this->dependencies_registered) { |
|
352 | 352 | add_action('AHEE__EE_System__load_espresso_addons__complete', [$this, 'registerDependencies'], 999); |
353 | 353 | $this->dependencies_registered = true; |
354 | 354 | } |
@@ -18,76 +18,76 @@ |
||
18 | 18 | */ |
19 | 19 | class AddonApiFactory |
20 | 20 | { |
21 | - public static function addonApiV1( |
|
22 | - int $ID, |
|
23 | - string $main_file, |
|
24 | - string $name, |
|
25 | - string $display_name, |
|
26 | - string $namespace, |
|
27 | - string $min_core_version |
|
28 | - ): AddonApi { |
|
29 | - $addon_collection = AddonApiFactory::getAddonCollection(); |
|
30 | - $slug = AddonApiFactory::getSlugFromMainfile($main_file); |
|
31 | - if ($addon_collection->hasAddon($slug)) { |
|
32 | - return $addon_collection->getAddon($slug); |
|
33 | - } |
|
34 | - $addon = AddonApiFactory::createAddon(); |
|
35 | - $addon->setID($ID); |
|
36 | - $addon->setSlug($slug); |
|
37 | - $addon->setName($name); |
|
38 | - $addon->setDisplayName($display_name); |
|
39 | - $addon->setMainFile($main_file); |
|
40 | - $addon->setVersion(AddonApiFactory::getVersionFromMainfile($main_file)); |
|
41 | - $addon->setMinCoreVersion($min_core_version); |
|
42 | - $addon->setNamespace($namespace); |
|
43 | - $addon_collection->addAddon($addon); |
|
44 | - return $addon; |
|
45 | - } |
|
21 | + public static function addonApiV1( |
|
22 | + int $ID, |
|
23 | + string $main_file, |
|
24 | + string $name, |
|
25 | + string $display_name, |
|
26 | + string $namespace, |
|
27 | + string $min_core_version |
|
28 | + ): AddonApi { |
|
29 | + $addon_collection = AddonApiFactory::getAddonCollection(); |
|
30 | + $slug = AddonApiFactory::getSlugFromMainfile($main_file); |
|
31 | + if ($addon_collection->hasAddon($slug)) { |
|
32 | + return $addon_collection->getAddon($slug); |
|
33 | + } |
|
34 | + $addon = AddonApiFactory::createAddon(); |
|
35 | + $addon->setID($ID); |
|
36 | + $addon->setSlug($slug); |
|
37 | + $addon->setName($name); |
|
38 | + $addon->setDisplayName($display_name); |
|
39 | + $addon->setMainFile($main_file); |
|
40 | + $addon->setVersion(AddonApiFactory::getVersionFromMainfile($main_file)); |
|
41 | + $addon->setMinCoreVersion($min_core_version); |
|
42 | + $addon->setNamespace($namespace); |
|
43 | + $addon_collection->addAddon($addon); |
|
44 | + return $addon; |
|
45 | + } |
|
46 | 46 | |
47 | - private static function createAddon(): AddonApi |
|
48 | - { |
|
49 | - return LoaderFactory::getNew(AddonApi::class); |
|
50 | - } |
|
47 | + private static function createAddon(): AddonApi |
|
48 | + { |
|
49 | + return LoaderFactory::getNew(AddonApi::class); |
|
50 | + } |
|
51 | 51 | |
52 | - private static function getSlugFromMainfile(string $main_file): string |
|
53 | - { |
|
54 | - $plugin_basename = plugin_basename($main_file); |
|
55 | - return substr($plugin_basename, 0, strpos($plugin_basename, '/')); |
|
56 | - } |
|
52 | + private static function getSlugFromMainfile(string $main_file): string |
|
53 | + { |
|
54 | + $plugin_basename = plugin_basename($main_file); |
|
55 | + return substr($plugin_basename, 0, strpos($plugin_basename, '/')); |
|
56 | + } |
|
57 | 57 | |
58 | - private static function getVersionFromMainfile(string $main_file): string |
|
59 | - { |
|
60 | - $version_file = dirname($main_file) . '/.VERSION'; |
|
61 | - if (! is_readable($version_file)) { |
|
62 | - throw new InvalidFilePathException($version_file); |
|
63 | - } |
|
64 | - $file_handle = fopen($version_file, 'r'); |
|
65 | - if ($file_handle === false) { |
|
66 | - throw new InvalidDataTypeException( |
|
67 | - '$file_handle', |
|
68 | - false, |
|
69 | - esc_html__('file pointer resource', 'event_espresso') |
|
70 | - ); |
|
71 | - } |
|
72 | - $version = fgets($file_handle); |
|
73 | - if ($version === false) { |
|
74 | - throw new InvalidDataTypeException( |
|
75 | - '$version', |
|
76 | - false, |
|
77 | - esc_html__('add-on version', 'event_espresso') |
|
78 | - ); |
|
79 | - } |
|
80 | - if (! fclose($file_handle)) { |
|
81 | - throw new RuntimeException( |
|
82 | - esc_html__('Failed to close file handle', 'event_espresso') |
|
83 | - ); |
|
84 | - } |
|
85 | - return $version; |
|
86 | - } |
|
58 | + private static function getVersionFromMainfile(string $main_file): string |
|
59 | + { |
|
60 | + $version_file = dirname($main_file) . '/.VERSION'; |
|
61 | + if (! is_readable($version_file)) { |
|
62 | + throw new InvalidFilePathException($version_file); |
|
63 | + } |
|
64 | + $file_handle = fopen($version_file, 'r'); |
|
65 | + if ($file_handle === false) { |
|
66 | + throw new InvalidDataTypeException( |
|
67 | + '$file_handle', |
|
68 | + false, |
|
69 | + esc_html__('file pointer resource', 'event_espresso') |
|
70 | + ); |
|
71 | + } |
|
72 | + $version = fgets($file_handle); |
|
73 | + if ($version === false) { |
|
74 | + throw new InvalidDataTypeException( |
|
75 | + '$version', |
|
76 | + false, |
|
77 | + esc_html__('add-on version', 'event_espresso') |
|
78 | + ); |
|
79 | + } |
|
80 | + if (! fclose($file_handle)) { |
|
81 | + throw new RuntimeException( |
|
82 | + esc_html__('Failed to close file handle', 'event_espresso') |
|
83 | + ); |
|
84 | + } |
|
85 | + return $version; |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | - public static function getAddonCollection(): AddonCollection |
|
90 | - { |
|
91 | - return LoaderFactory::getShared(AddonCollection::class); |
|
92 | - } |
|
89 | + public static function getAddonCollection(): AddonCollection |
|
90 | + { |
|
91 | + return LoaderFactory::getShared(AddonCollection::class); |
|
92 | + } |
|
93 | 93 | } |
@@ -57,8 +57,8 @@ discard block |
||
57 | 57 | |
58 | 58 | private static function getVersionFromMainfile(string $main_file): string |
59 | 59 | { |
60 | - $version_file = dirname($main_file) . '/.VERSION'; |
|
61 | - if (! is_readable($version_file)) { |
|
60 | + $version_file = dirname($main_file).'/.VERSION'; |
|
61 | + if ( ! is_readable($version_file)) { |
|
62 | 62 | throw new InvalidFilePathException($version_file); |
63 | 63 | } |
64 | 64 | $file_handle = fopen($version_file, 'r'); |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | esc_html__('add-on version', 'event_espresso') |
78 | 78 | ); |
79 | 79 | } |
80 | - if (! fclose($file_handle)) { |
|
80 | + if ( ! fclose($file_handle)) { |
|
81 | 81 | throw new RuntimeException( |
82 | 82 | esc_html__('Failed to close file handle', 'event_espresso') |
83 | 83 | ); |