merged branch Tobion/strictrequirements2 (PR #5445)
This PR was merged into the master branch. Commits -------e22823b
[Routing] context params should have higher priority than defaults16c1b01
[Routing] fixed 4 bugs in the UrlGenerator Discussion ---------- [Routing] UrlGenerator: fixed missing query param and some ignored requirements reopened version of #5181 (cherry-picked) On top of that I fixed #5437 in my code and added test case. --------------------------------------------------------------------------- by Tobion at 2012-10-03T18:41:45Z @fabpot ping --------------------------------------------------------------------------- by fabpot at 2012-10-03T18:43:43Z IIUC, #5437 is a regression in 2.1 and should be done in 2.1, no? --------------------------------------------------------------------------- by Tobion at 2012-10-03T18:46:42Z It's not in 2.1 anymore as you reverted the PR. #5437 is not valid currently and can be closed. So this can either be merged in master or 2.1 whatever you prefer.
This commit is contained in:
commit
7276b2d0b8
@ -23,6 +23,7 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
|||||||
* UrlGenerator generates a URL based on a set of routes.
|
* UrlGenerator generates a URL based on a set of routes.
|
||||||
*
|
*
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Tobias Schultze <http://tobion.de>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@ -132,13 +133,10 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
|
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
|
||||||
{
|
{
|
||||||
$variables = array_flip($variables);
|
$variables = array_flip($variables);
|
||||||
|
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters);
|
||||||
$originParameters = $parameters;
|
|
||||||
$parameters = array_replace($this->context->getParameters(), $parameters);
|
|
||||||
$tparams = array_replace($defaults, $parameters);
|
|
||||||
|
|
||||||
// all params must be given
|
// all params must be given
|
||||||
if ($diff = array_diff_key($variables, $tparams)) {
|
if ($diff = array_diff_key($variables, $mergedParams)) {
|
||||||
throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters ("%s").', $name, implode('", "', array_keys($diff))));
|
throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters ("%s").', $name, implode('", "', array_keys($diff))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,30 +144,26 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
$optional = true;
|
$optional = true;
|
||||||
foreach ($tokens as $token) {
|
foreach ($tokens as $token) {
|
||||||
if ('variable' === $token[0]) {
|
if ('variable' === $token[0]) {
|
||||||
if (false === $optional || !array_key_exists($token[3], $defaults) || (isset($parameters[$token[3]]) && (string) $parameters[$token[3]] != (string) $defaults[$token[3]])) {
|
if (!$optional || !array_key_exists($token[3], $defaults) || (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
|
||||||
if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
|
// check requirement
|
||||||
// check requirement
|
if (!preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
|
||||||
if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
|
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]);
|
||||||
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]);
|
if ($this->strictRequirements) {
|
||||||
if ($this->strictRequirements) {
|
throw new InvalidParameterException($message);
|
||||||
throw new InvalidParameterException($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->logger) {
|
|
||||||
$this->logger->err($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->logger) {
|
||||||
|
$this->logger->err($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$isEmpty || !$optional) {
|
$url = $token[1].$mergedParams[$token[3]].$url;
|
||||||
$url = $token[1].$tparams[$token[3]].$url;
|
|
||||||
}
|
|
||||||
|
|
||||||
$optional = false;
|
$optional = false;
|
||||||
}
|
}
|
||||||
} elseif ('text' === $token[0]) {
|
} else {
|
||||||
|
// static text
|
||||||
$url = $token[1].$url;
|
$url = $token[1].$url;
|
||||||
$optional = false;
|
$optional = false;
|
||||||
}
|
}
|
||||||
@ -193,7 +187,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add a query string if needed
|
// add a query string if needed
|
||||||
$extra = array_diff_key($originParameters, $variables, $defaults);
|
$extra = array_diff_key($parameters, $variables);
|
||||||
if ($extra && $query = http_build_query($extra, '', '&')) {
|
if ($extra && $query = http_build_query($extra, '', '&')) {
|
||||||
$url .= '?'.$query;
|
$url .= '?'.$query;
|
||||||
}
|
}
|
||||||
|
@ -74,12 +74,15 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('/app.php/testing', $url);
|
$this->assertEquals('/app.php/testing', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
|
||||||
|
*/
|
||||||
public function testRelativeUrlWithNullParameterButNotOptional()
|
public function testRelativeUrlWithNullParameterButNotOptional()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
|
||||||
$url = $this->getGenerator($routes)->generate('test', array(), false);
|
// This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params.
|
||||||
|
// Generating path "/testing//bar" would be wrong as matching this route would fail.
|
||||||
$this->assertEquals('/app.php/testing//bar', $url);
|
$this->getGenerator($routes)->generate('test', array(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRelativeUrlWithOptionalZeroParameter()
|
public function testRelativeUrlWithOptionalZeroParameter()
|
||||||
@ -90,6 +93,13 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('/app.php/testing/0', $url);
|
$this->assertEquals('/app.php/testing/0', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNotPassedOptionalParameterInBetween()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{slug}/{page}', array('slug' => 'index', 'page' => 0)));
|
||||||
|
$this->assertSame('/app.php/index/1', $this->getGenerator($routes)->generate('test', array('page' => 1)));
|
||||||
|
$this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testRelativeUrlWithExtraParameters()
|
public function testRelativeUrlWithExtraParameters()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/testing'));
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
||||||
@ -138,6 +148,18 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('/app.php/testing/bar', $url);
|
$this->assertEquals('/app.php/testing/bar', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGlobalParameterHasHigherPriorityThanDefault()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{_locale}', array('_locale' => 'en')));
|
||||||
|
$generator = $this->getGenerator($routes);
|
||||||
|
$context = new RequestContext('/app.php');
|
||||||
|
$context->setParameter('_locale', 'de');
|
||||||
|
$generator->setContext($context);
|
||||||
|
$url = $generator->generate('test', array());
|
||||||
|
|
||||||
|
$this->assertSame('/app.php/de', $url);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Symfony\Component\Routing\Exception\RouteNotFoundException
|
* @expectedException Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||||
*/
|
*/
|
||||||
@ -165,6 +187,15 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
|
||||||
|
*/
|
||||||
|
public function testGenerateForRouteWithInvalidParameter()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => '1|2')));
|
||||||
|
$this->getGenerator($routes)->generate('test', array('foo' => '0'), true);
|
||||||
|
}
|
||||||
|
|
||||||
public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
|
public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
|
||||||
@ -196,6 +227,15 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
|
||||||
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
|
||||||
|
*/
|
||||||
|
public function testRequiredParamAndEmptyPassed()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{slug}', array(), array('slug' => '.+')));
|
||||||
|
$this->getGenerator($routes)->generate('test', array('slug' => ''));
|
||||||
|
}
|
||||||
|
|
||||||
public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
|
public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
|
||||||
{
|
{
|
||||||
@ -229,6 +269,15 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
|
$this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testQueryParamSameAsDefault()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
|
||||||
|
|
||||||
|
$this->assertSame('/app.php/test?default=foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
|
||||||
|
$this->assertSame('/app.php/test?default=value', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
|
||||||
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testUrlEncoding()
|
public function testUrlEncoding()
|
||||||
{
|
{
|
||||||
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
|
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
|
||||||
|
Reference in New Issue
Block a user