Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ce1dda

Browse files
bug #40755 [Routing] Better inline requirements and defaults parsing (Foxprodev)
This PR was merged into the 4.4 branch. Discussion ---------- [Routing] Better inline requirements and defaults parsing | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40749 #40701 | License | MIT | Doc PR | Remove `!` symbol from requirements and defaults array keys in Route class. Leave `!` symbol in Route compiled path for correct token creation. **The only restriction I found:** Important variable can't get default value, only in UrlGenerator. As mentioned in https://github.com/symfony/symfony/blob/0f96ac74847d114c9d9679655bcf3e94b6ba69d1/src/Symfony/Component/Routing/RouteCompiler.php#L217 they are not optional Feel free to help me with some advice. Thank you in advance Commits ------- 2a8c94a [Route] Better inline requirements and defaults parsing
2 parents 4a8ea40 + 2a8c94a commit 7ce1dda

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/Symfony/Component/Routing/Route.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ public function getPath()
137137
public function setPath($pattern)
138138
{
139139
if (false !== strpbrk($pattern, '?<')) {
140-
$pattern = preg_replace_callback('#\{(!?\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
141-
if (isset($m[3][0])) {
142-
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
140+
$pattern = preg_replace_callback('#\{(!?)(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
141+
if (isset($m[4][0])) {
142+
$this->setDefault($m[2], '?' !== $m[4] ? substr($m[4], 1) : null);
143143
}
144-
if (isset($m[2][0])) {
145-
$this->setRequirement($m[1], substr($m[2], 1, -1));
144+
if (isset($m[3][0])) {
145+
$this->setRequirement($m[2], substr($m[3], 1, -1));
146146
}
147147

148-
return '{'.$m[1].'}';
148+
return '{'.$m[1].$m[2].'}';
149149
}, $pattern);
150150
}
151151

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function testPath()
5050
$this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
5151
$route->setPath('//path');
5252
$this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slashes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
53+
$route->setPath('/path/{!foo}');
54+
$this->assertEquals('/path/{!foo}', $route->getPath(), '->setPath() keeps ! to pass important params');
55+
$route->setPath('/path/{bar<\w++>}');
56+
$this->assertEquals('/path/{bar}', $route->getPath(), '->setPath() removes inline requirements');
57+
$route->setPath('/path/{foo?value}');
58+
$this->assertEquals('/path/{foo}', $route->getPath(), '->setPath() removes inline defaults');
59+
$route->setPath('/path/{!bar<\d+>?value}');
60+
$this->assertEquals('/path/{!bar}', $route->getPath(), '->setPath() removes all inline settings');
5361
}
5462

5563
public function testOptions()
@@ -211,16 +219,19 @@ public function testInlineDefaultAndRequirement()
211219
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', null), new Route('/foo/{bar?}'));
212220
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', 'baz'), new Route('/foo/{bar?baz}'));
213221
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', 'baz<buz>'), new Route('/foo/{bar?baz<buz>}'));
214-
$this->assertEquals((new Route('/foo/{!bar}'))->setDefault('!bar', 'baz<buz>'), new Route('/foo/{!bar?baz<buz>}'));
222+
$this->assertEquals((new Route('/foo/{!bar}'))->setDefault('bar', 'baz<buz>'), new Route('/foo/{!bar?baz<buz>}'));
215223
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', 'baz'), new Route('/foo/{bar?}', ['bar' => 'baz']));
216224

217225
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '.*'), new Route('/foo/{bar<.*>}'));
218226
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '>'), new Route('/foo/{bar<>>}'));
219227
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '\d+'), new Route('/foo/{bar<.*>}', [], ['bar' => '\d+']));
220228
$this->assertEquals((new Route('/foo/{bar}'))->setRequirement('bar', '[a-z]{2}'), new Route('/foo/{bar<[a-z]{2}>}'));
229+
$this->assertEquals((new Route('/foo/{!bar}'))->setRequirement('bar', '\d+'), new Route('/foo/{!bar<\d+>}'));
221230

222231
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', null)->setRequirement('bar', '.*'), new Route('/foo/{bar<.*>?}'));
223232
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', '<>')->setRequirement('bar', '>'), new Route('/foo/{bar<>>?<>}'));
233+
234+
$this->assertEquals((new Route('/{foo}/{!bar}'))->setDefaults(['bar' => '<>', 'foo' => '\\'])->setRequirements(['bar' => '\\', 'foo' => '.']), new Route('/{foo<.>?\}/{!bar<\>?<>}'));
224235
}
225236

226237
/**

0 commit comments

Comments
 (0)