8000 merged branch stof/routing_options_import (PR #3775) · symfony/symfony@71c9dc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71c9dc3

Browse files
committed
merged branch stof/routing_options_import (PR #3775)
Commits ------- 3c32569 [Routing] Added the possibility to define options for imported resources Discussion ---------- [Routing] Added the possibility to define options for imported resources Closes #2772
2 parents 479d808 + 3c32569 commit 71c9dc3

12 files changed

+59
-17
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
348348

349349
* the UrlMatcher does not throw a \LogicException any more when the required scheme is not the current one
350350
* added a TraceableUrlMatcher
351-
* added the possibility to define default values and requirements for placeholders in prefix, including imported routes
351+
* added the possibility to define options, default values and requirements for placeholders in prefix, including imported routes
352352
* added RouterInterface::getRouteCollection
353353

354354
### Security

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
7979

8080
$defaults = array();
8181
$requirements = array();
82+
$options = array();
8283

8384
foreach ($node->childNodes as $n) {
8485
if (!$n instanceof \DOMElement) {
@@ -92,13 +93,16 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
9293
case 'requirement':
9394
$requirements[(string) $n->getAttribute('key')] = trim((string) $n->nodeValue);
9495
break;
96+
case 'option':
97+
$options[(string) $n->getAttribute('key')] = trim((string) $n->nodeValue);
98+
break;
9599
default:
96100
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $n->tagName));
97101
}
98102
}
99103

100104
$this->setCurrentDir(dirname($path));
101-
$collection->addCollection($this->import($resource, ('' !== $type ? $type : null), false, $file), $prefix, $defaults, $requirements);
105+
$collection->addCollection($this->import($resource, ('' !== $type ? $type : null), false, $file), $prefix, $defaults, $requirements, $options);
102106
break;
103107
default:
104108
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public function load($file, $type = null)
6969
$prefix = isset($config['prefix']) ? $config['prefix'] : null;
7070
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
7171
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
72+
$options = isset($config['options']) ? $config['options'] : array();
7273

7374
$this->setCurrentDir(dirname($path));
74-
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements);
75+
$collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements, $options);
7576
} else {
7677
$this->parseRoute($collection, $name, $config, $path);
7778
}

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
@@ -29,6 +29,7 @@
2929
<xsd:sequence>
3030
<xsd:element name="default" type="element" minOccurs="0" maxOccurs="unbounded" />
3131
<xsd:element name="requirement" type="element" minOccurs="0" maxOccurs="unbounded" />
32+
<xsd:element name="option" type="element" minOccurs="0" maxOccurs="unbounded" />
3233
</xsd:sequence>
3334

3435
<xsd:attribute name="resource" type="xsd:string" />

