[Routing] added the possibility to define default values and requirements for placeholders in prefix
This commit is contained in:
parent
1bd6e4d427
commit
2e1344eb7e
@ -79,6 +79,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
||||
|
||||
### Routing
|
||||
|
||||
* added the possibility to define default values and requirements for placeholders in prefix
|
||||
* added RouterInterface::getRouteCollection
|
||||
|
||||
### Translation
|
||||
|
@ -76,8 +76,29 @@ class XmlFileLoader extends FileLoader
|
||||
$resource = (string) $node->getAttribute('resource');
|
||||
$type = (string) $node->getAttribute('type');
|
||||
$prefix = (string) $node->getAttribute('prefix');
|
||||
|
||||
$defaults = array();
|
||||
$requirements = array();
|
||||
|
||||
foreach ($node->childNodes as $n) {
|
||||
if (!$n instanceof \DOMElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($n->tagName) {
|
||||
case 'default':
|
||||
$defaults[(string) $n->getAttribute('key')] = trim((string) $n->nodeValue);
|
||||
break;
|
||||
case 'requirement':
|
||||
$requirements[(string) $n->getAttribute('key')] = trim((string) $n->nodeValue);
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $n->tagName));
|
||||
}
|
||||
}
|
||||
|
||||
$this->setCurrentDir(dirname($path));
|
||||
$collection->addCollection($this->import($resource, ('' !== $type ? $type : null), false, $file), $prefix);
|
||||
$collection->addCollection($this->import($resource, ('' !== $type ? $type : null), false, $file), $prefix, $defaults, $requirements);
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
|
||||
|
@ -67,8 +67,11 @@ class YamlFileLoader extends FileLoader
|
||||
if (isset($config['resource'])) {
|
||||
$type = isset($config['type']) ? $config['type'] : null;
|
||||
$prefix = isset($config['prefix']) ? $config['prefix'] : null;
|
||||
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
|
||||
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
|
||||
|
||||
$this->setCurrentDir(dirname($path));
|
||||
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix);
|
||||
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements);
|
||||
} else {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
}
|
||||
|
@ -26,6 +26,11 @@
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="import">
|
||||
<xsd:sequence>
|
||||
<xsd:element name="default" type="element" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="requirement" type="element" minOccurs="0" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
|
||||
<xsd:attribute name="resource" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="prefix" type="xsd:string" />
|
||||
|
@ -162,6 +162,21 @@ class Route
|
||||
public function setDefaults(array $defaults)
|
||||
{
|
||||
$this->defaults = array();
|
||||
|
||||
return $this->addDefaults($defaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds defaults.
|
||||
*
|
||||
* This method implements a fluent interface.
|
||||
*
|
||||
* @param array $defaults The defaults
|
||||
*
|
||||
* @return Route The current Route instance
|
||||
*/
|
||||
public function addDefaults(array $defaults)
|
||||
{
|
||||
foreach ($defaults as $name => $default) {
|
||||
$this->defaults[(string) $name] = $default;
|
||||
}
|
||||
@ -232,6 +247,21 @@ class Route
|
||||
public function setRequirements(array $requirements)
|
||||
{
|
||||
$this->requirements = array();
|
||||
|
||||
return $this->addRequirements($requirements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds requirements.
|
||||
*
|
||||
* This method implements a fluent interface.
|
||||
*
|
||||
* @param array $requirements The requirements
|
||||
*
|
||||
* @return Route The current Route instance
|
||||
*/
|
||||
public function addRequirements(array $requirements)
|
||||
{
|
||||
foreach ($requirements as $key => $regex) {
|
||||
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
|
||||
}
|
||||
|
@ -165,15 +165,17 @@ class RouteCollection implements \IteratorAggregate
|
||||
/**
|
||||
* Adds a route collection to the current set of routes (at the end of the current set).
|
||||
*
|
||||
* @param RouteCollection $collection A RouteCollection instance
|
||||
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
||||
* @param RouteCollection $collection A RouteCollection instance
|
||||
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
||||
* @param array $defaults An array of default values
|
||||
* @param array $requirements An array of requirements
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function addCollection(RouteCollection $collection, $prefix = '')
|
||||
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array())
|
||||
{
|
||||
$collection->setParent($this);
|
||||
$collection->addPrefix($prefix);
|
||||
$collection->addPrefix($prefix, $defaults, $requirements);
|
||||
|
||||
// remove all routes with the same name in all existing collections
|
||||
foreach (array_keys($collection->all()) as $name) {
|
||||
@ -186,11 +188,13 @@ class RouteCollection implements \IteratorAggregate
|
||||
/**
|
||||
* Adds a prefix to all routes in the current set.
|
||||
*
|
||||
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
||||
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
||||
* @param array $defaults An array of default values
|
||||
* @param array $requirements An array of requirements
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function addPrefix($prefix)
|
||||
public function addPrefix($prefix, $defaults = array(), $requirements = array())
|
||||
{
|
||||
// a prefix must not end with a slash
|
||||
$prefix = rtrim($prefix, '/');
|
||||
@ -208,9 +212,11 @@ class RouteCollection implements \IteratorAggregate
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
if ($route instanceof RouteCollection) {
|
||||
$route->addPrefix($prefix);
|
||||
$route->addPrefix($prefix, $defaults, $requirements);
|
||||
} else {
|
||||
$route->setPattern($prefix.$route->getPattern());
|
||||
$route->addDefaults($defaults);
|
||||
$route->addRequirements($requirements);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,5 +4,8 @@
|
||||
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="validpattern.xml" />
|
||||
<import resource="validpattern.xml" prefix="/{foo}">
|
||||
<default key="foo">foo</default>
|
||||
<requirement key="foo">\d+</requirement>
|
||||
</import>
|
||||
</routes>
|
||||
|
@ -1,2 +1,5 @@
|
||||
blog_show:
|
||||
resource: validpattern.yml
|
||||
resource: validpattern.yml
|
||||
prefix: /{foo}
|
||||
defaults: { 'foo': 'foo' }
|
||||
requirements: { 'foo': '\d+' }
|
||||
|
@ -51,6 +51,8 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(1, count($routes), 'One route is loaded');
|
||||
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
|
||||
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
|
||||
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,4 +91,3 @@ class CustomXmlFileLoader extends XmlFileLoader
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,6 +88,8 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(1, count($routes), 'One route is loaded');
|
||||
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
|
||||
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
|
||||
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,8 +103,10 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
|
||||
$collection->add('foo', $foo = new Route('/foo'));
|
||||
$collection1 = new RouteCollection();
|
||||
$collection1->add('foo', $foo1 = new Route('/foo1'));
|
||||
$collection->addCollection($collection1, '/foo');
|
||||
$this->assertEquals('/foo/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
|
||||
$collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'));
|
||||
$this->assertEquals('/{foo}/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
|
||||
$this->assertEquals(array('foo' => 'foo'), $collection->get('foo')->getDefaults(), '->addCollection() can add a prefix to all merged routes');
|
||||
$this->assertEquals(array('foo' => '\d+'), $collection->get('foo')->getRequirements(), '->addCollection() can add a prefix to all merged routes');
|
||||
|
||||
$collection = new RouteCollection();
|
||||
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
|
||||
@ -119,9 +121,13 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
|
||||
$collection = new RouteCollection();
|
||||
$collection->add('foo', $foo = new Route('/foo'));
|
||||
$collection->add('bar', $bar = new Route('/bar'));
|
||||
$collection->addPrefix('/admin');
|
||||
$this->assertEquals('/admin/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals('/admin/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
||||
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
|
||||
$this->assertEquals('/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals('/{admin}/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds a prefix to all routes');
|
||||
$this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds a prefix to all routes');
|
||||
}
|
||||
|
||||
public function testResource()
|
||||
|
@ -68,6 +68,11 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$route->setDefault('_controller', $closure = function () { return 'Hello'; });
|
||||
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
|
||||
|
||||
$route->setDefaults(array('foo' => 'foo'));
|
||||
$route->addDefaults(array('bar' => 'bar'));
|
||||
$this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
|
||||
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
|
||||
}
|
||||
|
||||
public function testRequirements()
|
||||
@ -80,6 +85,11 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
$route->setRequirements(array('foo' => '^\d+$'));
|
||||
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the pattern');
|
||||
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
|
||||
|
||||
$route->setRequirements(array('foo' => '\d+'));
|
||||
$route->addRequirements(array('bar' => '\d+'));
|
||||
$this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
|
||||
$this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
|
||||
}
|
||||
|
||||
public function testRequirement()
|
||||
|
Reference in New Issue
Block a user