Skip to content

Commit f392282

Browse files
committed
bug #22636 [Routing] Expose request in route conditions, if needed and possible (ro0NL)
This PR was squashed before being merged into the 2.7 branch (closes #22636). Discussion ---------- [Routing] Expose request in route conditions, if needed and possible | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16968, #22635 | License | MIT | Doc PR | - given ``` /** * @route("/", name="homepage", condition="request.isXmlHttpRequest()") */ ``` ``` $ app/console route:match / ``` before ![image](https://cloud.githubusercontent.com/assets/1047696/25716808/b9ab518e-3100-11e7-8b59-21351b5c14ca.png) after ![image](https://cloud.githubusercontent.com/assets/1047696/25716833/d08065fc-3100-11e7-9462-987b2c6eaa26.png) Commits ------- 016e976 [Routing] Expose request in route conditions, if needed and possible
2 parents 53a9111 + 016e976 commit f392282

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php

+1-1
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

src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php

+1-1
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;

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

+16-1
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
}

src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php

+10
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)