8000 Allow to set name prefixes from the configuration · symfony/symfony@e0a8690 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0a8690

Browse files
committed
Allow to set name prefixes from the configuration
1 parent 22192b1 commit e0a8690

File tree

10 files changed

+77
-3
lines changed

10 files changed

+77
-3
lines changed

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
153153
}
154154
}
155155

156+
/**
157+
* Add a prefix to the name of all the routes within in the collection.
158+
*
159+
* @param string $prefix
160+
*/
161+
public function addNamePrefix(string $prefix)
162+
{
163+
foreach ($this->routes as $name => $route) {
164+
$this->routes[$prefix.$name] = $route;
165+
unset($this->routes[$name]);
166+
}
167+
}
168+
156169
/**
157170
* Sets the host pattern on all routes.
158171
*
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions()
118118
$collection->add('foo', new Route('/{placeholder}'));
119119
$collection1 = new RouteCollection();
120120
$collection1->add('bar', new Route('/{placeholder}',
121-
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
121+
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
122122
);
123123
$collection->addCollection($collection1);
124124

@@ -302,4 +302,17 @@ 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->addNamePrefix('api_');
312+
313+
$this->assertEquals($foo, $collection->get('api_foo'));
314+
$this->assertEquals($bar, $collection->get('api_bar'));
315+
$this->assertNull($collection->get('foo'));
316+
$this->assertNull($collection->get('bar'));
317+
}
305318
}

0 commit comments

Comments
 (0)
0