1 | <?php |
||
10 | class Router |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var Route |
||
15 | */ |
||
16 | public $route; |
||
17 | /** |
||
18 | * @var RouteCollection |
||
19 | */ |
||
20 | public $collection; |
||
21 | /** |
||
22 | * @var ResponseInterface |
||
23 | */ |
||
24 | public $response; |
||
25 | /** |
||
26 | * @var |
||
27 | */ |
||
28 | public $middleware; |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | public $matcher = []; |
||
33 | /** |
||
34 | * @var |
||
35 | */ |
||
36 | public $dispatcher; |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $config = [ |
||
41 | 'templateExtension' => ['.html', '.php', '.json', '.xml'], |
||
42 | 'templateCallback' => [], |
||
43 | 'di' => '', |
||
44 | 'generateRoutesPath' => false, |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * @param RouteCollection $collection |
||
49 | * @param ResponseInterface $response |
||
50 | * @param Route $route |
||
51 | */ |
||
52 | public function __construct(RouteCollection $collection,ResponseInterface $response = null,Route $route = null) |
||
53 | { |
||
54 | $this->collection = $collection; |
||
55 | $this->response = is_null($response)? new Response() : $response; |
||
56 | $this->response->setStatusCode(404); |
||
57 | $this->route = is_null($route)? new Route() : $route; |
||
58 | $this->middleware = new Middleware($this); |
||
59 | $this->config['di'] = function($class){ |
||
60 | return new $class; |
||
61 | }; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param array $config |
||
66 | */ |
||
67 | public function setConfig($config) |
||
68 | { |
||
69 | $this->config = array_merge($this->config, $config); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | public function getConfig() |
||
76 | { |
||
77 | return $this->config; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param object|array $matcher |
||
82 | */ |
||
83 | public function setMatcher($matcher){ |
||
84 | if(is_object($matcher)) |
||
85 | $matcher = [$matcher]; |
||
86 | $this->matcher = $matcher; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $matcher |
||
91 | */ |
||
92 | public function addMatcher($matcher){ |
||
93 | $this->matcher[] = $matcher; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @description main function |
||
98 | */ |
||
99 | public function run() |
||
100 | { |
||
101 | $this->setUrl(); |
||
102 | if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
||
103 | if ($this->match()) { |
||
104 | $this->callTarget(); |
||
105 | } |
||
106 | $this->callResponse(); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param null $url |
||
111 | */ |
||
112 | public function setUrl($url = null) |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function match() |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | */ |
||
134 | public function callTarget() |
||
144 | |||
145 | /** |
||
146 | * @param array $responses |
||
147 | */ |
||
148 | public function setResponses($responses = []) |
||
149 | { |
||
150 | $this->route->addDetail('response_templates', $responses); |
||
152 | |||
153 | /** |
||
154 | * @description set response code |
||
155 | */ |
||
156 | public function callResponse() |
||
173 | } |
||
174 |
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: