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

Skip to content

Commit a603ba0

Browse files
committed
feature #25178 [Routing] Allow to set name prefixes from the configuration (sroze)
This PR was squashed before being merged into the 4.1-dev branch (closes #25178). Discussion ---------- [Routing] Allow to set name prefixes from the configuration | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19612 | License | MIT | Doc PR | ø This allows setting name prefixes to routes while importing them. Typically, we can then import multiple times a similar file. This was originally requested by 🎸 @chrisguitarguy in #19612 ```yaml app: resource: ../controller/routing.yml api: resource: ../controller/routing.yml name_prefix: api_ prefix: /api ``` ```xml <?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> <import resource="../controller/routing.xml" /> <import resource="../controller/routing.xml" prefix="/api" name-prefix="api_" /> </routes> ``` Commits ------- 880d7e7 [Routing] Allow to set name prefixes from the configuration
2 parents 44fcbd3 + 880d7e7 commit a603ba0

File tree

11 files changed

+89
-2
lines changed

11 files changed

+89
-2
lines changed

src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,14 @@ final public function controller($controller)
124124

125125
return $this;
126126
}
127+
128+
/**
129+
* Adds a prefix to the name of all the routes within the collection.
130+
*/
131+
final public function addNamePrefix(string $prefix): self
132+
{
133+
$this->route->addNamePrefix($prefix);
134+
135+
return $this;
136+
}
127137
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
165165
$subCollection->addRequirements($requirements);
166166
$subCollection->addOptions($options);
167167

168+
if ($namePrefix = $node->getAttribute('name-prefix')) {
169+
$subCollection->addNamePrefix($namePrefix);
170+
}
171+
168172
$collection->addCollection($subCollection);
169173
}
170174

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class YamlFileLoader extends FileLoader
2828
{
2929
private static $availableKeys = array(
30-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
30+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix',
3131
);
3232
private $yamlParser;
3333

@@ -169,6 +169,10 @@ protected function parseImport(RouteCollection $collection, array $config, $path
169169
$subCollection->addRequirements($requirements);
170170
$subCollection->addOptions($options);
171171

172+
if (isset($config['name_prefix'])) {
173+
$subCollection->addNamePrefix($config['name_prefix']);
174+
}
175+
172176
$collection->addCollection($subCollection);
173177
}
174178

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<xsd:attribute name="resource" type="xsd:string" use="required" />
5151
<xsd:attribute name="type" type="xsd:string" />
5252
<xsd:attribute name="prefix" type="xsd:string" />
53+
<xsd:attribute name="name-prefix" type="xsd:string" />
5354
<xsd:attribute name="host" type="xsd:string" />
5455
<xsd:attribute name="schemes" type="xsd:string" />
5556
<xsd:attribute name="methods" type="xsd:string" />

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
153153
}
154154
}
155155

156+
/**
157+
* Adds a prefix to the name of all the routes within in the collection.
158+
*/
159+
public function addNamePrefix(string $prefix)
160+
{
161+
$prefixedRoutes = array();
162+
163+
foreach ($this->routes as $name => $route) {
164+
$prefixedRoutes[$prefix.$name] = $route;
165+
}
166+
167+
$this->routes = $prefixedRoutes;
168+
}
169+
156170
/**
157171
* Sets the host pattern on all routes.
158172
*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<import resource="../controller/routing.xml" />
8+
<import resource="../controller/routing.xml" prefix="/api" name-prefix="api_" />
9+
10+
</routes>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
app:
2+
resource: ../controller/routing.yml
3+
4+
api:
5+
resource: ../controller/routing.yml
6+
name_prefix: api_
7+
prefix: /api
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
someroute:
22
resource: path/to/some.yml
3-
name_prefix: test_
3+
not_valid_key: test_

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,15 @@ public function testImportWithOverriddenController()
361361
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
362362
$loader->load('import_override_defaults.xml');
363363
}
364+
365+
public function testImportRouteWithNamePrefix()
366+
{
367+
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
368+
$routeCollection = $loader->load('routing.xml');
369+
370+
$this->assertNotNull($routeCollection->get('app_blog'));
371+
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
372+
$this->assertNotNull($routeCollection->get('api_app_blog'));
373+
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
374+
}
364375
}

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,15 @@ public function testImportWithOverriddenController()
182182
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
183183
$loader->load('import_override_defaults.yml');
184184
}
185+
186+
public function testImportRouteWithNamePrefix()
187+
{
188+
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
189+
$routeCollection = $loader->load('routing.yml');
190+
191+
$this->assertNotNull($routeCollection->get('app_blog'));
192+
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
193+
$this->assertNotNull($routeCollection->get('api_app_blog'));
194+
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
195+
}
185196
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,19 @@ public function testSetMethods()
302302
$this->assertEquals(array('PUT'), $routea->getMethods());
303303
$this->assertEquals(array('PUT'), $routeb->getMethods());
304304
}
305+
306+
public function testAddNamePrefix()
307+
{
308+
$collection = new RouteCollection();
309+
$collection->add('foo', $foo = new Route('/foo'));
310+
$collection->add('bar', $bar = new Route('/bar'));
311+
$collection->add('api_foo', $apiFoo = new Route('/api/foo'));
312+
$collection->addNamePrefix('api_');
313+
314+
$this->assertEquals($foo, $collection->get('api_foo'));
315+
$this->assertEquals($bar, $collection->get('api_bar'));
316+
$this->assertEquals($apiFoo, $collection->get('api_api_foo'));
317+
$this->assertNull($collection->get('foo'));
318+
$this->assertNull($collection->get('bar'));
319+
}
305320
}

0 commit comments

Comments
 (0)