1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace Mezon\Router; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait StaticRoutes |
7
|
|
|
* |
8
|
|
|
* @package Router |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2021/09/27) |
11
|
|
|
* @copyright Copyright (c) 2021, http://aeon.su |
12
|
|
|
*/ |
13
|
|
|
trait StaticRoutes |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* List of static routes for all supported request methods |
18
|
|
|
* |
19
|
|
|
* @var array<string, array<string, array{0: string, 1:string}|callable|string>> |
20
|
|
|
*/ |
21
|
|
|
protected $staticRoutes = [ |
22
|
|
|
'GET' => [], |
23
|
|
|
'POST' => [], |
24
|
|
|
'PUT' => [], |
25
|
|
|
'DELETE' => [], |
26
|
|
|
'OPTION' => [], |
27
|
|
|
'PATCH' => [] |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Method sets $calledRoute |
32
|
|
|
* |
33
|
|
|
* @param string $calledRoute |
34
|
|
|
* called router |
35
|
|
|
*/ |
36
|
|
|
protected abstract function setCalledRoute(string $calledRoute): void; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Method returns static routes handlers for the specified request methods |
40
|
|
|
* |
41
|
|
|
* @param string $requestMethod |
42
|
|
|
* request method |
43
|
|
|
* @return array<string, array{0: string, 1:string}|callable|string> handlers |
44
|
|
|
*/ |
45
|
|
|
protected function getStaticRoutes(string $requestMethod): array |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$requestMethod = (string) $_SERVER['REQUEST_METHOD']; |
48
|
|
|
|
49
|
|
|
SupportedRequestMethods::validateRequestMethod($requestMethod); |
50
|
|
|
|
51
|
|
|
return $this->staticRoutes[$requestMethod]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Method returns route handler |
56
|
|
|
* |
57
|
|
|
* @param string $route |
58
|
|
|
* routes |
59
|
|
|
* @return array{0: string, 1: string}|callable|false|string route handler |
60
|
|
|
*/ |
61
|
|
|
protected function getStaticRouteProcessor(string $route) |
62
|
|
|
{ |
63
|
|
|
$processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']); |
64
|
|
|
|
65
|
|
|
if (isset($processors[$route])) { |
66
|
|
|
$this->setCalledRoute($route); |
67
|
|
|
|
68
|
|
|
return $processors[$route]; |
69
|
|
|
} else { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Method returns route handler |
76
|
|
|
* |
77
|
|
|
* @return array{0: string, 1: string}|callable|false|string route handler |
78
|
|
|
*/ |
79
|
|
|
protected function getUniversalRouteProcessor() |
80
|
|
|
{ |
81
|
|
|
$processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']); |
82
|
|
|
|
83
|
|
|
if (isset($processors['*'])) { |
84
|
|
|
$this->setCalledRoute('*'); |
85
|
|
|
|
86
|
|
|
return $processors['*']; |
87
|
|
|
} else { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.