Skip to content

Commit 5cdaffe

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Routing] Fix XmlFileLoader exception message Sessions: configurable "use_strict_mode" option for NativeSessionStorage [FrameworkBundle] [Command] Clean bundle directory, fixes #23177 Reset redirectCount when throwing exception [TwigBundle] Remove template.xml services when templating is disabled add content-type header on exception response Embedding a response that combines expiration and validation, that should not defeat expiration on the combined response Fix two edge cases in ResponseCacheStrategy [Routing] Expose request in route conditions, if needed and possible [Routing] Expose request in route conditions, if needed and possible [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034 [Filesystem] added workaround in Filesystem::rename for PHP bug Add tests for ResponseCacheStrategy to document some more edge cases [HttpFoundation] added missing docs fixes #21606 [VarDumper] fixes [Security] fix switch user _exit without having current token
2 parents d428588 + ebb0b3e commit 5cdaffe

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private function parseConfigs(\DOMElement $node, $path)
250250
$condition = trim($n->textContent);
251251
break;
252252
default:
253-
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
253+
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement", "option" or "condition".', $n->localName, $path));
254254
}
255255
}
256256

Matcher/RedirectableUrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function match($pathinfo)
4949
protected function handleRouteRequirements($pathinfo, $name, Route $route)
5050
{
5151
// expression condition
52-
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request))) {
52+
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)))) {
5353
return array(self::REQUIREMENT_MISMATCH, null);
5454
}
5555

Matcher/TraceableUrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
105105

106106
// check condition
107107
if ($condition = $route->getCondition()) {
108-
if (!$this->getExpressionLanguage()->evaluate($condition, array('context' => $this->context, 'request' => $this->request))) {
108+
if (!$this->getExpressionLanguage()->evaluate($condition, array('context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)))) {
109109
$this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $condition), self::ROUTE_ALMOST_MATCHES, $name, $route);
110110

111111
continue;

Matcher/UrlMatcher.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function getAttributes(Route $route, $name, array $attributes)
207207
protected function handleRouteRequirements($pathinfo, $name, Route $route)
208208
{
209209
// expression condition
210-
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request))) {
210+
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)))) {
211211
return array(self::REQUIREMENT_MISMATCH, null);
212212
}
213213

@@ -248,4 +248,19 @@ protected function getExpressionLanguage()
248248

249249
return $this->expressionLanguage;
250250
}
251+
252+
/**
253+
* @internal
254+
*/
255+
protected function createRequest($pathinfo)
256+
{
257+
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
258+
return null;
259+
}
260+
261+
return Request::create($this->context->getScheme().'://'.$this->context->getHost().$this->context->getBaseUrl().$pathinfo, $this->context->getMethod(), $this->context->getParameters(), array(), array(), array(
262+
'SCRIPT_FILENAME' => $this->context->getBaseUrl(),
263+
'SCRIPT_NAME' => $this->context->getBaseUrl(),
264+
));
265+
}
251266
}

Tests/Matcher/UrlMatcherTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ public function testCondition()
337337
$matcher->match('/foo');
338338
}
339339

340+
public function testRequestCondition()
341+
{
342+
$coll = new RouteCollection();
343+
$route = new Route('/foo/{bar}');
344+
$route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"');
345+
$coll->add('foo', $route);
346+
$matcher = new UrlMatcher($coll, new RequestContext('/sub/front.php'));
347+
$this->assertEquals(array('bar' => 'bar', '_route' => 'foo'), $matcher->match('/foo/bar'));
348+
}
349+
340350
public function testDecodeOnce()
341351
{
342352
$coll = new RouteCollection();

0 commit comments

Comments
 (0)