Skip to content

Commit f92c197

Browse files
Tobias Alex-Petersendbu
authored andcommitted
Initialize request context if none has been set.
Symfony core router never returns null in getContext() and some third party code relies on that behaviour.
1 parent 4b856db commit f92c197

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Changelog
44
2.1.0 (unreleased)
55
------------------
66

7-
* **2017-10-23**: Dropped php hhvm support
7+
* ChainRouter now returns a new RequestContext if none has been set, to be closer in behaviour to the Symfony router.
8+
* Dropped php hhvm support
89

910
2.0.3
1011
-----

src/ChainRouter.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@
3333
class ChainRouter implements ChainRouterInterface, WarmableInterface
3434
{
3535
/**
36-
* @var RequestContext
36+
* @var RequestContext|null
3737
*/
3838
private $context;
3939

4040
/**
4141
* Array of arrays of routers grouped by priority.
4242
*
43-
* @var array
43+
* @var RouterInterface[][] Priority => RouterInterface[]
4444
*/
4545
private $routers = [];
4646

4747
/**
48-
* @var RouterInterface[] Array of routers, sorted by priority
48+
* @var RouterInterface[] List of routers, sorted by priority
4949
*/
5050
private $sortedRouters = [];
5151

@@ -72,6 +72,10 @@ public function __construct(LoggerInterface $logger = null)
7272
*/
7373
public function getContext()
7474
{
75+
if (!$this->context) {
76+
$this->context = new RequestContext();
77+
}
78+
7579
return $this->context;
7680
}
7781

@@ -103,11 +107,10 @@ public function all()
103107

104108
// setContext() is done here instead of in add() to avoid fatal errors when clearing and warming up caches
105109
// See https://github.com/symfony-cmf/Routing/pull/18
106-
$context = $this->getContext();
107-
if (null !== $context) {
110+
if (null !== $this->context) {
108111
foreach ($this->sortedRouters as $router) {
109112
if ($router instanceof RequestContextAwareInterface) {
110-
$router->setContext($context);
113+
$router->setContext($this->context);
111114
}
112115
}
113116
}
@@ -253,36 +256,34 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf
253256
/**
254257
* Rebuild the request object from a URL with the help of the RequestContext.
255258
*
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.
257260
*
258261
* @param string $pathinfo
259262
*
260263
* @return Request
261264
*/
262265
private function rebuildRequest($pathinfo)
263266
{
264-
if (!$this->context) {
265-
return Request::create('http://localhost'.$pathinfo);
266-
}
267+
$context = $this->getContext();
267268

268269
$uri = $pathinfo;
269270

270271
$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();
275276
}
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();
279280
}
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();
282283
}
283-
$uri = $this->context->getScheme().'://'.$host.$uri.'?'.$this->context->getQueryString();
284+
$uri = $context->getScheme().'://'.$host.$uri.'?'.$context->getQueryString();
284285

285-
return Request::create($uri, $this->context->getMethod(), $this->context->getParameters(), [], [], $server);
286+
return Request::create($uri, $context->getMethod(), $context->getParameters(), [], [], $server);
286287
}
287288

288289
private function getErrorMessage($name, $router = null, $parameters = null)

0 commit comments

Comments
 (0)