|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ReRoute\Route; |
|
4
|
|
|
|
|
5
|
|
|
use ReRoute\Modifier\AbstractRouteModifier; |
|
6
|
|
|
use ReRoute\ParameterStore; |
|
7
|
|
|
use ReRoute\RequestContext; |
|
8
|
|
|
use ReRoute\RouteMatch; |
|
9
|
|
|
use ReRoute\Template\UrlTemplate; |
|
10
|
|
|
use ReRoute\Url; |
|
11
|
|
|
use ReRoute\UrlBuilder; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @package ReRoute |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractRoute { |
|
19
|
|
|
|
|
20
|
|
|
use ParameterStore; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var AbstractRoute |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $parentRoute; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var UrlTemplate |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $urlTemplate; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var AbstractRouteModifier[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $modifiers = []; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param UrlTemplate|null $urlTemplate |
|
40
|
|
|
*/ |
|
41
|
129 |
|
public function __construct(UrlTemplate $urlTemplate = null) { |
|
42
|
129 |
|
if ($urlTemplate !== null) { |
|
43
|
69 |
|
$this->setUrlTemplate($urlTemplate); |
|
44
|
69 |
|
} |
|
45
|
129 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param RequestContext $requestContext |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
90 |
|
protected function matchUrlTemplate(RequestContext $requestContext) { |
|
53
|
90 |
|
if ($this->urlTemplate == null) { |
|
54
|
18 |
|
return true; |
|
55
|
|
|
} |
|
56
|
87 |
|
return $this->urlTemplate->isMatched($requestContext); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param RequestContext $requestContext |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
90 |
|
protected function matchModifiers(RequestContext $requestContext) { |
|
66
|
90 |
|
if (empty($this->modifiers)) { |
|
67
|
84 |
|
return true; |
|
68
|
|
|
} |
|
69
|
33 |
|
foreach ($this->modifiers as $modifier) { |
|
70
|
33 |
|
$modifierResult = $modifier->isMatched($requestContext); |
|
71
|
33 |
|
if (false === $modifierResult) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
33 |
|
} |
|
75
|
33 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param RequestContext $requestContext |
|
81
|
|
|
* |
|
82
|
|
|
* @return RouteMatch|bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public abstract function doMatch(RequestContext $requestContext); |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param RequestContext $requestContext |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
90 |
|
protected function isMatched(RequestContext $requestContext) { |
|
92
|
90 |
|
if (false === $this->matchModifiers($requestContext)) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
90 |
|
if (false === $this->matchUrlTemplate($requestContext)) { |
|
96
|
57 |
|
return false; |
|
97
|
|
|
} |
|
98
|
54 |
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
27 |
|
public function build(Url $url, UrlBuilder $urlBuilder) { |
|
106
|
27 |
|
if (!empty($this->parentRoute)) { |
|
107
|
21 |
|
$this->parentRoute->build($url, $urlBuilder); |
|
108
|
21 |
|
} |
|
109
|
27 |
|
if (!empty($this->urlTemplate)) { |
|
110
|
24 |
|
$this->urlTemplate->build($url, $urlBuilder); |
|
111
|
24 |
|
} |
|
112
|
27 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param Url $url |
|
117
|
|
|
* @param UrlBuilder $urlBuilder |
|
118
|
|
|
*/ |
|
119
|
24 |
|
public function buildModifiers(Url $url, UrlBuilder $urlBuilder) { |
|
120
|
24 |
|
$modifiers = $this->getModifiers(); |
|
121
|
24 |
|
if (!empty($modifiers)) { |
|
122
|
|
|
/** @var AbstractRouteModifier[] $modifiers */ |
|
123
|
15 |
|
$modifiers = array_reverse($modifiers); |
|
124
|
15 |
|
foreach ($modifiers as $modifier) { |
|
125
|
15 |
|
$modifier->build($url, $urlBuilder); |
|
126
|
15 |
|
} |
|
127
|
15 |
|
} |
|
128
|
|
|
|
|
129
|
24 |
|
if ($parentRoute = $this->getParentRoute()) { |
|
130
|
18 |
|
$parentRoute->buildModifiers($url, $urlBuilder); |
|
131
|
18 |
|
} |
|
132
|
24 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param RouteMatch $routeMatch |
|
137
|
|
|
* @return RouteMatch |
|
138
|
|
|
*/ |
|
139
|
42 |
|
protected function storeParametersToRouteMatch(RouteMatch $routeMatch) { |
|
140
|
42 |
|
foreach ($this->getParameters() as $key => $value) { |
|
141
|
6 |
|
$routeMatch->set($key, $value); |
|
142
|
42 |
|
} |
|
143
|
42 |
|
foreach ($this->getModifiers() as $modifier) { |
|
144
|
15 |
|
foreach ($modifier->getParameters() as $key => $value) { |
|
145
|
12 |
|
$routeMatch->set($key, $value); |
|
146
|
15 |
|
} |
|
147
|
42 |
|
} |
|
148
|
42 |
|
if ($urlTemplate = $this->getUrlTemplate()) { |
|
149
|
39 |
|
foreach ($urlTemplate->getParameters() as $key => $value) { |
|
150
|
12 |
|
$routeMatch->set($key, $value); |
|
151
|
39 |
|
} |
|
152
|
39 |
|
} |
|
153
|
42 |
|
$parentRoute = $this->getParentRoute(); |
|
154
|
42 |
|
while (!empty($parentRoute)) { |
|
155
|
21 |
|
$parentRoute->storeParametersToRouteMatch($routeMatch); |
|
156
|
21 |
|
$parentRoute = $parentRoute->getParentRoute(); |
|
157
|
21 |
|
} |
|
158
|
42 |
|
return $routeMatch; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return UrlBuilder |
|
164
|
|
|
*/ |
|
165
|
30 |
|
public function getUrl() { |
|
166
|
30 |
|
return new UrlBuilder($this); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return UrlTemplate |
|
172
|
|
|
*/ |
|
173
|
66 |
|
public function getUrlTemplate() { |
|
174
|
66 |
|
return $this->urlTemplate; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param UrlTemplate $template |
|
180
|
|
|
* @return $this |
|
181
|
|
|
*/ |
|
182
|
105 |
|
public function setUrlTemplate(UrlTemplate $template) { |
|
183
|
105 |
|
$this->urlTemplate = $template; |
|
184
|
105 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return AbstractRouteModifier[] |
|
190
|
|
|
*/ |
|
191
|
69 |
|
public function getModifiers() { |
|
192
|
69 |
|
return $this->modifiers; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param AbstractRouteModifier $modifier |
|
198
|
|
|
* |
|
199
|
|
|
* @return $this |
|
200
|
|
|
*/ |
|
201
|
48 |
|
public function addModifier(AbstractRouteModifier $modifier) { |
|
202
|
48 |
|
$this->modifiers[] = $modifier; |
|
203
|
48 |
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param AbstractRoute $route |
|
209
|
|
|
* |
|
210
|
|
|
* @return $this |
|
211
|
|
|
*/ |
|
212
|
54 |
|
public function setParentRoute($route) { |
|
213
|
54 |
|
$this->parentRoute = $route; |
|
214
|
54 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return AbstractRoute |
|
220
|
|
|
*/ |
|
221
|
69 |
|
public function getParentRoute() { |
|
222
|
69 |
|
return $this->parentRoute; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
} |