[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)

This commit is contained in:
Fabien Potencier 2012-04-24 22:58:11 +02:00 committed by Arnaud Le Blanc
parent 462999d2d2
commit fc015d5227
2 changed files with 42 additions and 26 deletions

View File

@ -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();

View File

@ -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');