10000 merged branch Tobion/requirementscheck2 (PR #5446) · symfony/symfony@78dcfe5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78dcfe5

Browse files
committed
merged branch Tobion/requirementscheck2 (PR #5446)
This PR was merged into the master branch. Commits ------- faee47c added note in changelog about disabling requirements check 5fbed36 extended phpdoc of ConfigurableRequirementsInterface 1964d43 [Routing] added test for disabled requirements check 98fb915 [Routing] allow disabling the requirements check on URL generation Discussion ---------- [Routing] allow disabling the requirements check on URL generation Reopened version of #5187 Requires #5445 first --------------------------------------------------------------------------- by fabpot at 2012-10-03T19:03:25Z Can you rebase on master and add a note in the CHANGELOG? Also, I think the doc should be updated accordingly. Thanks. --------------------------------------------------------------------------- by Tobion at 2012-10-03T19:04:07Z @fabpot ping. The config for this feature is already present in master in symfony-standard. See [config_prod.yml](https://github.com/symfony/symfony-standard/blob/master/app/config/config_prod.yml) --------------------------------------------------------------------------- by Tobion at 2012-10-03T19:06:53Z Hehe, I already rebased while you wrote that. You mean I should add a tip to the routing component documentation? Sure, I have alread planned to adjust the symfony doc for the recent routing PRs. --------------------------------------------------------------------------- by fabpot at 2012-10-03T19:12:36Z Just adding a note about the new possibility in the CHANGELOG. --------------------------------------------------------------------------- by Tobion at 2012-10-03T19:53:19Z @fabpot done
2 parents 7276b2d + faee47c commit 78dcfe5

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ CHANGELOG
1515
* The default requirement now uses possesive quantifiers when possible which
1616
improves matching performance by up to 20% because it prevents backtracking
1717
when it's not needed.
18+
* The ConfigurableRequirementsInterface can now also be used to disable the requirements
19+
check on URL generation completely by calling `setStrictRequirements(null)`. It
20+
improves performance in production environment as you should know that params always
21+
pass the requirements (otherwise it would break your link anyway).
1822

1923
2.1.0
2024
-----
@@ -37,5 +41,6 @@ CHANGELOG
3741
been used anyway without creating inconsistencies
3842
* [BC BREAK] RouteCollection::remove also removes a route from parent
3943
collections (not only from its children)
40-
* added strict_requirements option to disable exceptions (and generate empty
41-
URLs instead) when generating a route with an invalid parameter value
44+
* added ConfigurableRequirementsInterface that allows to disable exceptions
45+
(and generate empty URLs instead) when generating a route with an invalid
46+
parameter value

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,44 @@
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.
19+
*
20+
* The possible configurations and use-cases:
21+
* - setStrictRequirements(true): Throw an exception for mismatching requirements. This
22+
* is mostly useful in development environment.
23+
* - setStrictRequirements(false): Don't throw an exception but return null as URL for
24+
* mismatching requirements and log the problem. Useful when you cannot control all
25+
* params because they come from third party libs but don't want to have a 404 in
26+
* production environment. It should log the mismatch so one can review it.
27+
* - setStrictRequirements(null): Return the URL with the given parameters without
28+
* checking the requirements at all. When generating an URL you should either trust
29+
* your params or you validated them beforehand because otherwise it would break your
30+
* link anyway. So in production environment you should know that params always pass
31+
* the requirements. Thus this option allows to disable the check on URL generation for
32+
* performance reasons (saving a preg_match for each requirement every time a URL is
33+
* generated).
1834
*
1935
* @author Fabien Potencier <fabien@symfony.com>
36+
* @author Tobias Schultze <http://tobion.de>
2037
*/
2138
interface ConfigurableRequirementsInterface
2239
{
2340
/**
2441
* Enables or disables the exception on incorrect parameters.
42+
* Passing null will deactivate the requirements check completely.
2543
*
26-
* @param Boolean $enabled
44+
* @param Boolean|null $enabled
2745
*/
2846
public function setStrictRequirements($enabled);
2947

3048
/**
31-
* Gets the strict check of incorrect parameters.
49+
* Returns whether to throw an exception on incorrect parameters.
50+
* Null means the requirements check is deactivated completely.
3251
*
33-
* @return Boolean
52+
* @return Boolean|null
3453
*/
3554
public function isStrictRequirements();
3655
}

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);

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLog
219219
$this->assertNull($generator->generate('test', array('foo' => 'bar'), true));
220220
}
221221

222+
public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsCheck()
223+
{
224+
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
225+
$generator = $this->getGenerator($routes);
226+
$generator->setStrictRequirements(null);
227+
$this->assertSame('/app.php/testing/bar', $generator->generate('test', array('foo' => 'bar')));
228+
}
229+
222230
/**
223231
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
224232
*/

0 commit comments

Comments
 (0)
0