Skip to content

Commit e2cae06

Browse files
committed
[Routing] Allow inline definition of requirements and defaults for host
1 parent 0ab5eed commit e2cae06

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+
$pattern = preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
540+
if (isset($m[3][0])) {
541+
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
542+
}
543+
if (isset($m[2][0])) {
544+
$this->setRequirement($m[1], substr($m[2], 1, -1));
545+
}
546+
547+
return '{'.$m[1].'}';
548+
}, $pattern);
549+
}
550+
551+
return $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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy