10000 [Routing] allow disabling the requirements check on URL generation · symfony/symfony@98fb915 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98fb915

Browse files
committed
[Routing] allow disabling the requirements check on URL generation
1 parent 7276b2d commit 98fb915

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,32 @@
1212
namespace Symfony\Component\Routing\Generator;
1313

1414
/**
15-
* ConfigurableRequirementsInterface must be implemented by URL generators in order
16-
* to be able to configure whether an exception should be generated when the
17-
* parameters do not match the requirements.
15+
* ConfigurableRequirementsInterface must be implemented by URL generators that
16+
* can be configured whether an exception should be generated when the parameters
17+
* do not match the requirements. It is also possible to disable the requirements
18+
* check for URL generation completely to improve performance when you know that
19+
* the parameters meet the requirements anyway (because they are from a trusted
20+
* source or you validated them beforehand which should be the case in production
21+
* environment as it would otherwise break your site).
1822
*
1923
* @author Fabien Potencier <fabien@symfony.com>
24+
* @author Tobias Schultze <http://tobion.de>
2025
*/
2126
interface ConfigurableRequirementsInterface
2227
{
2328
/**
2429
* Enables or disables the exception on incorrect parameters.
30+
* Passing null will deactivate the requirements check completely.
2531
*
26-
* @param Boolean $enabled
32+
* @param Boolean|null $enabled
2733
*/
2834
public function setStrictRequirements($enabled);
2935

3036
/**
31-
* Gets the strict check of incorrect parameters.
37+
* Returns whether to throw an exception on incorrect parameters.
38+
* Null means the requirements check is deactivated completely.
3239
*
33-
* @return Boolean
40+
* @return Boolean|null
3441
*/
3542
public function isStrictRequirements();
3643
}

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getContext()
100100
*/
101101
public function setStrictRequirements($enabled)
102102
{
103-
$this->strictRequirements = (Boolean) $enabled;
103+
$this->strictRequirements = null === $enabled ? null : (Boolean) $enabled;
104104
}
105105

106106
/**
@@ -146,7 +146,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
146146
if ('variable' === $token[0]) {
147147
if (!$optional || !array_key_exists($token[3], $defaults) || (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
148148
// check requirement
149-
if (!preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
149+
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
150150
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]);
151151
if ($this->strictRequirements) {
152152
throw new InvalidParameterException($message);

0 commit comments

Comments
 (0)
0