Skip to content

Commit

Permalink
[Routing] allow disabling the requirements check on URL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Oct 3, 2012
1 parent 7276b2d commit 98fb915
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -100,7 +100,7 @@ public function getContext()
*/
public function setStrictRequirements($enabled)
{
$this->strictRequirements = (Boolean) $enabled;
$this->strictRequirements = null === $enabled ? null : (Boolean) $enabled;
}

/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 98fb915

Please sign in to comment.