Skip to content

Commit cd086b7

Browse files
[Routing] Allow defining and reusing subroutines
1 parent 9e82562 commit cd086b7

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public function load($file, $type = null)
7878
}
7979

8080
foreach ($parsedConfig as $name => $config) {
81+
if ('_subroutines' === $name && \is_array($config) && !isset($config['resource']) && !isset($config['path'])) {
82+
foreach ($config as $name => $pattern) {
83+
$collection->setSubroutine($name, $pattern);
84+
}
85+
continue;
86+
}
8187
$this->validate($config, $name, $path);
8288

8389
if (isset($config['resource'])) {

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ private function compileDynamicRoutes(RouteCollection $collection, bool $matchHo
315315
if (!$collection->all()) {
316316
return '';
317317
}
318+
$subroutines = '';
319+
foreach ($this->getRoutes()->getSubroutines() as $name => $regex) {
320+
$subroutines .= sprintf('(?(DEFINE)(?P<%s>%s))', $name, $regex);
321+
}
318322
$code = '';
319323
$state = (object) array(
320324
'regex' => '',
@@ -399,7 +403,7 @@ private function compileDynamicRoutes(RouteCollection $collection, bool $matchHo
399403
$code .= "\n .')'";
400404
$state->regex .= ')';
401405
}
402-
$rx = ")$}{$modifiers}";
406+
$rx = $subroutines.")$}{$modifiers}";
403407
$code .= "\n .'{$rx}',";
404408
$state->regex .= $rx;
405409

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class RouteCollection implements \IteratorAggregate, \Countable
3535
*/
3636
private $resources = array();
3737

38+
/**
39+
* @var array
40+
*/
41+
private $subroutines = array();
42+
3843
public function __clone()
3944
{
4045
foreach ($this->routes as $name => $route) {
@@ -129,6 +134,10 @@ public function addCollection(self $collection)
129134
foreach ($collection->getResources() as $resource) {
130135
$this->addResource($resource);
131136
}
137+
138+
foreach ($collection->getSubroutines() as $name => $pattern) {
139+
$this->setSubroutine($name, $pattern);
140+
}
132141
}
133142

134143
/**
@@ -291,4 +300,17 @@ public function addResource(ResourceInterface $resource)
291300
$this->resources[$key] = $resource;
292301
}
293302
}
303+
304+
public function setSubroutine(string $name, string $pattern)
305+
{
306+
if (\strlen($name) > RouteCompiler::VARIABLE_MAXIMUM_LENGTH) {
307+
throw new \DomainException(sprintf('Subroutine name "%s" cannot be longer than %s characters. Please use a shorter name for pattern "%s".', $name, RouteCompiler::VARIABLE_MAXIMUM_LENGTH, $pattern));
308+
}
309+
$this->subroutines[$name] = $pattern;
310+
}
311+
312+
public function getSubroutines()
313+
{
314+
return $this->subroutines;
315+
}
294316
}

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class RouteCollectionBuilder
3737
private $schemes;
3838
private $methods;
3939
private $resources = array();
40+
private $subroutines = array();
4041

4142
public function __construct(LoaderInterface $loader = null)
4243
{
@@ -76,6 +77,10 @@ public function import($resource, $prefix = '/', $type = null)
7677
foreach ($collection->getResources() as $resource) {
7778
$builder->addResource($resource);
7879
}
80+
81+
foreach ($collection->getSubroutines() as $name => $pattern) {
82+
$builder->setSubroutine($name, $pattern);
83+
}
7984
}
8085

8186
// mount into this builder
@@ -262,6 +267,16 @@ private function addResource(ResourceInterface $resource): RouteCollectionBuilde
262267
return $this;
263268
}
264269

270+
public function setSubroutine(string $name, string $pattern)
271+
{
272+
$this->subroutines[$name] = $pattern;
273+
}
274+
275+
public function getSubroutines()
276+
{
277+
return $this->subroutines;
278+
}
279+
265280
/**
266281
* Creates the final RouteCollection and returns it.
267282
*
@@ -321,6 +336,10 @@ public function build()
321336
$routeCollection->addResource($resource);
322337
}
323338

339+
foreach ($this->subroutines as $name => $pattern) {
340+
$routeCollection->setSubroutine($name, $pattern);
341+
}
342+
324343
return $routeCollection;
325344
}
326345

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