[Routing] Allow to set name prefixes from the configuration
This commit is contained in:
parent
22192b16d8
commit
880d7e7436
@ -124,4 +124,14 @@ trait RouteTrait
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a prefix to the name of all the routes within the collection.
|
||||
*/
|
||||
final public function addNamePrefix(string $prefix): self
|
||||
{
|
||||
$this->route->addNamePrefix($prefix);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -165,6 +165,10 @@ class XmlFileLoader extends FileLoader
|
||||
$subCollection->addRequirements($requirements);
|
||||
$subCollection->addOptions($options);
|
||||
|
||||
if ($namePrefix = $node->getAttribute('name-prefix')) {
|
||||
$subCollection->addNamePrefix($namePrefix);
|
||||
}
|
||||
|
||||
$collection->addCollection($subCollection);
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ use Symfony\Component\Config\Loader\FileLoader;
|
||||
class YamlFileLoader extends FileLoader
|
||||
{
|
||||
private static $availableKeys = array(
|
||||
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
|
||||
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix',
|
||||
);
|
||||
private $yamlParser;
|
||||
|
||||
@ -169,6 +169,10 @@ class YamlFileLoader extends FileLoader
|
||||
$subCollection->addRequirements($requirements);
|
||||
$subCollection->addOptions($options);
|
||||
|
||||
if (isset($config['name_prefix'])) {
|
||||
$subCollection->addNamePrefix($config['name_prefix']);
|
||||
}
|
||||
|
||||
$collection->addCollection($subCollection);
|
||||
}
|
||||
|
||||
|
@ -50,6 +50,7 @@
|
||||
<xsd:attribute name="resource" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="prefix" type="xsd:string" />
|
||||
<xsd:attribute name="name-prefix" type="xsd:string" />
|
||||
<xsd:attribute name="host" type="xsd:string" />
|
||||
<xsd:attribute name="schemes" type="xsd:string" />
|
||||
<xsd:attribute name="methods" type="xsd:string" />
|
||||
|
@ -153,6 +153,20 @@ class RouteCollection implements \IteratorAggregate, \Countable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a prefix to the name of all the routes within in the collection.
|
||||
*/
|
||||
public function addNamePrefix(string $prefix)
|
||||
{
|
||||
$prefixedRoutes = array();
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
$prefixedRoutes[$prefix.$name] = $route;
|
||||
}
|
||||
|
||||
$this->routes = $prefixedRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the host pattern on all routes.
|
||||
*
|
||||
|
@ -0,0 +1,10 @@
|
||||
<?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>
|
@ -0,0 +1,7 @@
|
||||
app:
|
||||
resource: ../controller/routing.yml
|
||||
|
||||
api:
|
||||
resource: ../controller/routing.yml
|
||||
name_prefix: api_
|
||||
prefix: /api
|
@ -1,3 +1,3 @@
|
||||
someroute:
|
||||
resource: path/to/some.yml
|
||||
name_prefix: test_
|
||||
not_valid_key: test_
|
||||
|
@ -361,4 +361,15 @@ class XmlFileLoaderTest extends TestCase
|
||||
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
|
||||
$loader->load('import_override_defaults.xml');
|
||||
}
|
||||
|
||||
public function testImportRouteWithNamePrefix()
|
||||
{
|
||||
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
|
||||
$routeCollection = $loader->load('routing.xml');
|
||||
|
||||
$this->assertNotNull($routeCollection->get('app_blog'));
|
||||
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
|
||||
$this->assertNotNull($routeCollection->get('api_app_blog'));
|
||||
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
|
||||
}
|
||||
}
|
||||
|
@ -182,4 +182,15 @@ class YamlFileLoaderTest extends TestCase
|
||||
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
|
||||
$loader->load('import_override_defaults.yml');
|
||||
}
|
||||
|
||||
public function testImportRouteWithNamePrefix()
|
||||
{
|
||||
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
|
||||
$routeCollection = $loader->load('routing.yml');
|
||||
|
||||
$this->assertNotNull($routeCollection->get('app_blog'));
|
||||
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
|
||||
$this->assertNotNull($routeCollection->get('api_app_blog'));
|
||||
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
|
||||
}
|
||||
}
|
||||
|
@ -302,4 +302,19 @@ class RouteCollectionTest extends TestCase
|
||||
$this->assertEquals(array('PUT'), $routea->getMethods());
|
||||
$this->assertEquals(array('PUT'), $routeb->getMethods());
|
||||
}
|
||||
|
||||
public function testAddNamePrefix()
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$collection->add('foo', $foo = new Route('/foo'));
|
||||
$collection->add('bar', $bar = new Route('/bar'));
|
||||
$collection->add('api_foo', $apiFoo = new Route('/api/foo'));
|
||||
$collection->addNamePrefix('api_');
|
||||
|
||||
$this->assertEquals($foo, $collection->get('api_foo'));
|
||||
$this->assertEquals($bar, $collection->get('api_bar'));
|
||||
$this->assertEquals($apiFoo, $collection->get('api_api_foo'));
|
||||
$this->assertNull($collection->get('foo'));
|
||||
$this->assertNull($collection->get('bar'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user