8000 [Routing] Allow inline definition of requirements and defaults · symfony/symfony@0819bf1 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

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

Commit 0819bf1

Browse files
[Routing] Allow inline definition of requirements and defaults
1 parent 9e82562 commit 0819bf1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Symfony/Component/Routing/Route.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class Route implements \Serializable
5353
public function __construct(string $path, array $defaults = array(), array $requirements = array(), array $options = array(), ?string $host = '', $schemes = array(), $methods = array(), ?string $condition = '')
5454
{
5555
$this->setPath($path);
56-
$this->setDefaults($defaults);
57-
$this->setRequirements($requirements);
56+
$this->addDefaults($defaults);
57+
$this->addRequirements($requirements);
5858
$this->setOptions($options);
5959
$this->setHost($host);
6060
$this->setSchemes($schemes);
@@ -123,6 +123,19 @@ public function getPath()
123123
*/
124124
public function setPath($pattern)
125125
{
126+
if (false !== strpbrk($pattern, '?<')) {
127+
$pattern = preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
128+
if (isset($m[3][0])) {
129+
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
130+
}
131+
if (isset($m[2][0])) {
132+
$this->setRequirement($m[1], substr($m[2], 1, -1));
133+
}
134+
135+
return '{'.$m[1].'}';
136+
}, $pattern);
137+
}
138+
126139
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
127140
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
128141
$this->path = '/'.ltrim(trim($pattern), '/');

src/Symfony/Component/Routing/Tests/RouteTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ public function testSerialize()
203203
$this->assertNotSame($route, $unserialized);
204204
}
205205

206+
public function testInlineDefaultAndRequirement()
207+
{
208+
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', null), new Route('/foo/{bar?}'));
209+
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', 'baz'), new Route('/foo/{bar?baz}'));
210+
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', 'baz<buz>'), new Route('/foo/{bar?baz<buz>}'));
211+
212+
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '.*'), new Route('/foo/{bar<.*>}'));
213+
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '>'), new Route('/foo/{bar<>>}'));
214+
215+
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', null)->setRequirement('bar', '.*'), new Route('/foo/{bar<.*>?}'));
216+
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', '<>')->setRequirement('bar', '>'), new Route('/foo/{bar<>>?<>}'));
217+
}
218+
206219
/**
207220
* Tests that the compiled version is also serialized to prevent the overhead
208221
* of compiling it again after unserialize.

0 commit comments

Comments
 (0)
0