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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow to set name prefixes from the configuration
  • Loading branch information
sroze committed Nov 27, 2017
commit e0a8690dd697f1e618a346f8ff88c28cbd17090c
4 changes: 4 additions & 0 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

if ($namePrefix = $node->getAttribute('name-prefix')) {
$subCollection->addNamePrefix($namePrefix);
}

$collection->addCollection($subCollection);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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;

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

if (isset($config['name_prefix'])) {
$subCollection->addNamePrefix($config['name_prefix']);
}

$collection->addCollection($subCollection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
}
}

/**
* Add a prefix to the name of all the routes within in the collection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds

*
* @param string $prefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be removed

*/
public function addNamePrefix(string $prefix)
{
foreach ($this->routes as $name => $route) {
$this->routes[$prefix.$name] = $route;
unset($this->routes[$name]);
}
}

/**
* Sets the host pattern on all routes.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app:
resource: ../controller/routing.yml

api:
resource: ../controller/routing.yml
name_prefix: api_
prefix: /api
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
someroute:
resource: path/to/some.yml
name_prefix: test_
not_valid_key: test_
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,15 @@ public function testImportWithOverriddenController()
$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());
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,15 @@ public function testImportWithOverriddenController()
$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());
}
}
15 changes: 14 additions & 1 deletion src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions()
$collection->add('foo', new Route('/{placeholder}'));
$collection1 = new RouteCollection();
$collection1->add('bar', new Route('/{placeholder}',
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
);
$collection->addCollection($collection1);

Expand Down Expand Up @@ -302,4 +302,17 @@ public function testSetMethods()
$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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add an existing api_foo route, to be sure that the prefixing does not delete this route due to overriding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, added.

$collection->add('bar', $bar = new Route('/bar'));
$collection->addNamePrefix('api_');

$this->assertEquals($foo, $collection->get('api_foo'));
$this->assertEquals($bar, $collection->get('api_bar'));
$this->assertNull($collection->get('foo'));
$this->assertNull($collection->get('bar'));
}
}