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