Skip to content

Commit 8c76b05

Browse files
committed
Merge pull request #809 from egulias/718-forward-options
#718 forward route options
2 parents 721f47e + 33a200d commit 8c76b05

File tree

9 files changed

+92
-1
lines changed

9 files changed

+92
-1
lines changed

Routing/Loader/RestXmlCollectionLoader.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
103103
*/
104104
protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
105105
{
106+
106107
// the Symfony Routing component uses a path attribute since Symfony 2.2
107108
// instead of the deprecated pattern attribute0
108109
if (!$node->hasAttribute('path')) {
@@ -150,9 +151,33 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
150151
$node->appendChild($defaultFormatNode);
151152
}
152153

154+
$options = $this->getOptions($node);
155+
156+
foreach ($options as $option) {
157+
$node->appendChild($option);
158+
}
159+
153160
parent::parseRoute($collection, $node, $path);
154161
}
155162

163+
private function getOptions(\DOMElement $node)
164+
{
165+
$options = array();
166+
foreach ($node->childNodes as $child) {
167+
if ($child instanceof \DOMElement && $child->tagName === 'option') {
168+
$option = $node->ownerDocument->createElementNs(
169+
self::NAMESPACE_URI,
170+
'option',
171+
$child->nodeValue
172+
);
173+
$option->setAttribute('key', $child->getAttribute('key'));
174+
$options[] = $option;
175+
}
176+
}
177+
178+
return $options;
179+
}
180+
156181
/**
157182
* {@inheritDoc}
158183
*/

Routing/Loader/RestYamlCollectionLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function load($file, $type = null)
7373
$namePrefix = isset($config['name_prefix']) ? $config['name_prefix'] : null;
7474
$parent = isset($config['parent']) ? $config['parent'] : null;
7575
$type = isset($config['type']) ? $config['type'] : null;
76+
$options = isset($config['options']) ? $config['options'] : null;
7677
$currentDir = dirname($path);
7778

7879
$parents = array();
@@ -93,6 +94,10 @@ public function load($file, $type = null)
9394
$this->collectionParents[$name] = $parents;
9495
}
9596

97+
if ($options) {
98+
$imported->addOptions($options);
99+
}
100+
96101
$imported->addPrefix($prefix);
97102
$collection->addCollection($imported);
98103
} elseif (isset($config['pattern']) || isset($config['path'])) {

Tests/Fixtures/Controller/AnnotatedUsersController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function getUsersAction()
4646
public function getUserAction($slug)
4747
{} // [GET] /users/{slug}
4848

49+
/**
50+
* @Route(requirements={"slug" = "[a-z]+", "id" = "\d+"}, options={"expose"=true})
51+
*/
52+
public function getUserPostAction($slug, $id)
53+
{} // [GET] /users/{slug}/posts/{id}
54+
4955
/**
5056
* @Patch
5157
*/

Tests/Fixtures/Etalon/annotated_users_controller.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ get_user_comment:
2323
controller: ::getUserCommentAction
2424
requirements: {_method: GET, slug: '[a-z]+', id: '\d+'}
2525

26+
get_user_post:
27+
pattern: /users/{slug}/posts/{id}.{_format}
28+
controller: ::getUserPostAction
29+
options:
30+
expose: true
31+
requirements: {_method: GET, slug: '[a-z]+', id: '\d+'}
32+
2633
rate_user:
2734
pattern: /users/{slug}/rate.{_format}
2835
controller: ::rateUserAction
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://friendsofsymfony.github.com/schema/rest"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://friendsofsymfony.github.com/schema/rest
5+
http://friendsofsymfony.github.com/schema/rest/routing-1.0.xsd"
6+
>
7+
<route id="get_users" pattern="/users">
8+
<default key="_controller">FOSRestBundle:UsersController:getUsers</default>
9+
<option key="expose">true</option>
10+
</route>
11+
</routes>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
users:
2+
type: rest
3+
resource: FOS\RestBundle\Tests\Fixtures\Controller\UsersController
4+
options:
5+
expose: true

Tests/Routing/Loader/RestRouteLoaderTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testAnnotatedUsersFixture()
9494
$etalonRoutes = $this->loadEtalonRoutesInfo('annotated_users_controller.yml');
9595

9696
$this->assertTrue($collection instanceof RestRouteCollection);
97-
$this->assertEquals(23, count($collection->all()));
97+
$this->assertEquals(24, count($collection->all()));
9898

9999
foreach ($etalonRoutes as $name => $params) {
100100
$route = $collection->get($name);
@@ -107,8 +107,14 @@ public function testAnnotatedUsersFixture()
107107
$this->assertEquals($params['condition'], $route->getCondition(), 'condition failed to match for '.$name);
108108
}
109109

110+
if (isset($params['options'])) {
111+
foreach ($params['options'] as $option => $value) {
112+
$this->assertEquals($value, $route->getOption($option));
113+
}
114+
}
110115
}
111116
}
117+
112118
/**
113119
* Test that annotated UsersController RESTful class gets parsed correctly with condition option (expression-language).
114120
*/

Tests/Routing/Loader/RestXmlCollectionLoaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ public function testManualRoutesWithDefaultFormat()
108108
$this->assertEquals('xml', $route->getDefault('_format'));
109109
}
110110

111+
public function testForwardOptions()
112+
{
113+
$collection = $this->loadFromXmlCollectionFixture(
114+
'routes_with_options.xml',
115+
true,
116+
array(
117+
'json' => false,
118+
'xml' => false,
119+
'html' => true,
120+
),
121+
'xml'
122+
);
123+
124+
foreach ($collection as $route) {
125+
$this->assertTrue('true' === $route->getOption('expose'));
126+
}
127+
}
111128
/**
112129
* Load routes collection from XML fixture routes under Tests\Fixtures directory.
113130
*

Tests/Routing/Loader/RestYamlCollectionLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ public function testNamedPrefixedReportsFixtureHasNoDuplicates()
9292
$this->assertEquals(count($names), count(array_unique($names)));
9393
}
9494

95+
public function testForwardOptions()
96+
{
97+
$collection = $this->loadFromYamlCollectionFixture('routes_with_options.yml');
98+
99+
foreach ($collection as $route) {
100+
$this->assertTrue($route->getOption('expose'));
101+
}
102+
}
103+
95104
public function testManualRoutes()
96105
{
97106
$collection = $this->loadFromYamlCollectionFixture('routes.yml');

0 commit comments

Comments
 (0)