1 | <?php |
||
9 | class Router |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var Route |
||
14 | */ |
||
15 | public $route; |
||
16 | /** |
||
17 | * @var RouteCollection |
||
18 | */ |
||
19 | public $collection; |
||
20 | /** |
||
21 | * @var ResponseInterface |
||
22 | */ |
||
23 | public $response; |
||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | public $middleware; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | public $matcher = []; |
||
32 | /** |
||
33 | * @var |
||
34 | */ |
||
35 | public $dispatcher; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $config = [ |
||
40 | 'templateExtension' => ['.html', '.php', '.json', '.xml'], |
||
41 | 'templateCallback' => [], |
||
42 | 'di' => '', |
||
43 | 'generateRoutesPath' => false, |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @param RouteCollection $collection |
||
48 | * @param ResponseInterface $response |
||
49 | * @param Route $route |
||
50 | */ |
||
51 | public function __construct(RouteCollection $collection,ResponseInterface $response = null,Route $route = null) |
||
61 | |||
62 | /** |
||
63 | * @param array $config |
||
64 | */ |
||
65 | public function setConfig($config) |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getConfig() |
||
77 | |||
78 | /** |
||
79 | * @param object|array $matcher |
||
80 | */ |
||
81 | public function setMatcher($matcher){ |
||
86 | |||
87 | /** |
||
88 | * @param string $matcher |
||
89 | */ |
||
90 | public function addMatcher($matcher){ |
||
93 | |||
94 | /** |
||
95 | * @description main function |
||
96 | */ |
||
97 | public function run() |
||
107 | |||
108 | /** |
||
109 | * @param null $url |
||
110 | */ |
||
111 | public function setUrl($url = null) |
||
117 | |||
118 | /** |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function match() |
||
129 | |||
130 | /** |
||
131 | * |
||
132 | */ |
||
133 | public function callTarget() |
||
143 | |||
144 | /** |
||
145 | * @description handle middleware |
||
146 | */ |
||
147 | private function handle() |
||
155 | |||
156 | /** |
||
157 | * @param array $responses |
||
158 | */ |
||
159 | public function setResponses($responses = []) |
||
163 | |||
164 | /** |
||
165 | * @description set response code |
||
166 | */ |
||
167 | public function callResponse() |
||
183 | } |
||
184 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: