[Routing] allow disabling the requirements check on URL generation

This commit is contained in:
Tobias Schultze 2012-08-06 03:33:49 +02:00
parent 569e29d91f
commit 4225869939
2 changed files with 15 additions and 8 deletions

View File

@ -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 <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*/
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();
}

View File

@ -100,7 +100,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
*/
public function setStrictRequirements($enabled)
{
$this->strictRequirements = (Boolean) $enabled;
$this->strictRequirements = null === $enabled ? null : (Boolean) $enabled;
}
/**
@ -146,7 +146,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
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);