1 | <?php |
||
13 | class Router |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var Route |
||
18 | */ |
||
19 | public $route; |
||
20 | /** |
||
21 | * @var RouteCollection |
||
22 | */ |
||
23 | public $collection; |
||
24 | /** |
||
25 | * @var ResponseInterface |
||
26 | */ |
||
27 | public $response; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | public $middlewareCollection = []; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | public $matcher = []; |
||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | public $dispatcher; |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $config = [ |
||
44 | 'templateExtension' => ['.html', '.php', '.json', '.xml'], |
||
45 | 'templateCallback' => [], |
||
46 | 'di' => '', |
||
47 | 'generateRoutesPath' => false, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @param RouteCollection $collection |
||
52 | * @param ResponseInterface $response |
||
53 | * @param Route $route |
||
54 | */ |
||
55 | public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null) |
||
56 | { |
||
57 | $this->collection = $collection; |
||
58 | $this->response = is_null($response) ? new Response() : $response; |
||
59 | $this->route = is_null($route) ? new Route() : $route; |
||
60 | $this->config['di'] = function ($class) { |
||
61 | return new $class; |
||
62 | }; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param array $config |
||
67 | */ |
||
68 | public function setConfig($config) |
||
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | */ |
||
76 | public function getConfig() |
||
80 | |||
81 | /** |
||
82 | * @param object|array $middleware |
||
83 | */ |
||
84 | public function setMiddleware($middleware) |
||
90 | |||
91 | /** |
||
92 | * @param MiddlewareInterface $middleware |
||
93 | */ |
||
94 | public function addMiddleware(MiddlewareInterface $middleware) |
||
98 | |||
99 | /** |
||
100 | * @param object|array $matcher |
||
101 | */ |
||
102 | public function setMatcher($matcher) |
||
108 | |||
109 | /** |
||
110 | * @param string $matcher |
||
111 | */ |
||
112 | public function addMatcher($matcher) |
||
116 | |||
117 | /** |
||
118 | * @description main function |
||
119 | */ |
||
120 | public function run() |
||
135 | |||
136 | /** |
||
137 | * @description call the middleware before and after the target |
||
138 | * @param $action |
||
139 | */ |
||
140 | private function callMiddleware($action) |
||
152 | |||
153 | /** |
||
154 | * @param null $url |
||
155 | */ |
||
156 | public function setUrl($url = null) |
||
162 | |||
163 | /** |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function match() |
||
173 | |||
174 | /** |
||
175 | * @description call the target for the request uri |
||
176 | */ |
||
177 | public function callTarget() |
||
187 | } |
||
188 |
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: