|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Router; |
|
10
|
|
|
|
|
11
|
|
|
use Aura\Router\Rule; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* A library-specific container. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Aura.Router |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
class RouterContainer |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* Generates paths from routes. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Generator |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $generator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* Logs activity in the Matcher. |
|
35
|
|
|
* |
|
36
|
|
|
* @var Psr\Log\LoggerInterface |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $logger; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* A factory to create the logger. |
|
44
|
|
|
* |
|
45
|
|
|
* @var callable |
|
46
|
|
|
* |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $loggerFactory; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
* A route map. |
|
53
|
|
|
* |
|
54
|
|
|
* @var Map |
|
55
|
|
|
* |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $map; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* A factory to create the map. |
|
62
|
|
|
* |
|
63
|
|
|
* @var callable |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $mapFactory; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* The route matcher. |
|
71
|
|
|
* |
|
72
|
|
|
* @var Matcher |
|
73
|
|
|
* |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $matcher; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* A proto-route for the map. |
|
80
|
|
|
* |
|
81
|
|
|
* @var Route |
|
82
|
|
|
* |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $route; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* |
|
88
|
|
|
* A factory to create the route. |
|
89
|
|
|
* |
|
90
|
|
|
* @var callable |
|
91
|
|
|
* |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $routeFactory; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* |
|
97
|
|
|
* An collection of route-matching rules to iterate through. |
|
98
|
|
|
* |
|
99
|
|
|
* @var RuleIterator |
|
100
|
|
|
* |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $ruleIterator; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* |
|
106
|
|
|
* The basepath to use for matching and generating. |
|
107
|
|
|
* |
|
108
|
|
|
* @var string |
|
109
|
|
|
* |
|
110
|
|
|
*/ |
|
111
|
|
|
protected $basepath; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* |
|
115
|
|
|
* Constructor. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $basepath The basepath to use for matching and generating. |
|
118
|
|
|
* |
|
119
|
|
|
*/ |
|
120
|
24 |
|
public function __construct($basepath = null) |
|
121
|
|
|
{ |
|
122
|
24 |
|
$this->basepath = $basepath; |
|
123
|
|
|
|
|
124
|
24 |
|
$this->setLoggerFactory([$this, 'loggerFactory']); |
|
125
|
|
|
|
|
126
|
24 |
|
$this->setRouteFactory([$this, 'routeFactory']); |
|
127
|
|
|
|
|
128
|
24 |
|
$this->setMapFactory([$this, 'mapFactory']); |
|
129
|
|
|
|
|
130
|
24 |
|
$this->setMapBuilder([$this, 'buildMap']); |
|
131
|
24 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Creates a Logger |
|
135
|
|
|
* |
|
136
|
|
|
* @return NullLogger |
|
137
|
|
|
* |
|
138
|
|
|
* @access protected |
|
139
|
|
|
*/ |
|
140
|
5 |
|
protected function loggerFactory() |
|
141
|
|
|
{ |
|
142
|
5 |
|
return new NullLogger(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Creates a new Route |
|
147
|
|
|
* |
|
148
|
|
|
* @return Route |
|
149
|
|
|
* |
|
150
|
|
|
* @access protected |
|
151
|
|
|
*/ |
|
152
|
24 |
|
protected function routeFactory() |
|
153
|
|
|
{ |
|
154
|
24 |
|
return new Route(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* mapFactory |
|
159
|
|
|
* |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
* |
|
162
|
|
|
* @access protected |
|
163
|
|
|
*/ |
|
164
|
24 |
|
protected function mapFactory() |
|
165
|
|
|
{ |
|
166
|
24 |
|
return new Map($this->getRoute()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Builds a map |
|
171
|
|
|
* |
|
172
|
|
|
* @param Map $map DESCRIPTION |
|
173
|
|
|
* |
|
174
|
|
|
* @return mixed |
|
175
|
|
|
* |
|
176
|
|
|
* @access protected |
|
177
|
|
|
*/ |
|
178
|
24 |
|
protected function buildMap(Map $map) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
// do nothing |
|
181
|
24 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* |
|
185
|
|
|
* Sets the logger factory. |
|
186
|
|
|
* |
|
187
|
|
|
* @param callable $loggerFactory The logger factory. |
|
188
|
|
|
* |
|
189
|
|
|
* @return null |
|
190
|
|
|
* |
|
191
|
|
|
*/ |
|
192
|
24 |
|
public function setLoggerFactory(callable $loggerFactory) |
|
193
|
|
|
{ |
|
194
|
24 |
|
$this->loggerFactory = $loggerFactory; |
|
195
|
24 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* |
|
199
|
|
|
* Sets the proto-route factory. |
|
200
|
|
|
* |
|
201
|
|
|
* @param callable $routeFactory The proto-route factory. |
|
202
|
|
|
* |
|
203
|
|
|
* @return null |
|
204
|
|
|
* |
|
205
|
|
|
*/ |
|
206
|
24 |
|
public function setRouteFactory(callable $routeFactory) |
|
207
|
|
|
{ |
|
208
|
24 |
|
$this->routeFactory = $routeFactory; |
|
209
|
24 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* |
|
213
|
|
|
* Sets the map factory. |
|
214
|
|
|
* |
|
215
|
|
|
* @param callable $mapFactory The map factory. |
|
216
|
|
|
* |
|
217
|
|
|
* @return null |
|
218
|
|
|
* |
|
219
|
|
|
*/ |
|
220
|
24 |
|
public function setMapFactory(callable $mapFactory) |
|
221
|
|
|
{ |
|
222
|
24 |
|
$this->mapFactory = $mapFactory; |
|
223
|
24 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* |
|
227
|
|
|
* Sets the map builder. |
|
228
|
|
|
* |
|
229
|
|
|
* @param callable $mapBuilder The map builder. |
|
230
|
|
|
* |
|
231
|
|
|
* @return null |
|
232
|
|
|
* |
|
233
|
|
|
*/ |
|
234
|
24 |
|
public function setMapBuilder(callable $mapBuilder) |
|
235
|
|
|
{ |
|
236
|
24 |
|
$this->mapBuilder = $mapBuilder; |
|
|
|
|
|
|
237
|
24 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* |
|
241
|
|
|
* Gets the shared Map instance. Creates it with the map factory, and runs |
|
242
|
|
|
* it through the map builder, on first call. |
|
243
|
|
|
* |
|
244
|
|
|
* @return Map |
|
245
|
|
|
* |
|
246
|
|
|
*/ |
|
247
|
24 |
|
public function getMap() |
|
248
|
|
|
{ |
|
249
|
24 |
|
if (! $this->map) { |
|
250
|
24 |
|
$this->map = call_user_func($this->mapFactory); |
|
251
|
24 |
|
call_user_func($this->mapBuilder, $this->map); |
|
252
|
24 |
|
} |
|
253
|
24 |
|
return $this->map; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* |
|
258
|
|
|
* Gets the shared Matcher instance. |
|
259
|
|
|
* |
|
260
|
|
|
* @return Matcher |
|
261
|
|
|
* |
|
262
|
|
|
*/ |
|
263
|
5 |
|
public function getMatcher() |
|
264
|
|
|
{ |
|
265
|
5 |
|
if (! $this->matcher) { |
|
266
|
5 |
|
$this->matcher = new Matcher( |
|
267
|
5 |
|
$this->getMap(), |
|
268
|
5 |
|
$this->getLogger(), |
|
269
|
5 |
|
$this->getRuleIterator() |
|
270
|
5 |
|
); |
|
271
|
5 |
|
} |
|
272
|
5 |
|
return $this->matcher; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* |
|
277
|
|
|
* Gets the shared Generator instance. |
|
278
|
|
|
* |
|
279
|
|
|
* @return Generator |
|
280
|
|
|
* |
|
281
|
|
|
*/ |
|
282
|
14 |
|
public function getGenerator() |
|
283
|
|
|
{ |
|
284
|
14 |
|
if (! $this->generator) { |
|
285
|
14 |
|
$this->generator = new Generator($this->getMap(), $this->basepath); |
|
286
|
14 |
|
} |
|
287
|
14 |
|
return $this->generator; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* |
|
292
|
|
|
* Gets the shared Logger instance. |
|
293
|
|
|
* |
|
294
|
|
|
* @return Logger |
|
295
|
|
|
* |
|
296
|
|
|
*/ |
|
297
|
5 |
|
public function getLogger() |
|
298
|
|
|
{ |
|
299
|
5 |
|
if (! $this->logger) { |
|
300
|
5 |
|
$this->logger = call_user_func($this->loggerFactory); |
|
301
|
5 |
|
} |
|
302
|
5 |
|
return $this->logger; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* |
|
307
|
|
|
* Gets the shared proto-route instance. |
|
308
|
|
|
* |
|
309
|
|
|
* @return Route |
|
310
|
|
|
* |
|
311
|
|
|
*/ |
|
312
|
24 |
|
public function getRoute() |
|
313
|
|
|
{ |
|
314
|
24 |
|
if (! $this->route) { |
|
315
|
24 |
|
$this->route = call_user_func($this->routeFactory); |
|
316
|
24 |
|
} |
|
317
|
24 |
|
return $this->route; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* |
|
322
|
|
|
* Gets the rule iterator instance. |
|
323
|
|
|
* |
|
324
|
|
|
* @return RuleIterator |
|
325
|
|
|
* |
|
326
|
|
|
*/ |
|
327
|
5 |
|
public function getRuleIterator() |
|
328
|
|
|
{ |
|
329
|
5 |
|
if (! $this->ruleIterator) { |
|
330
|
5 |
|
$this->ruleIterator = new Rule\RuleIterator([ |
|
|
|
|
|
|
331
|
5 |
|
new Rule\Secure(), |
|
332
|
5 |
|
new Rule\Host(), |
|
333
|
5 |
|
new Rule\Path($this->basepath), |
|
334
|
5 |
|
new Rule\Allows(), |
|
335
|
5 |
|
new Rule\Accepts(), |
|
336
|
5 |
|
]); |
|
337
|
5 |
|
} |
|
338
|
5 |
|
return $this->ruleIterator; |
|
|
|
|
|
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* |
|
343
|
|
|
* Gets a route generation helper |
|
344
|
|
|
* |
|
345
|
|
|
* @return Helper\Route |
|
346
|
|
|
* |
|
347
|
|
|
*/ |
|
348
|
1 |
|
public function getRouteHelper() |
|
349
|
|
|
{ |
|
350
|
1 |
|
return new Helper\Route($this->getGenerator()); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* |
|
355
|
|
|
* Gets a raw route generation helper |
|
356
|
|
|
* |
|
357
|
|
|
* @return Helper\RouteRaw |
|
358
|
|
|
* |
|
359
|
|
|
*/ |
|
360
|
1 |
|
public function getRouteRawHelper() |
|
361
|
|
|
{ |
|
362
|
1 |
|
return new Helper\RouteRaw($this->getGenerator()); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.