Skip to content

Commit

Permalink
[Routing] fixed default value for Routes as they can be anything if t…
Browse files Browse the repository at this point in the history
…hey don't need to be used as default values for placeholders
  • Loading branch information
fabpot committed Jul 19, 2011
1 parent dafa4d2 commit 9e7cb0a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -120,7 +120,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$optional = true;
foreach ($tokens as $token) {
if ('variable' === $token[0]) {
if (false === $optional || !array_key_exists($token[3], $defaults) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]])) {
if (false === $optional || !array_key_exists($token[3], $defaults) || (isset($parameters[$token[3]]) && (string) $parameters[$token[3]] != (string) $defaults[$token[3]])) {
if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
// check requirement
if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/Route.php
Expand Up @@ -163,7 +163,7 @@ public function setDefaults(array $defaults)
{
$this->defaults = array();
foreach ($defaults as $name => $default) {
$this->defaults[(string) $name] = (string) $default;
$this->defaults[(string) $name] = $default;
}

return $this;
Expand Down Expand Up @@ -205,7 +205,7 @@ public function hasDefault($name)
*/
public function setDefault($name, $default)
{
$this->defaults[(string) $name] = (string) $default;
$this->defaults[(string) $name] = $default;

return $this;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Symfony/Tests/Component/Routing/RouteTest.php
Expand Up @@ -65,6 +65,9 @@ public function testDefaults()
$route->setDefault('foo2', 'bar2');
$this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');

$route->setDefault('_controller', $closure = function () { return 'Hello'; });
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
}

public function testRequirements()
Expand Down

0 comments on commit 9e7cb0a

Please sign in to comment.