src/Symfony/Component/Routing/Route.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,27 @@ public function getOptions()
107107
*/
108108
public function setOptions(array $options)
109109
{
110-
$this->options = array_merge(array(
110+
$this->options = array(
111111
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
112-
), $options);
112+
);
113+
114+
return $this->addOptions($options);
115+
}
116+
117+
/**
118+
* Adds options.
119+
*
120+
* This method implements a fluent interface.
121+
*
122+
* @param array $options The options
123+
*
124+
* @return Route The current Route instance
125+
*/
126+
public function addOptions(array $options)
127+
{
128+
foreach ($options as $name => $option) {
129+
$this->options[(string) $name] = $option;
130+
}
113131

114132
return $this;
115133
}

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ public function remove($name)
179179
* @param string $prefix An optional prefix to add before each pattern of the route collection
180180
* @param array $defaults An array of default values
181181
* @param array $requirements An array of requirements
182+
* @param array $options An array of options
182183
*
183184
* @api
184185
*/
185-
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array())
186+
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array())
186187
{
187188
$collection->setParent($this);
188-
$collection->addPrefix($prefix, $defaults, $requirements);
189+
$collection->addPrefix($prefix, $defaults, $requirements, $options);
189190

190191
// remove all routes with the same name in all existing collections
191192
foreach (array_keys($collection->all()) as $name) {
@@ -201,10 +202,11 @@ public function addCollection(RouteCollection $collection, $prefix = '', $defaul
201202
* @param string $prefix An optional prefix to add before each pattern of the route collection
202203
* @param array $defaults An array of default values
203204
* @param array $requirements An array of requirements
205+
* @param array $options An array of options
204206
*
205207
* @api
206208
*/
207-
public function addPrefix($prefix, $defaults = array(), $requirements = array())
209+
public function addPrefix($prefix, $defaults = array(), $requirements = array(), $options = array())
208210
{
209211
// a prefix must not end with a slash
210212
$prefix = rtrim($prefix, '/');
@@ -218,11 +220,12 @@ public function addPrefix($prefix, $defaults = array(), $requirements = array())
218220

219221
foreach ($this->routes as $name => $route) {
220222
if ($route instanceof RouteCollection) {
221-
$route->addPrefix($prefix, $defaults, $requirements);
223+
$route->addPrefix($prefix, $defaults, $requirements, $options);
222224
} else {
223225
$route->setPattern($prefix.$route->getPattern());
224226
$route->addDefaults($defaults);
225227
$route->addRequirements($requirements);
228+
$route->addOptions($options);
226229
}
227230
}
228231
}

src/Symfony/Component/Routing/Tests/Fixtures/validresource.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
<import resource="validpattern.xml" prefix="/{foo}">
88
<default key="foo">foo</default>
99
<requirement key="foo">\d+</requirement>
10+
<option key="foo">bar</option>
1011
</import>
1112
</routes>

src/Symfony/Component/Routing/Tests/Fixtures/validresource.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ blog_show:
33
prefix: /{foo}
44
defaults: { 'foo': 'foo' }
55
requirements: { 'foo': '\d+' }
6+
options: { 'foo': 'bar' }

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function testLoadWithImport()
6161
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
6262
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
6363
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
64+
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
6465
}
6566

6667
/**

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function testLoadWithResource()
101101
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
102102
$this->assertEquals('foo', $routes['blog_show']->getDefault('foo'));
103103
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
104+
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
104105
}
105106

106107
/**

src/Symfony/Component/Routing/Tests/RouteCollectionTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ public function testAddCollection()
107107
$collection->add('foo', $foo = new Route('/foo'));
108108
$collection1 = new RouteCollection();
109109
$collection1->add('foo', $foo1 = new Route('/foo1'));
110-
$collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'));
110+
$collection->addCollection($collection1, '/{foo}', array('foo' => 'foo'), array('foo' => '\d+'), array('foo' => 'bar'));
111111
$this->assertEquals('/{foo}/foo1', $collection->get('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
112112
$this->assertEquals(array('foo' => 'foo'), $collection->get('foo')->getDefaults(), '->addCollection() can add a prefix to all merged routes');
113113
$this->assertEquals(array('foo' => '\d+'), $collection->get('foo')->getRequirements(), '->addCollection() can add a prefix to all merged routes');
114+
$this->assertEquals(
115+
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
116+
$collection->get('foo')->getOptions(), '->addCollection() can add an option to all merged routes'
117+
);
114118

115119
$collection = new RouteCollection();
116120
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
@@ -125,13 +129,21 @@ public function testAddPrefix()
125129
$collection = new RouteCollection();
126130
$collection->add('foo', $foo = new Route('/foo'));
127131
$collection->add('bar', $bar = new Route('/bar'));
128-
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
132+
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'), array('foo' => 'bar'));
129133
$this->assertEquals('/{admin}/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
130134
$this->assertEquals('/{admin}/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
131135
$this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds a prefix to all routes');
132136
$this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds a prefix to all routes');
133137
$this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds a prefix to all routes');
134138
$this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds a prefix to all routes');
139+
$this->assertEquals(
140+
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
141+
$collection->get('foo')->getOptions(), '->addPrefix() adds an option to all routes'
142+
);
143+
$this->assertEquals(
144+
array('foo' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
145+
$collection->get('bar')->getOptions(), '->addPrefix() adds an option to all routes'
146+
);
135147
}
136148

137149
public function testAddPrefixOverridesDefaultsAndRequirements()

src/Symfony/Component/Routing/Tests/RouteTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ public function testOptions()
4444
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
4545
), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
4646
$this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
47+
48+
$route->setOptions(array('foo' => 'foo'));
49+
$route->addOptions(array('bar' => 'bar'));
50+
$this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
51+
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
4752
}
4853

49-
/**
50-
* @covers Symfony\Component\Routing\Route::setDefaults
51-
* @covers Symfony\Component\Routing\Route::getDefaults
52-
* @covers Symfony\Component\Routing\Route::setDefault
53-
* @covers Symfony\Component\Routing\Route::getDefault
54-
*/
5554
public function testDefaults()
5655
{
5756
$route = new Route('/{foo}');

0 commit comments

Comments
 (0)
0