From fc015d522759316bbf47bd38cd94f198d96dccc9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 24 Apr 2012 22:58:11 +0200 Subject: [PATCH] [Routing] fixed route generation with a hostname pattern when the hostname is the same as the current one (no need to force the generated URL to be absolute) --- .../Routing/Generator/UrlGenerator.php | 47 +++++++++---------- .../Tests/Generator/UrlGeneratorTest.php | 21 +++++++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index e16b07972b..5cf7dbdcc0 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -185,32 +185,13 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt $url = substr($url, 0, -1) . '%2E'; } - if ($hostnameTokens) { - $host = ''; - foreach ($hostnameTokens as $token) { - if ('variable' === $token[0]) { - if (in_array($tparams[$token[3]], array(null, '', false), true)) { - // check requirement - if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) { - throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]])); - } - } - - $host = $token[1].$tparams[$token[3]].$host; - - } elseif ('text' === $token[0]) { - $host = $token[1].$host; - } - } - } - // add a query string if needed $extra = array_diff_key($parameters, $variables); if ($extra && $query = http_build_query($extra, '', '&')) { $url .= '?'.$query; } - if ($this->context->getHost()) { + if ($host = $this->context->getHost()) { $scheme = $this->context->getScheme(); if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) { $absolute = true; @@ -218,15 +199,29 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt } if ($hostnameTokens) { - $absolute = true; + $ghost = ''; + foreach ($hostnameTokens as $token) { + if ('variable' === $token[0]) { + if (in_array($mergedParams[$token[3]], array(null, '', false), true)) { + // check requirement + if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { + throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]])); + } + } + + $ghost = $token[1].$mergedParams[$token[3]].$ghost; + } elseif ('text' === $token[0]) { + $ghost = $token[1].$ghost; + } + } + + if ($ghost != $host) { + $host = $ghost; + $absolute = true; + } } if ($absolute) { - - if (!$hostnameTokens) { - $host = $this->context->getHost(); - } - $port = ''; if ('http' === $scheme && 80 != $this->context->getHttpPort()) { $port = ':'.$this->context->getHttpPort(); diff --git a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php index 210239a0d8..080125ce11 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php @@ -377,6 +377,27 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase $this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html')); } + public function testWithHostnameDifferentFromContext() + { + $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); + + $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' =>'Fabien', 'locale' => 'fr'))); + } + + public function testWithHostnameSameAsContext() + { + $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); + + $this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'))); + } + + public function testWithHostnameSameAsContextAndAbsolute() + { + $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com')); + + $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true)); + } + protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null) { $context = new RequestContext('/app.php');