From 98fb915bfbaaa0c1029d536fdf569b848a3fbdd6 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Mon, 6 Aug 2012 03:33:49 +0200 Subject: [PATCH] [Routing] allow disabling the requirements check on URL generation --- .../ConfigurableRequirementsInterface.php | 19 +++++++++++++------ .../Routing/Generator/UrlGenerator.php | 4 ++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php b/src/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php index 9a795d6aac33..89c86588eea9 100644 --- a/src/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php +++ b/src/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php @@ -12,25 +12,32 @@ namespace Symfony\Component\Routing\Generator; /** - * ConfigurableRequirementsInterface must be implemented by URL generators in order - * to be able to configure whether an exception should be generated when the - * parameters do not match the requirements. + * ConfigurableRequirementsInterface must be implemented by URL generators that + * can be configured whether an exception should be generated when the parameters + * do not match the requirements. It is also possible to disable the requirements + * check for URL generation completely to improve performance when you know that + * the parameters meet the requirements anyway (because they are from a trusted + * source or you validated them beforehand which should be the case in production + * environment as it would otherwise break your site). * * @author Fabien Potencier + * @author Tobias Schultze */ interface ConfigurableRequirementsInterface { /** * Enables or disables the exception on incorrect parameters. + * Passing null will deactivate the requirements check completely. * - * @param Boolean $enabled + * @param Boolean|null $enabled */ public function setStrictRequirements($enabled); /** - * Gets the strict check of incorrect parameters. + * Returns whether to throw an exception on incorrect parameters. + * Null means the requirements check is deactivated completely. * - * @return Boolean + * @return Boolean|null */ public function isStrictRequirements(); } diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index 23ac3d790406..c1c1aefe8e7c 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -100,7 +100,7 @@ public function getContext() */ public function setStrictRequirements($enabled) { - $this->strictRequirements = (Boolean) $enabled; + $this->strictRequirements = null === $enabled ? null : (Boolean) $enabled; } /** @@ -146,7 +146,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa if ('variable' === $token[0]) { if (!$optional || !array_key_exists($token[3], $defaults) || (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) { // check requirement - if (!preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { + if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]); if ($this->strictRequirements) { throw new InvalidParameterException($message);