[Routing] Added the possibility to define options for imported resources
Closes #2772
This commit is contained in:
parent
e5121e91c8
commit
3c325698ac
@ -348,7 +348,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
||||
|
||||
* the UrlMatcher does not throw a \LogicException any more when the required scheme is not the current one
|
||||
* added a TraceableUrlMatcher
|
||||
* added the possibility to define default values and requirements for placeholders in prefix, including imported routes
|
||||
* added the possibility to define options, default values and requirements for placeholders in prefix, including imported routes
|
||||
* added RouterInterface::getRouteCollection
|
||||
|
||||
### Security
|
||||
|
@ -79,6 +79,7 @@ class XmlFileLoader extends FileLoader
|
||||
|
||||
$defaults = array();
|
||||
$requirements = array();
|
||||
$options = array();
|
||||
|
||||
foreach ($node->childNodes as $n) {
|
||||
if (!$n instanceof \DOMElement) {
|
||||
@ -92,13 +93,16 @@ class XmlFileLoader extends FileLoader
|
||||
case 'requirement':
|
||||
$requirements[(string) $n->getAttribute('key')] = trim((string) $n->nodeValue);
|
||||
break;
|
||||
case 'option':
|
||||
$options[(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, $defaults, $requirements);
|
||||
$collection->addCollection($this->import($resource, ('' !== $type ? $type : null), false, $file), $prefix, $defaults, $requirements, $options);
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
|
||||
|
@ -69,9 +69,10 @@ class YamlFileLoader extends FileLoader
|
||||
$prefix = isset($config['prefix']) ? $config['prefix'] : null;
|
||||
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
|
||||
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
|
||||
$options = isset($config['options']) ? $config['options'] : array();
|
||||
|
||||
$this->setCurrentDir(dirname($path));
|
||||
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements);
|
||||
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements, $options);
|
||||
} else {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
<xsd:sequence>
|
||||
<xsd:element name="default" type="element" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="requirement" type="element" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="option" type="element" minOccurs="0" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
|
||||
<xsd:attribute name="resource" type="xsd:string" />
|
||||
|
@ -107,9 +107,27 @@ class Route
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = array_merge(array(
|
||||
$this->options = array(
|
||||
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
|
||||
), $options);
|
||||
);
|
||||
|
||||
return $this->addOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds options.
|
||||
*
|
||||
* This method implements a fluent interface.
|
||||
*
|
||||
* @param array $options The options
|
||||
*
|
||||
* @return Route The current Route instance
|
||||
*/
|
||||
public function addOptions(array $options)
|
||||
{
|
||||
foreach ($options as $name => $option) {
|
||||
$this->options[(string) $name] = $option;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -179,13 +179,14 @@ class RouteCollection implements \IteratorAggregate
|
||||
* @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
|
||||
* @param array $options An array of options
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array())
|
||||
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array())
|
||||
{
|
||||
$collection->setParent($this);
|
||||
$collection->addPrefix($prefix, $defaults, $requirements);
|
||||
$collection->addPrefix($prefix, $defaults, $requirements, $options);
|
||||
|
||||
// remove all routes with the same name in all existing collections
|
||||
foreach (array_keys($collection->all()) as $name) {
|
||||
@ -201,10 +202,11 @@ class RouteCollection implements \IteratorAggregate
|
||||
* @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
|
||||
* @param array $options An array of options
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function addPrefix($prefix, $defaults = array(), $requirements = array())
|
||||
public function addPrefix($prefix, $defaults = array(), $requirements = array(), $options = array())
|
||||
{
|
||||
// a prefix must not end with a slash
|
||||
$prefix = rtrim($prefix, '/');
|
||||
@ -218,11 +220,12 @@ class RouteCollection implements \IteratorAggregate
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
if ($route instanceof RouteCollection) {
|
||||
$route->addPrefix($prefix, $defaults, $requirements);
|
||||
$route->addPrefix($prefix, $defaults, $requirements, $options);
|
||||
} else {
|
||||
$route->setPattern($prefix.$route->getPattern());
|
||||
$route->addDefaults($defaults);
|
||||
$route->addRequirements($requirements);
|
||||
$route->addOptions($options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,6 @@
|
||||
<import resource="validpattern.xml" prefix="/{foo}">
|
||||
<default key="foo">foo</default>
|
||||
<requirement key="foo">\d+</requirement>
|
||||
<option key="foo">bar</option>
|
||||
</import>
|
||||
</routes>
|
||||
|
@ -3,3 +3,4 @@ blog_show:
|
||||
prefix: /{foo}
|
||||
defaults: { 'foo': 'foo' }
|
||||
requirements: { 'foo': '\d+' }
|
||||
options: { 'foo': 'bar' }
|
||||
|
@ -61,6 +61,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
|
||||
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
|
||||
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
|
||||
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,6 +101,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
|
||||
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
|
||||
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
|
||||
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,10 +107,14 @@ 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}', array('foo' => 'foo'), array('foo' => '\d+'));
|
||||
$collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'), array('foo' => 'bar'));
|
||||
$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');
|
||||
$this->assertEquals(
|
||||
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
|
||||
$collection->get('foo')->getOptions(), '->addCollection() can add an option to all merged routes'
|
||||
);
|
||||
|
||||
$collection = new RouteCollection();
|
||||
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
|
||||
@ -125,13 +129,21 @@ 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}', array('admin' => 'admin'), array('admin' => '\d+'));
|
||||
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'), array('foo' => 'bar'));
|
||||
$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');
|
||||
$this->assertEquals(
|
||||
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
|
||||
$collection->get('foo')->getOptions(), '->addPrefix() adds an option to all routes'
|
||||
);
|
||||
$this->assertEquals(
|
||||
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
|
||||
$collection->get('bar')->getOptions(), '->addPrefix() adds an option to all routes'
|
||||
);
|
||||
}
|
||||
|
||||
public function testAddPrefixOverridesDefaultsAndRequirements()
|
||||
|
@ -44,14 +44,13 @@ class RouteTest extends \PHPUnit_Framework_TestCase
|
||||
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
|
||||
), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
|
||||
$this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
|
||||
|
||||
$route->setOptions(array('foo' => 'foo'));
|
||||
$route->addOptions(array('bar' => 'bar'));
|
||||
$this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
|
||||
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\Routing\Route::setDefaults
|
||||
* @covers Symfony\Component\Routing\Route::getDefaults
|
||||
* @covers Symfony\Component\Routing\Route::setDefault
|
||||
* @covers Symfony\Component\Routing\Route::getDefault
|
||||
*/
|
||||
public function testDefaults()
|
||||
{
|
||||
$route = new Route('/{foo}');
|
||||
|
Reference in New Issue
Block a user