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 array |
||
26 | */ |
||
27 | public $middlewareCollection = []; |
||
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) |
||
60 | |||
61 | /** |
||
62 | * @param array $config |
||
63 | */ |
||
64 | public function setConfig($config) |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getConfig() |
||
76 | |||
77 | /** |
||
78 | * @param object|array $middleware |
||
79 | */ |
||
80 | public function setMiddleware($middleware) |
||
86 | |||
87 | /** |
||
88 | * @param MiddlewareInterface $middleware |
||
89 | */ |
||
90 | public function addMiddleware(MiddlewareInterface $middleware) |
||
94 | |||
95 | /** |
||
96 | * @param object|array $matcher |
||
97 | */ |
||
98 | public function setMatcher($matcher) |
||
104 | |||
105 | /** |
||
106 | * @param string $matcher |
||
107 | */ |
||
108 | public function addMatcher($matcher) |
||
112 | |||
113 | /** |
||
114 | * @description main function |
||
115 | */ |
||
116 | public function run() |
||
131 | |||
132 | /** |
||
133 | * @description call the middleware before and after the target |
||
134 | * @param $action |
||
135 | */ |
||
136 | public function callMiddleware($action) |
||
148 | |||
149 | /** |
||
150 | * @param null $url |
||
151 | */ |
||
152 | public function setUrl($url = null) |
||
158 | |||
159 | /** |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function match() |
||
169 | |||
170 | /** |
||
171 | * @description call the target for the request uri |
||
172 | */ |
||
173 | public function callTarget() |
||
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: