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 | public const EMPTY = 8; |
||
35 | |||
36 | public const OPTIONS = 'OPTIONS'; |
||
37 | public const HEAD = 'HEAD'; |
||
38 | public const GET = 'GET'; |
||
39 | public const POST = 'POST'; |
||
40 | public const PUT = 'PUT'; |
||
41 | public const DELETE = 'DELETE'; |
||
42 | public const PATCH = 'PATCH'; |
||
43 | |||
44 | /** |
||
45 | * Routes array to be registered. |
||
46 | * Some routes may have aliases to be used in templating system |
||
47 | * Route item can be defined using array key as an alias key. |
||
48 | * Each route item is an array has items respectively: Request Method, Request Uri, Controller/Action, Return Type. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $routes = []; |
||
53 | |||
54 | /** |
||
55 | * Aliases array to be registered. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $aliases = []; |
||
60 | |||
61 | /** |
||
62 | * HTTP request Method |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $method; |
||
67 | |||
68 | /** |
||
69 | * Request Uri |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $requestedPath; |
||
74 | |||
75 | /** |
||
76 | * Default return type if not noted in the $routes |
||
77 | * |
||
78 | * @var int |
||
79 | */ |
||
80 | private $defaultReturnType; |
||
81 | |||
82 | /** |
||
83 | * @var null|string |
||
84 | */ |
||
85 | private $cacheFile; |
||
86 | |||
87 | /** |
||
88 | * Valid Request Methods array. |
||
89 | * Make sures about requested methods. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | private static $validRequestMethods = [ |
||
94 | 'GET', |
||
95 | 'OPTIONS', |
||
96 | 'HEAD', |
||
97 | 'POST', |
||
98 | 'PUT', |
||
99 | 'DELETE', |
||
100 | 'PATCH' |
||
101 | ]; |
||
102 | |||
103 | /* |
||
104 | * Router constructor. |
||
105 | * Create new router. |
||
106 | |||
107 | */ |
||
108 | public function __construct( |
||
121 | |||
122 | public static function createWithServerRequestInterface(int $defaultReturnType, ServerRequestInterface $request) |
||
126 | |||
127 | public function withDefaultReturnType(int $defaultReturnType) : self |
||
133 | |||
134 | public function withSubFolder(string $folder) : self |
||
140 | |||
141 | public function withCacheFile(?string $fileName=null) : self |
||
147 | |||
148 | /* |
||
149 | * Remove sub folder from requestedPath if defined |
||
150 | */ |
||
151 | private function extractFolder(string $requestPath, string $folder) : string |
||
161 | |||
162 | public function add( |
||
180 | |||
181 | |||
182 | public function __call(string $method, array $args) : void |
||
193 | |||
194 | private function determineReturnType(?int $returnType) : int |
||
201 | |||
202 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
209 | |||
210 | public function getRoute() : Route |
||
218 | } |
||
219 |