Skip to content

[Routing] Optional placeholders anywhere in the pattern #112

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
16 changes: 9 additions & 7 deletions src/Symfony/Component/Routing/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RouteCompiler implements RouteCompilerInterface
protected $route;
protected $variables;
protected $firstOptional;
protected $optionals;
protected $segments;
protected $tokens;
protected $staticPrefix;
Expand All @@ -38,6 +39,7 @@ public function compile(Route $route)
{
$this->route = $route;
$this->firstOptional = 0;
$this->optionals = array();
$this->segments = array();
$this->variables = array();
$this->tokens = array();
Expand Down Expand Up @@ -91,11 +93,10 @@ protected function preCompile()
*/
protected function postCompile()
{
// all segments after the last static segment are optional
// be careful, the n-1 is optional only if n is empty
for ($i = $this->firstOptional, $max = count($this->segments); $i < $max; $i++) {
$this->segments[$i] = (0 == $i ? '/?' : '').str_repeat(' ', $i - $this->firstOptional).'(?:'.$this->segments[$i];
$this->segments[] = str_repeat(' ', $max - $i - 1).')?';
if (in_array($i, $this->optionals)) {
$this->segments[$i] = (0 == $i ? '/?' : '').'(?:'.$this->segments[$i].')?';
}
}

$this->staticPrefix = '';
Expand Down Expand Up @@ -211,9 +212,10 @@ protected function compileForVariable($separator, $name, $variable)

$this->segments[] = preg_quote($separator, '#').'(?P<'.$variable.'>'.$requirement.')';
$this->variables[$variable] = $name;

if (!$this->route->getDefault($variable)) {
$this->firstOptional = count($this->segments);

$this->firstOptional = 0;
if (null !== $this->route->getDefault($variable)) {
$this->optionals[] = count($this->segments)-1;
}
}

Expand Down
23 changes: 21 additions & 2 deletions tests/Symfony/Tests/Component/Routing/RouteCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function provideCompileData()
array(
'Route with several variables that have default values',
array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => 'foobar')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?) (?:/(?P<foobar>[^/\.]+?) )?)?$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?))?(?:/(?P<foobar>[^/\.]+?))?$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
Expand All @@ -79,12 +79,31 @@ public function provideCompileData()
array(
'Route with several variables but some of them have no default values',
array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
'/foo', '#^/foo/(?P<bar>[^/\.]+?)/(?P<foobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?))?/(?P<foobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}'), array(
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),

array(
'Route with several variables that some of them have no default values and some of them have empty defaults',
array('/foo/{bar}/{foobar}/{foofoobar}', array('bar' => 'bar', 'foobar' => '')),
'/foo', '#^/foo(?:/(?P<bar>[^/\.]+?))?(?:/(?P<foobar>[^/\.]+?))?/(?P<foofoobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}', 'foofoobar' => '{foofoobar}'), array(
array('variable', '/', '{foofoobar}', 'foofoobar'),
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar'),
array('text', '/', 'foo', null),
)),

array(
'Route with several variables that some of them have no default values and one of them is first segment',
array('/{bar}/{foobar}/{foofoobar}', array('bar' => 'bar', 'foobar' => 'foobar')),
'', '#^/?(?:/(?P<bar>[^/\.]+?))?(?:/(?P<foobar>[^/\.]+?))?/(?P<foofoobar>[^/\.]+?)$#x', array('bar' => '{bar}', 'foobar' => '{foobar}', 'foofoobar' => '{foofoobar}'), array(
array('variable', '/', '{foofoobar}', 'foofoobar'),
array('variable', '/', '{foobar}', 'foobar'),
array('variable', '/', '{bar}', 'bar')
)),

array(
'Route with a custom token',
array('/=foo', array(), array(), array('compiler_class' => 'Symfony\\Tests\\Component\\Routing\\RouteCompiler')),
Expand Down