33
33
class ChainRouter implements ChainRouterInterface, WarmableInterface
34
34
{
35
35
/**
36
- * @var RequestContext
36
+ * @var RequestContext|null
37
37
*/
38
38
private $ context ;
39
39
40
40
/**
41
41
* Array of arrays of routers grouped by priority.
42
42
*
43
- * @var array
43
+ * @var RouterInterface[][] Priority => RouterInterface[]
44
44
*/
45
45
private $ routers = [];
46
46
47
47
/**
48
- * @var RouterInterface[] Array of routers, sorted by priority
48
+ * @var RouterInterface[] List of routers, sorted by priority
49
49
*/
50
50
private $ sortedRouters = [];
51
51
@@ -72,6 +72,10 @@ public function __construct(LoggerInterface $logger = null)
72
72
*/
73
73
public function getContext ()
74
74
{
75
+ if (!$ this ->context ) {
76
+ $ this ->context = new RequestContext ();
77
+ }
78
+
75
79
return $ this ->context ;
76
80
}
77
81
@@ -103,11 +107,10 @@ public function all()
103
107
104
108
// setContext() is done here instead of in add() to avoid fatal errors when clearing and warming up caches
105
109
// See https://github.com/symfony-cmf/Routing/pull/18
106
- $ context = $ this ->getContext ();
107
- if (null !== $ context ) {
110
+ if (null !== $ this ->context ) {
108
111
foreach ($ this ->sortedRouters as $ router ) {
109
112
if ($ router instanceof RequestContextAwareInterface) {
110
- $ router ->setContext ($ context );
113
+ $ router ->setContext ($ this -> context );
111
114
}
112
115
}
113
116
}
@@ -253,36 +256,34 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf
253
256
/**
254
257
* Rebuild the request object from a URL with the help of the RequestContext.
255
258
*
256
- * If the request context is not set, this simply returns the request object built from $uri .
259
+ * If the request context is not set, this returns the request object built from $pathinfo .
257
260
*
258
261
* @param string $pathinfo
259
262
*
260
263
* @return Request
261
264
*/
262
265
private function rebuildRequest ($ pathinfo )
263
266
{
264
- if (!$ this ->context ) {
265
- return Request::create ('http://localhost ' .$ pathinfo );
266
- }
267
+ $ context = $ this ->getContext ();
267
268
268
269
$ uri = $ pathinfo ;
269
270
270
271
$ server = [];
271
- if ($ this -> context ->getBaseUrl ()) {
272
- $ uri = $ this -> context ->getBaseUrl ().$ pathinfo ;
273
- $ server ['SCRIPT_FILENAME ' ] = $ this -> context ->getBaseUrl ();
274
- $ server ['PHP_SELF ' ] = $ this -> context ->getBaseUrl ();
272
+ if ($ context ->getBaseUrl ()) {
273
+ $ uri = $ context ->getBaseUrl ().$ pathinfo ;
274
+ $ server ['SCRIPT_FILENAME ' ] = $ context ->getBaseUrl ();
275
+ $ server ['PHP_SELF ' ] = $ context ->getBaseUrl ();
275
276
}
276
- $ host = $ this -> context ->getHost () ?: 'localhost ' ;
277
- if ('https ' === $ this -> context ->getScheme () && 443 !== $ this -> context ->getHttpsPort ()) {
278
- $ host .= ': ' .$ this -> context ->getHttpsPort ();
277
+ $ host = $ context ->getHost () ?: 'localhost ' ;
278
+ if ('https ' === $ context ->getScheme () && 443 !== $ context ->getHttpsPort ()) {
279
+ $ host .= ': ' .$ context ->getHttpsPort ();
279
280
}
280
- if ('http ' === $ this -> context ->getScheme () && 80 !== $ this -> context ->getHttpPort ()) {
281
- $ host .= ': ' .$ this -> context ->getHttpPort ();
281
+ if ('http ' === $ context ->getScheme () && 80 !== $ context ->getHttpPort ()) {
282
+ $ host .= ': ' .$ context ->getHttpPort ();
282
283
}
283
- $ uri = $ this -> context ->getScheme ().':// ' .$ host .$ uri .'? ' .$ this -> context ->getQueryString ();
284
+ $ uri = $ context ->getScheme ().':// ' .$ host .$ uri .'? ' .$ context ->getQueryString ();
284
285
285
- return Request::create ($ uri , $ this -> context ->getMethod (), $ this -> context ->getParameters (), [], [], $ server );
286
+ return Request::create ($ uri , $ context ->getMethod (), $ context ->getParameters (), [], [], $ server );
286
287
}
287
288
288
289
private function getErrorMessage ($ name , $ router = null , $ parameters = null )
0 commit comments