Skip to content

Commit 664d055

Browse files
committed
bug symfony#10002 Routing condition bugfix (marco-jantke)
This PR was submitted for the 2.4-dev branch but it was merged into the 2.4 branch instead (closes symfony#10002). Discussion ---------- Routing condition bugfix [Routing] Fixed inheriting of condition to sub routes for xml and yaml configuration. Commits ------- bb7e15e Routing condition bugfix
2 parents 07de761 + 0a2bb7b commit 664d055

File tree

8 files changed

+27
-6
lines changed

8 files changed

+27
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
167167
if (null !== $host) {
168168
$subCollection->setHost($host);
169169
}
170+
if (null !== $condition) {
171+
$subCollection->setCondition($condition);
172+
}
170173
if (null !== $schemes) {
171174
$subCollection->setSchemes($schemes);
172175
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
146146
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
147147
$options = isset($config['options']) ? $config['options'] : array();
148148
$host = isset($config['host']) ? $config['host'] : null;
149+
$condition = isset($config['condition']) ? $config['condition'] : null;
149150
$schemes = isset($config['schemes']) ? $config['schemes'] : null;
150151
$methods = isset($config['methods']) ? $config['methods'] : null;
151152

@@ -157,6 +158,9 @@ protected function parseImport(RouteCollection $collection, array $config, $path
157158
if (null !== $host) {
158159
$subCollection->setHost($host);
159160
}
161+
if (null !== $condition) {
162+
$subCollection->setCondition($condition);
163+
}
160164
if (null !== $schemes) {
161165
$subCollection->setSchemes($schemes);
162166
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
<option key="compiler_class">RouteCompiler</option>
2121
<condition>context.getMethod() == "GET"</condition>
2222
</route>
23+
24+
<route id="blog_show_inherited" path="/blog/{slug}" />
2325
</routes>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ blog_show_legacy:
1717
condition: 'context.getMethod() == "GET"'
1818
options:
1919
compiler_class: RouteCompiler
20+
21+
blog_show_inherited:
22+
path: /blog/{slug}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<default key="foo">123</default>
99
<requirement key="foo">\d+</requirement>
1010
<option key="foo">bar</option>
11+
<condition>context.getMethod() == "POST"</condition>
1112
</import>
1213
</routes>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ _blog:
55
requirements: { 'foo': '\d+' }
66
options: { 'foo': 'bar' }
77
host: ""
8+
condition: 'context.getMethod() == "POST"'

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public function testLoadWithRoute()
3434
$routeCollection = $loader->load('validpattern.xml');
3535
$routes = $routeCollection->all();
3636

37-
$this->assertCount(2, $routes, 'Two routes are loaded');
37+
$this->assertCount(3, $routes, 'Three routes are loaded');
3838
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
3939

40-
foreach ($routes as $route) {
40+
$identicalRoutes = array_slice($routes, 0, 2);
41+
42+
foreach ($identicalRoutes as $route) {
4143
$this->assertSame('/blog/{slug}', $route->getPath());
4244
$this->assertSame('{locale}.example.com', $route->getHost());
4345
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
@@ -72,7 +74,7 @@ public function testLoadWithImport()
7274
$routeCollection = $loader->load('validresource.xml');
7375
$routes = $routeCollection->all();
7476

75-
$this->assertCount(2, $routes, 'Two routes are loaded');
77+
$this->assertCount(3, $routes, 'Three routes are loaded');
7678
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
7779

7880
foreach ($routes as $route) {
@@ -81,6 +83,7 @@ public function testLoadWithImport()
8183
$this->assertSame('\d+', $route->getRequirement('foo'));
8284
$this->assertSame('bar', $route->getOption('foo'));
8385
$this->assertSame('', $route->getHost());
86+
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
8487
}
8588
}
8689

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public function testLoadWithRoute()
6868
$routeCollection = $loader->load('validpattern.yml');
6969
$routes = $routeCollection->all();
7070

71-
$this->assertCount(2, $routes, 'Two routes are loaded');
71+
$this->assertCount(3, $routes, 'Three routes are loaded');
7272
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
7373

74-
foreach ($routes as $route) {
74+
$identicalRoutes = array_slice($routes, 0, 2);
75+
76+
foreach ($identicalRoutes as $route) {
7577
$this->assertSame('/blog/{slug}', $route->getPath());
7678
$this->assertSame('{locale}.example.com', $route->getHost());
7779
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
@@ -89,7 +91,7 @@ public function testLoadWithResource()
8991
$routeCollection = $loader->load('validresource.yml');
9092
$routes = $routeCollection->all();
9193

92-
$this->assertCount(2, $routes, 'Two routes are loaded');
94+
$this->assertCount(3, $routes, 'Three routes are loaded');
9395
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
9496

9597
foreach ($routes as $route) {
@@ -98,6 +100,8 @@ public function testLoadWithResource()
98100
$this->assertSame('\d+', $route->getRequirement('foo'));
99101
$this->assertSame('bar', $route->getOption('foo'));
100102
$this->assertSame('', $route->getHost());
103+
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
101104
}
102105
}
106+
103107
}

0 commit comments

Comments
 (0)