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
|
22 |
|
public function __construct($basepath = null) |
121
|
|
|
{ |
122
|
22 |
|
$this->basepath = $basepath; |
123
|
|
|
|
124
|
22 |
|
$this->setLoggerFactory([$this, 'loggerFactory']); |
125
|
|
|
|
126
|
22 |
|
$this->setRouteFactory([$this, 'routeFactory']); |
127
|
|
|
|
128
|
22 |
|
$this->setMapFactory([$this, 'mapFactory']); |
129
|
|
|
|
130
|
22 |
|
$this->setMapBuilder([$this, 'buildMap']); |
131
|
22 |
|
} |
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
|
22 |
|
protected function routeFactory() |
153
|
|
|
{ |
154
|
22 |
|
return new Route(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* mapFactory |
159
|
|
|
* |
160
|
|
|
* @return mixed |
161
|
|
|
* |
162
|
|
|
* @access protected |
163
|
|
|
*/ |
164
|
22 |
|
protected function mapFactory() |
165
|
|
|
{ |
166
|
22 |
|
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
|
22 |
|
protected function buildMap(Map $map) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
// do nothing |
181
|
22 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* |
185
|
|
|
* Sets the logger factory. |
186
|
|
|
* |
187
|
|
|
* @param callable $loggerFactory The logger factory. |
188
|
|
|
* |
189
|
|
|
* @return null |
190
|
|
|
* |
191
|
|
|
*/ |
192
|
22 |
|
public function setLoggerFactory(callable $loggerFactory) |
193
|
|
|
{ |
194
|
22 |
|
$this->loggerFactory = $loggerFactory; |
195
|
22 |
|
} |
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
|
22 |
|
public function setRouteFactory(callable $routeFactory) |
207
|
|
|
{ |
208
|
22 |
|
$this->routeFactory = $routeFactory; |
209
|
22 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* |
213
|
|
|
* Sets the map factory. |
214
|
|
|
* |
215
|
|
|
* @param callable $mapFactory The map factory. |
216
|
|
|
* |
217
|
|
|
* @return null |
218
|
|
|
* |
219
|
|
|
*/ |
220
|
22 |
|
public function setMapFactory(callable $mapFactory) |
221
|
|
|
{ |
222
|
22 |
|
$this->mapFactory = $mapFactory; |
223
|
22 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* |
227
|
|
|
* Sets the map builder. |
228
|
|
|
* |
229
|
|
|
* @param callable $mapBuilder The map builder. |
230
|
|
|
* |
231
|
|
|
* @return null |
232
|
|
|
* |
233
|
|
|
*/ |
234
|
22 |
|
public function setMapBuilder(callable $mapBuilder) |
235
|
|
|
{ |
236
|
22 |
|
$this->mapBuilder = $mapBuilder; |
|
|
|
|
237
|
22 |
|
} |
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
|
22 |
|
public function getMap() |
248
|
|
|
{ |
249
|
22 |
|
if (! $this->map) { |
250
|
22 |
|
$this->map = call_user_func($this->mapFactory); |
251
|
22 |
|
call_user_func($this->mapBuilder, $this->map); |
252
|
22 |
|
} |
253
|
22 |
|
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
|
12 |
|
public function getGenerator() |
283
|
|
|
{ |
284
|
12 |
|
if (! $this->generator) { |
285
|
12 |
|
$this->generator = new Generator($this->getMap(), $this->basepath); |
286
|
12 |
|
} |
287
|
12 |
|
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
|
22 |
|
public function getRoute() |
313
|
|
|
{ |
314
|
22 |
|
if (! $this->route) { |
315
|
22 |
|
$this->route = call_user_func($this->routeFactory); |
316
|
22 |
|
} |
317
|
22 |
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.