1 | <?php |
||
27 | final class Router |
||
28 | { |
||
29 | public const HTML = 1; |
||
30 | public const JSON = 2; |
||
31 | public const TEXT = 3; |
||
32 | public const XML = 4; |
||
33 | public const REDIRECT = 5; |
||
34 | public const DOWNLOAD = 6; |
||
35 | public const CUSTOM = 7; |
||
36 | |||
37 | public const OPTIONS = 'OPTIONS'; |
||
38 | public const HEAD = 'HEAD'; |
||
39 | public const GET = 'GET'; |
||
40 | public const POST = 'POST'; |
||
41 | public const PUT = 'PUT'; |
||
42 | public const DELETE = 'DELETE'; |
||
43 | public const PATCH = 'PATCH'; |
||
44 | |||
45 | /** |
||
46 | * Routes array to be registered. |
||
47 | * Some routes may have aliases to be used in templating system |
||
48 | * Route item can be defined using array key as an alias key. |
||
49 | * Each route item is an array has items respectively: Request Method, Request Uri, Controller/Action, Return Type. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $routes = []; |
||
54 | |||
55 | /** |
||
56 | * Aliases array to be registered. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $aliases = []; |
||
61 | |||
62 | /** |
||
63 | * HTTP request Method |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $method; |
||
68 | |||
69 | /** |
||
70 | * Request Uri |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $requestedPath; |
||
75 | |||
76 | /** |
||
77 | * Default return type if not noted in the $routes |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | private $defaultReturnType; |
||
82 | |||
83 | /** |
||
84 | * @var null|string |
||
85 | */ |
||
86 | private $cachedFile; |
||
87 | |||
88 | /** |
||
89 | * Valid Request Methods array. |
||
90 | * Make sures about requested methods. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | private static $validRequestMethods = [ |
||
95 | 'GET', |
||
96 | 'OPTIONS', |
||
97 | 'HEAD', |
||
98 | 'POST', |
||
99 | 'PUT', |
||
100 | 'DELETE', |
||
101 | 'PATCH' |
||
102 | ]; |
||
103 | |||
104 | /* |
||
105 | * Router constructor. |
||
106 | * Create new router. |
||
107 | |||
108 | */ |
||
109 | public function __construct( |
||
125 | |||
126 | /* |
||
127 | * Remove sub folder from requestedPath if defined |
||
128 | */ |
||
129 | private function extractFolder(string $requestPath, string $folder) : string |
||
139 | |||
140 | |||
141 | public function add( |
||
160 | |||
161 | |||
162 | public function __call(string $method, array $args) : void |
||
173 | |||
174 | |||
175 | private function determineReturnType(?int $returnType) : int |
||
182 | |||
183 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
190 | |||
191 | public function getRoute() : Route |
||
199 | } |
||
200 |