Skip to content

Commit 00ec9b5

Browse files
committed
Revert "merged branch Tobion/strictrequirements (PR #5181)"
This reverts commit 2da2a44, reversing changes made to 5885547.
1 parent aae4cee commit 00ec9b5

File tree

2 files changed

+27
-58
lines changed

2 files changed

+27
-58
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* UrlGenerator generates a URL based on a set of routes.
2424
*
2525
* @author Fabien Potencier <fabien@symfony.com>
26-
* @author Tobias Schultze <http://tobion.de>
2726
*
2827
* @api
2928
*/
@@ -133,37 +132,44 @@ public function generate($name, $parameters = array(), $absolute = false)
133132
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
134133
{
135134
$variables = array_flip($variables);
136-
$mergedParams = array_replace($this->context->getParameters(), $defaults, $parameters);
135+
136+
$originParameters = $parameters;
137+
$parameters = array_replace($this->context->getParameters(), $parameters);
138+
$tparams = array_replace($defaults, $parameters);
137139

138140
// all params must be given
139-
if ($diff = array_diff_key($variables, $mergedParams)) {
141+
if ($diff = array_diff_key($variables, $tparams)) {
140142
throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters ("%s").', $name, implode('", "', array_keys($diff))));
141143
}
142144

143145
$url = '';
144146
$optional = true;
145147
foreach ($tokens as $token) {
146148
if ('variable' === $token[0]) {
147-
if (!$optional || !array_key_exists($token[3], $defaults) || (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
148-
// check requirement
149-
if (!preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
150-
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]);
151-
if ($this->strictRequirements) {
152-
throw new InvalidParameterException($message);
153-
}
154-
155-
if ($this->logger) {
156-
$this->logger->err($message);
149+
if (false === $optional || !array_key_exists($token[3], $defaults) || (isset($parameters[$token[3]]) && (string) $parameters[$token[3]] != (string) $defaults[$token[3]])) {
150+
if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
151+
// check requirement
152+
if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
153+
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]);
154+
if ($this->strictRequirements) {
155+
throw new InvalidParameterException($message);
156+
}
157+
158+
if ($this->logger) {
159+
$this->logger->err($message);
160+
}
161+
162+
return null;
157163
}
164+
}
158165

159-
return null;
166+
if (!$isEmpty || !$optional) {
167+
$url = $token[1].$tparams[$token[3]].$url;
160168
}
161169

162-
$url = $token[1].$mergedParams[$token[3]].$url;
163170
$optional = false;
164171
}
165-
} else {
166-
// static text
172+
} elseif ('text' === $token[0]) {
167173
$url = $token[1].$url;
168174
$optional = false;
169175
}
@@ -187,7 +193,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
187193
}
188194

189195
// add a query string if needed
190-
$extra = array_diff_key($parameters, $variables);
196+
$extra = array_diff_key($originParameters, $variables, $defaults);
191197
if ($extra && $query = http_build_query($extra, '', '&')) {
192198
$url .= '?'.$query;
193199
}

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

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,12 @@ public function testRelativeUrlWithNullParameter()
7474
$this->assertEquals('/app.php/testing', $url);
7575
}
7676

77-
/**
78-
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
79-
*/
8077
public function testRelativeUrlWithNullParameterButNotOptional()
8178
{
8279
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
83-
// This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params.
84-
// Generating path "/testing//bar" would be wrong as matching this route would fail.
85-
$this->getGenerator($routes)->generate('test', array(), false);
80+
$url = $this->getGenerator($routes)->generate('test', array(), false);
81+
82+
$this->assertEquals('/app.php/testing//bar', $url);
8683
}
8784

8885
public function testRelativeUrlWithOptionalZeroParameter()
@@ -93,13 +90,6 @@ public function testRelativeUrlWithOptionalZeroParameter()
9390
$this->assertEquals('/app.php/testing/0', $url);
9491
}
9592

96-
public function testNotPassedOptionalParameterInBetween()
97-
{
98-
$routes = $this->getRoutes('test', new Route('/{slug}/{page}', array('slug' => 'index', 'page' => 0)));
99-
$this->assertSame('/app.php/index/1', $this->getGenerator($routes)->generate('test', array('page' => 1)));
100-
$this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test'));
101-
}
102-
10393
public function testRelativeUrlWithExtraParameters()
10494
{
10595
$routes = $this->getRoutes('test', new Route('/testing'));
@@ -175,15 +165,6 @@ public function testGenerateForRouteWithInvalidOptionalParameter()
175165
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
176166
}
177167

178-
/**
179-
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
180-
*/
181-
public function testGenerateForRouteWithInvalidParameter()
182-
{
183-
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => '1|2')));
184-
$this->getGenerator($routes)->generate('test', array('foo' => '0'), true);
185-
}
186-
187168
public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
188169
{
189170
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
@@ -215,15 +196,6 @@ public function testGenerateForRouteWithInvalidMandatoryParameter()
215196
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
216197
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
217198
}
218-
219-
/**
220-
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
221-
*/
222-
public function testRequiredParamAndEmptyPassed()
223-
{
224-
$routes = $this->getRoutes('test', new Route('/{slug}', array(), array('slug' => '.+')));
225-
$this->getGenerator($routes)->generate('test', array('slug' => ''));
226-
}
227199

228200
public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
229201
{
@@ -257,15 +229,6 @@ public function testWithAnIntegerAsADefaultValue()
257229
$this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
258230
}
259231

260-
public function testQueryParamSameAsDefault()
261-
{
262-
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
263-
264-
$this->assertSame('/app.php/test?default=foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
265-
$this->assertSame('/app.php/test?default=value', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
266-
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
267-
}
268-
269232
public function testUrlEncoding()
270233
{
271234
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)

0 commit comments

Comments
 (0)