8000 feature #37696 [Routing] Allow inline definition of requirements and … · symfony/symfony@e411c96 · GitHub
[go: up one dir, main page]

Skip to content

Commit e411c96

Browse files
committed
feature #37696 [Routing] Allow inline definition of requirements and defaults for host (julienfalque)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Routing] Allow inline definition of requirements and defaults for host | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#14007 This PR extends the support of inline routing requirements and defaults to the host part, which can also contain parameters. Commits ------- 1501476 [Routing] Allow inline definition of requirements and defaults for host
2 parents e983035 + 1501476 commit e411c96

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* Added support for inline definition of requirements and defaults for host
8+
49
5.1.0
510
-----
611

src/Symfony/Component/Routing/Route.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,7 @@ public function getPath()
130130
*/
131131
public function setPath(string $pattern)
132132
{
133-
if (false !== strpbrk($pattern, '?<')) {
134-
$pattern = preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
135-
if (isset($m[3][0])) {
136-
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
137-
}
138-
if (isset($m[2][0])) {
139-
$this->setRequirement($m[1], substr($m[2], 1, -1));
140-
}
141-
142-
return '{'.$m[1].'}';
143-
}, $pattern);
144-
}
133+
$pattern = $this->extractInlineDefaultsAndRequirements($pattern);
145134

146135
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
147136
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
@@ -170,7 +159,7 @@ public function getHost()
170159
*/
171160
public function setHost(?string $pattern)
172161
{
173-
$this->host = (string) $pattern;
162+
$this->host = $this->extractInlineDefaultsAndRequirements((string) $pattern);
174163
$this->compiled = null;
175164

176165
return $this;
@@ -544,6 +533,24 @@ public function compile()
544533
return $this->compiled = $class::compile($this);
545534
}
546535

536+
private function extractInlineDefaultsAndRequirements(string $pattern): string
537+
{
538+
if (false === strpbrk($pattern, '?<')) {
539+
return $pattern;
540+
}
541+
542+
return preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
543+
if (isset($m[3][0])) {
544+
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
545+
}
546+
if (isset($m[2][0])) {
547+
$this->setRequirement($m[1], substr($m[2], 1, -1));
548+
}
549+
550+
return '{'.$m[1].'}';
551+
}, $pattern);
552+
}
553+
547554
private function sanitizeRequirement(string $key, string $regex)
548555
{
549556
if ('' !== $regex && '^' === $regex[0]) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ public function testInlineDefaultAndRequirement()
216216

217217
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', null)->setRequirement('bar', '.*'), new Route('/foo/{bar<.*>?}'));
218218
$this->assertEquals((new Route('/foo/{bar}'))->setDefault('bar', '<>')->setRequirement('bar', '>'), new Route('/foo/{bar<>>?<>}'));
219+
220+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', null), (new Route('/'))->setHost('{bar?}'));
221+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', 'baz'), (new Route('/'))->setHost('{bar?baz}'));
222+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', 'baz<buz>'), (new Route('/'))->setHost('{bar?baz<buz>}'));
223+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', null), (new Route('/', ['bar' => 'baz']))->setHost('{bar?}'));
224+
225+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setRequirement('bar', '.*'), (new Route('/'))->setHost('{bar<.*>}'));
226+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setRequirement('bar', '>'), (new Route('/'))->setHost('{bar<>>}'));
227+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setRequirement('bar', '.*'), (new Route('/', [], ['bar' => '\d+']))->setHost('{bar<.*>}'));
228+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setRequirement('bar', '[a-z]{2}'), (new Route('/'))->setHost('{bar<[a-z]{2}>}'));
229+
230+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', null)->setRequirement('bar', '.*'), (new Route('/'))->setHost('{bar<.*>?}'));
231+
$this->assertEquals((new Route('/'))->setHost('{bar}')->setDefault('bar', '<>')->setRequirement('bar', '>'), (new Route('/'))->setHost('{bar<>>?<>}'));
219232
}
220233

221234
/**

0 commit comments

Comments
 (0)
0