Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add Route Annotation : explicit_defaults #28633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/Routing/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Route
private $methods = array();
private $schemes = array();
private $condition;
private $explicitDefaults = false;

/**
* @param array $data An array of key/value parameters
Expand Down Expand Up @@ -161,4 +162,14 @@ public function getCondition()
{
return $this->condition;
}

public function setExplicitDefaults($explicitDefaults)
{
$this->explicitDefaults = $explicitDefaults;
}

public function getExplicitDefaults()
{
return $this->explicitDefaults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function generateDeclaredRoutes()
$properties[] = $compiledRoute->getTokens();
$properties[] = $compiledRoute->getHostTokens();
$properties[] = $route->getSchemes();
$properties[] = $route->getExplicitDefaults();

$routes .= sprintf(" '%s' => %s,\n", $name, PhpMatcherDumper::export($properties));
}
Expand Down Expand Up @@ -127,9 +128,9 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
}

list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = self::$declaredRoutes[$name];
list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes, $explicitDefaults) = self::$declaredRoutes[$name];

return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes, $explicitDefaults);
}
EOF;
}
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Routing/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
* @throws InvalidParameterException When a parameter value for a placeholder is not correct because
* it does not match the requirement
*/
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array(), $explicitDefaults = false)
{
$variables = array_flip($variables);
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters);
Expand All @@ -156,7 +156,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$message = 'Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.';
foreach ($tokens as $token) {
if ('variable' === $token[0]) {
if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && ((string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) || true === $explicitDefaults) {
// check requirement
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
if ($this->strictRequirements) {
Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
$schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
$methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
$trailingSlashOnRoot = $node->hasAttribute('trailing-slash-on-root') ? XmlUtils::phpize($node->getAttribute('trailing-slash-on-root')) : true;
$explicitDefaults = $node->hasAttribute('explicit-defaults') ? XmlUtils::phpize($node->getAttribute('explicit-defaults')) : false;

list($defaults, $requirements, $options, $condition, /* $paths */, $prefixes) = $this->parseConfigs($node, $path);

Expand Down Expand Up @@ -222,6 +223,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->setExplicitDefaults($explicitDefaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

Expand Down Expand Up @@ -262,6 +264,7 @@ protected function loadFile($file)
private function parseConfigs(\DOMElement $node, $path)
{
$defaults = array();
$explicitDefaults = false;
$requirements = array();
$options = array();
$condition = null;
Expand Down
7 changes: 4 additions & 3 deletions 7 src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class YamlFileLoader extends FileLoader
{
private static $availableKeys = array(
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'explicit_defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root',
);
private $yamlParser;

Expand Down Expand Up @@ -109,6 +109,7 @@ public function supports($resource, $type = null)
protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
{
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
$explicitDefaults = isset($config['explicit_defaults']) ? $config['explicit_defaults'] : false;
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
$options = isset($config['options']) ? $config['options'] : array();
$host = isset($config['host']) ? $config['host'] : '';
Expand All @@ -127,7 +128,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
}

if (\is_array($config['path'])) {
$route = new Route('', $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$route = new Route('', $defaults, $requirements, $options, $host, $schemes, $methods, $condition, $explicitDefaults);

foreach ($config['path'] as $locale => $path) {
$localizedRoute = clone $route;
Expand All @@ -137,7 +138,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
$collection->add($name.'.'.$locale, $localizedRoute);
}
} else {
$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition, $explicitDefaults);
$collection->add($name, $route);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<xsd:group name="configs">
<xsd:choice>
<xsd:element name="default" nillable="true" type="default" />
<xsd:element name="explicit-defaults" nillable="true" type="xsd:boolean" />
<xsd:element name="requirement" type="element" />
<xsd:element name="option" type="element" />
<xsd:element name="condition" type="xsd:string" />
Expand Down
32 changes: 31 additions & 1 deletion 32 src/Symfony/Component/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Route implements \Serializable
private $requirements = array();
private $options = array();
private $condition = '';
private $explicitDefaults = false;

/**
* @var null|CompiledRoute
Expand All @@ -49,8 +50,9 @@ class Route implements \Serializable
* @param string|string[] $schemes A required URI scheme or an array of restricted schemes
* @param string|string[] $methods A required HTTP method or an array of restricted methods
* @param string $condition A condition that should evaluate to true for the route to match
* @param bool $explicitDefaults
*/
public function __construct(string $path, array $defaults = array(), array $requirements = array(), array $options = array(), ?string $host = '', $schemes = array(), $methods = array(), ?string $condition = '')
public function __construct(string $path, array $defaults = array(), array $requirements = array(), array $options = array(), ?string $host = '', $schemes = array(), $methods = array(), ?string $condition = '', bool $explicitDefaults = false)
{
$this->setPath($path);
$this->addDefaults($defaults);
Expand All @@ -60,6 +62,7 @@ public function __construct(string $path, array $defaults = array(), array $requ
$this->setSchemes($schemes);
$this->setMethods($methods);
$this->setCondition($condition);
$this->setExplicitDefaults($explicitDefaults);
}

/**
Expand Down Expand Up @@ -527,6 +530,33 @@ public function setCondition($condition)
return $this;
}

/**
* Returns the explicitDefaults.
*
* @return string The explicitDefaults
*/
public function getExplicitDefaults()
{
return $this->explicitDefaults;
}

/**
* Sets the explicitDefaults.
*
* This method implements a fluent interface.
*
* @param string $explicitDefaults The explicitDefaults
*
* @return $this
*/
public function setExplicitDefaults($explicitDefaults)
{
$this->explicitDefaults = (bool) $explicitDefaults;
$this->compiled = null;

return $this;
}

/**
* Compiles the route.
*
Expand Down
14 changes: 14 additions & 0 deletions 14 src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ public function setCondition($condition)
}
}

/**
* Sets explicit defaults on all routes.
*
* Existing explicit defaults will be overridden.
*
* @param bool $explicitDefaults
*/
public function setExplicitDefaults($explicitDefaults)
{
foreach ($this->routes as $route) {
$route->setExplicitDefaults($explicitDefaults);
}
}

/**
* Adds defaults to all routes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getValidParameters()
array('host', '{locale}.example.com', 'getHost'),
array('condition', 'context.getMethod() == "GET"', 'getCondition'),
array('value', array('nl' => '/hier', 'en' => '/here'), 'getLocalizedPaths'),
array('explicit_defaults', true, 'getExplicitDefaults'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
$collection->addDefaults(array(
'foo' => 123,
));
$collection->setExplicitDefaults(true);
$collection->addRequirements(array(
'foo' => '\d+',
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ _blog:
resource: validpattern.yml
prefix: /{foo}
defaults: { 'foo': '123' }
explicit_defaults: true
requirements: { 'foo': '\d+' }
options: { 'foo': 'bar' }
host: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ public function testDumpForRouteWithDefaults()
$this->assertEquals('/testing', $url);
}

public function testDumpForRouteWithExplicitDefaults()
{
$this->routeCollection->add('Test', (new Route('/testing/{foo}.{_format}', array('foo' => 'bar', '_format' => 'baz')))->setExplicitDefaults(true));

file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'ExplicitDefaultsRoutesUrlGenerator')));
include $this->testTmpFilepath;

$projectUrlGenerator = new \ExplicitDefaultsRoutesUrlGenerator(new RequestContext());
$url = $projectUrlGenerator->generate('Test', array());

$this->assertEquals('/testing/bar.baz', $url);
}

public function testDumpWithSchemeRequirement()
{
$this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ public function testNoTrailingSlashForMultipleOptionalParameters()
$this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
}

public function testExplicitDefaults()
{
$routes = $this->getRoutes('test', (new Route('/category/{slug1}'))->setExplicitDefaults(true));

$this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
}

public function testWithAnIntegerAsADefaultValue()
{
$routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0)));
Expand Down
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/Routing/Tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function testOption()
$this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
}

public function testExplicitDefaults()
{
$route = new Route('/{foo}');
$this->assertFalse($route->getExplicitDefaults(), '->getExplicitDefaults() return false by default');
}

public function testDefaults()
{
$route = new Route('/{foo}');
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.