1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc; |
11
|
|
|
|
12
|
|
|
use Aura\Router\Matcher; |
13
|
|
|
use Aura\Router\RouterContainer; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Slick\Http\Server\AbstractMiddleware; |
17
|
|
|
use Slick\Http\Server\MiddlewareInterface; |
18
|
|
|
use Slick\Mvc\Router\RouteBuilder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HTTP Request Router |
22
|
|
|
* |
23
|
|
|
* @package Slick\Mvc |
24
|
|
|
* @author Filipe Silva <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Router extends AbstractMiddleware implements MiddlewareInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RouterContainer |
31
|
|
|
*/ |
32
|
|
|
protected $routerContainer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Matcher |
36
|
|
|
*/ |
37
|
|
|
protected $matcher; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RouteBuilder |
41
|
|
|
*/ |
42
|
|
|
protected $routeBuilder; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $routeFile; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns route container |
51
|
|
|
* |
52
|
|
|
* @return RouterContainer |
53
|
|
|
*/ |
54
|
4 |
|
public function getRouterContainer() |
55
|
|
|
{ |
56
|
4 |
|
if (null === $this->routerContainer) { |
57
|
2 |
|
$this->setRouterContainer(new RouterContainer()); |
58
|
2 |
|
$this->routerContainer |
59
|
2 |
|
->setMapBuilder([$this->getRouteBuilder(), 'build']); |
60
|
1 |
|
} |
61
|
4 |
|
return $this->routerContainer; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets router container |
66
|
|
|
* |
67
|
|
|
* @param RouterContainer $routerContainer |
68
|
|
|
* |
69
|
|
|
* @return self|$this|Router |
70
|
|
|
*/ |
71
|
4 |
|
public function setRouterContainer(RouterContainer $routerContainer) |
72
|
|
|
{ |
73
|
4 |
|
$this->routerContainer = $routerContainer; |
74
|
4 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Handles a Request and updated the response |
79
|
|
|
* |
80
|
|
|
* @param ServerRequestInterface $request |
81
|
|
|
* @param ResponseInterface $response |
82
|
|
|
* |
83
|
|
|
* @return ResponseInterface |
84
|
|
|
*/ |
85
|
2 |
|
public function handle( |
86
|
|
|
ServerRequestInterface $request, ResponseInterface $response |
87
|
|
|
) |
88
|
|
|
{ |
89
|
2 |
|
$route = $this->getMatcher()->match($request); |
90
|
2 |
|
$request = $request->withAttribute('route', $route); |
91
|
2 |
|
return $this->executeNext($request, $response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets route matcher for this router |
96
|
|
|
* |
97
|
|
|
* @return Matcher |
98
|
|
|
*/ |
99
|
4 |
|
public function getMatcher() |
100
|
|
|
{ |
101
|
4 |
|
if (null === $this->matcher) { |
102
|
2 |
|
$this->setMatcher($this->getRouterContainer()->getMatcher()); |
103
|
1 |
|
} |
104
|
4 |
|
return $this->matcher; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the route matcher for current used in Router::handle() |
109
|
|
|
* |
110
|
|
|
* @param Matcher $matcher |
111
|
|
|
* |
112
|
|
|
* @return $this|self|Router |
113
|
|
|
*/ |
114
|
4 |
|
public function setMatcher(Matcher $matcher) |
115
|
|
|
{ |
116
|
4 |
|
$this->matcher = $matcher; |
117
|
4 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get route builder |
122
|
|
|
* |
123
|
|
|
* @return RouteBuilder |
124
|
|
|
*/ |
125
|
4 |
|
public function getRouteBuilder() |
126
|
|
|
{ |
127
|
4 |
|
if (null == $this->routeBuilder) { |
128
|
2 |
|
$this->setRouteBuilder(new RouteBuilder($this->getRouteFile())); |
129
|
1 |
|
} |
130
|
4 |
|
return $this->routeBuilder; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set route builder |
135
|
|
|
* |
136
|
|
|
* @param RouteBuilder $routeBuilder |
137
|
|
|
* @return Router |
138
|
|
|
*/ |
139
|
4 |
|
public function setRouteBuilder($routeBuilder) |
140
|
|
|
{ |
141
|
4 |
|
$this->routeBuilder = $routeBuilder; |
142
|
4 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get route file full path |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
2 |
|
public function getRouteFile() |
151
|
|
|
{ |
152
|
2 |
|
return $this->routeFile; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set route definition file |
157
|
|
|
* |
158
|
|
|
* @param string $routeFile |
159
|
|
|
* @return Router |
160
|
|
|
*/ |
161
|
8 |
|
public function setRouteFile($routeFile) |
162
|
|
|
{ |
163
|
8 |
|
$this->routeFile = $routeFile; |
164
|
8 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
} |