Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fea27c1

Browse files
committed
merged branch Tobion/scheme-method-def-tolerance (PR #7268)
This PR was merged into the 2.2 branch. Commits ------- 54c333d [Routing] unify and fix the loader tests 41ad9d8 [Routing] make xml loader more tolerant Discussion ---------- [Routing] make xml loader more tolerant schemes and methods may also be delimited by whitespace, comma or pipe. Fixes #6049 (comment) this eases migration as now `methods="GET|POST"` also works the second commit unifies the tests and fixes some strange assertions that were useless | Q | A | ------------- | --- | Bug fix? | [yes] | New feature? | [yes but not really] | BC breaks? | [no] | Deprecations? | [no] | Tests pass? | [yes] | License | MIT
2 parents 2850fca + 54c333d commit fea27c1

14 files changed

+71
-79
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
126126
$node->removeAttribute('pattern');
127127
}
128128

129-
$schemes = array_filter(explode(' ', $node->getAttribute('schemes')));
130-
$methods = array_filter(explode(' ', $node->getAttribute('methods')));
129+
$schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
130+
$methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
131131

132132
list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
133133

@@ -154,8 +154,8 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
154154
$type = $node->getAttribute('type');
155155
$prefix = $node->getAttribute('prefix');
156156
$host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
157-
$schemes = $node->hasAttribute('schemes') ? array_filter(explode(' ', $node->getAttribute('schemes'))) : null;
158-
$methods = $node->hasAttribute('methods') ? array_filter(explode(' ', $node->getAttribute('methods'))) : null;
157+
$schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
158+
$methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
159159

160160
list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
161161

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
<xsd:element name="routes" type="routes" />
1919

20-
<xsd:simpleType name="word">
21-
<xsd:restriction base="xsd:string">
22-
<xsd:pattern value="([a-zA-Z]){3,}"/>
23-
</xsd:restriction>
24-
</xsd:simpleType>
25-
26-
<xsd:simpleType name="stringlist">
27-
<xsd:list itemType="word"/>
28-
</xsd:simpleType>
29-
3020
<xsd:complexType name="routes">
3121
<xsd:choice minOccurs="0" maxOccurs="unbounded">
3222
<xsd:element name="import" type="import" />
@@ -49,8 +39,8 @@
4939
<xsd:attribute name="path" type="xsd:string" />
5040
<xsd:attribute name="pattern" type="xsd:string" />
5141
<xsd:attribute name="host" type="xsd:string" />
52-
<xsd:attribute name="schemes" type="stringlist" />
53-
<xsd:attribute name="methods" type="stringlist" />
42+
<xsd:attribute name="schemes" type="xsd:string" />
43+
<xsd:attribute name="methods" type="xsd:string" />
5444
</xsd:complexType>
5545

5646
<xsd:complexType name="import">
@@ -60,8 +50,8 @@
6050
<xsd:attribute name="type" type="xsd:string" />
6151
<xsd:attribute name="prefix" type="xsd:string" />
6252
<xsd:attribute name="host" type="xsd:string" />
63-
<xsd:attribute name="schemes" type="stringlist" />
64-
<xsd:attribute name="methods" type="stringlist" />
53+
<xsd:attribute name="schemes" type="xsd:string" />
54+
<xsd:attribute name="methods" type="xsd:string" />
6555
</xsd:complexType>
6656

6757
<xsd:complexType name="element">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route pattern="/test"></route>
7+
<route path="/test"></route>
88
</routes>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<r:route id="blog_show" pattern="/blog/{slug}" host="{_locale}.example.com">
7+
<r:route id="blog_show" path="/blog/{slug}" host="{_locale}.example.com">
88
<r:default key="_controller">MyBundle:Blog:show</r:default>
99
<requirement xmlns="http://symfony.com/schema/routing" key="slug">\w+</requirement>
1010
<r2:requirement xmlns:r2="http://symfony.com/schema/routing" key="_locale">en|fr|de</r2:requirement>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route id="blog_show" pattern="/blog/{slug}">
7+
<route id="blog_show" path="/blog/{slug}">
88
<default key="_controller">MyBundle:Blog:show</default>
99
<requirement key="_method">GET</requirement>
10-
<option key="segment_separators">/</option>
1110
<!-- </route> -->
1211
</routes>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route id="blog_show" pattern="/blog/{slug}">
7+
<route id="blog_show" path="/blog/{slug}">
88
<default key="_controller">MyBundle:Blog:show</default>
99
<requirement key="_method">GET</requirement>
1010
<option key="compiler_class">RouteCompiler</option>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
$collection->add('blog_show', new Route(
77
'/blog/{slug}',
88
array('_controller' => 'MyBlogBundle:Blog:show'),
9-
array('_method' => 'GET', 'locale' => '\w+', '_scheme' => 'https'),
9+
array('locale' => '\w+'),
1010
array('compiler_class' => 'RouteCompiler'),
11-
'{locale}.example.com'
11+
'{locale}.example.com',
12+
array('https'),
13+
array('GET','POST','put','OpTiOnS')
1214
));
1315
$collection->add('blog_show_legacy', new Route(
1416
'/blog/{slug}',
1517
array('_controller' => 'MyBlogBundle:Blog:show'),
16-
array('locale' => '\w+'),
18+
array('_method' => 'GET|POST|put|OpTiOnS', '_scheme' => 'https', 'locale' => '\w+',),
1719
array('compiler_class' => 'RouteCompiler'),
18-
'{locale}.example.com',
19-
array('https'),
20-
array('GET')
20+
'{locale}.example.com'
2121
));
2222

2323
return $collection;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route id="blog_show" path="/blog/{slug}" host="{locale}.example.com" methods="GET" schemes="https">
7+
<route id="blog_show" path="/blog/{slug}" host="{locale}.example.com" methods="GET|POST put,OpTiOnS" schemes="hTTps">
88
<default key="_controller">MyBundle:Blog:show</default>
99
<requirement key="locale">\w+</requirement>
1010
<option key="compiler_class">RouteCompiler</option>
1111
</route>
1212

1313
<route id="blog_show_legacy" pattern="/blog/{slug}" host="{locale}.example.com">
1414
<default key="_controller">MyBundle:Blog:show</default>
15-
<requirement key="_method">GET</requirement>
16-
<requirement key="_scheme">https</requirement>
15+
<requirement key="_method">GET|POST|put|OpTiOnS</requirement>
16+
<requirement key="_scheme">hTTps</requirement>
1717
<requirement key="locale">\w+</requirement>
1818
<option key="compiler_class">RouteCompiler</option>
1919
</route>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
blog_show:
22
path: /blog/{slug}
3-
defaults: { _controller: MyBlogBundle:Blog:show }
3+
defaults: { _controller: "MyBundle:Blog:show" }
44
host: "{locale}.example.com"
55
requirements: { 'locale': '\w+' }
6-
methods: ['GET']
6+
methods: ['GET','POST','put','OpTiOnS']
77
schemes: ['https']
88
options:
99
compiler_class: RouteCompiler
1010

1111
blog_show_legacy:
1212
pattern: /blog/{slug}
13-
defaults: { _controller: MyBlogBundle:Blog:show }
13+
defaults: { _controller: "MyBundle:Blog:show" }
1414
host: "{locale}.example.com"
15-
requirements: { '_method': 'GET', 'locale': '\w+', _scheme: 'https' }
15+
requirements: { '_method': 'GET|POST|put|OpTiOnS', _scheme: https, 'locale': '\w+' }
1616
options:
1717
compiler_class: RouteCompiler

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<import resource="validpattern.xml" prefix="/{foo}" host="{locale}.example.com">
7+
<import resource="validpattern.xml" prefix="/{foo}" host="">
88
<default key="foo">123</default>
99
<requirement key="foo">\d+</requirement>
1010
<option key="foo">bar</option>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
blog_show:
1+
_blog:
22
resource: validpattern.yml
33
prefix: /{foo}
44
defaults: { 'foo': '123' }
55
requirements: { 'foo': '\d+' }
66
options: { 'foo': 'bar' }
7-
host: "{locale}.example.com"
7+
host: ""

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public function testLoadWithRoute()
4444
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
4545

4646
foreach ($routes as $route) {
47-
$this->assertEquals('/blog/{slug}', $route->getPath());
48-
$this->assertEquals('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
49-
$this->assertEquals('GET', $route->getRequirement('_method'));
50-
$this->assertEquals('https', $route->getRequirement('_scheme'));
51-
$this->assertEquals('{locale}.example.com', $route->getHost());
52-
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));
47+
$this->assertSame('/blog/{slug}', $route->getPath());
48+
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
49+
$this->assertSame('{locale}.example.com', $route->getHost());
50+
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
51+
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
52+
$this->assertEquals(array('https'), $route->getSchemes());
5353
}
5454
}
5555
}

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public function testLoadWithRoute()
4343

4444
$this->assertCount(2, $routes, 'Two routes are loaded');
4545
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
46-
$route = $routes['blog_show'];
47-
$this->assertEquals('/blog/{slug}', $route->getPath());
48-
$this->assertEquals('MyBundle:Blog:show', $route->getDefault('_controller'));
49-
$this->assertEquals('GET', $route->getRequirement('_method'));
50-
$this->assertEquals('https', $route->getRequirement('_scheme'));
51-
$this->assertEquals('\w+', $route->getRequirement('locale'));
52-
$this->assertEquals('{locale}.example.com', $route->getHost());
53-
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));
46+
47+
foreach ($routes as $route) {
48+
$this->assertSame('/blog/{slug}', $route->getPath());
49+
$this->assertSame('{locale}.example.com', $route->getHost());
50+
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
51+
$this->assertSame('\w+', $route->getRequirement('locale'));
52+
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
53+
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
54+
$this->assertEquals(array('https'), $route->getSchemes());
55+
}
5456
}
5557

5658
public function testLoadWithNamespacePrefix()
@@ -61,12 +63,12 @@ public function testLoadWithNamespacePrefix()
6163
$this->assertCount(1, $routeCollection->all(), 'One route is loaded');
6264

6365
$route = $routeCollection->get('blog_show');
64-
$this->assertEquals('/blog/{slug}', $route->getPath());
65-
$this->assertEquals('MyBundle:Blog:show', $route->getDefault('_controller'));
66-
$this->assertEquals('\w+', $route->getRequirement('slug'));
67-
$this->assertEquals('en|fr|de', $route->getRequirement('_locale'));
68-
$this->assertEquals('{_locale}.example.com', $route->getHost());
69-
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));
66+
$this->assertSame('/blog/{slug}', $route->getPath());
67+
$this->assertSame('{_locale}.example.com', $route->getHost());
68+
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
69+
$this->assertSame('\w+', $route->getRequirement('slug'));
70+
$this->assertSame('en|fr|de', $route->getRequirement('_locale'));
71+
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
7072
}
7173

7274
public function testLoadWithImport()
@@ -79,12 +81,11 @@ public function testLoadWithImport()
7981
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
8082

8183
foreach ($routes as $route) {
82-
$this->assertEquals('/{foo}/blog/{slug}', $routes['blog_show']->getPath());
83-
$this->assertEquals('MyBundle:Blog:show', $routes['blog_show']->getDefault('_controller'));
84-
$this->assertEquals('123', $routes['blog_show']->getDefault('foo'));
85-
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
86-
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
87-
$this->assertEquals('{locale}.example.com', $routes['blog_show']->getHost());
84+
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
85+
$this->assertSame('123', $route->getDefault('foo'));
86+
$this->assertSame('\d+', $route->getRequirement('foo'));
87+
$this->assertSame('bar', $route->getOption('foo'));
88+
$this->assertSame('', $route->getHost());
8889
}
8990
}
9091

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testLoadSpecialRouteName()
7373
$this->assertSame('/true', $route->getPath());
7474
}
7575

76-
public function testLoadWithPattern()
76+
public function testLoadWithRoute()
7777
{
7878
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
7979
$routeCollection = $loader->load('validpattern.yml');
@@ -83,13 +83,13 @@ public function testLoadWithPattern()
8383
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
8484

8585
foreach ($routes as $route) {
86-
$this->assertEquals('/blog/{slug}', $route->getPath());
87-
$this->assertEquals('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
88-
$this->assertEquals('GET', $route->getRequirement('_method'));
89-
$this->assertEquals('https', $route->getRequirement('_scheme'));
90-
$this->assertEquals('\w+', $route->getRequirement('locale'));
91-
$this->assertEquals('{locale}.example.com', $route->getHost());
92-
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));
86+
$this->assertSame('/blog/{slug}', $route->getPath());
87+
$this->assertSame('{locale}.example.com', $route->getHost());
88+
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
89+
$this->assertSame('\w+', $route->getRequirement('locale'));
90+
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
91+
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
92+
$this->assertEquals(array('https'), $route->getSchemes());
9393
}
9494
}
9595

@@ -101,11 +101,13 @@ public function testLoadWithResource()
101101

102102
$this->assertCount(2, $routes, 'Two routes are loaded');
103103
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
104-
$this->assertEquals('/{foo}/blog/{slug}', $routes['blog_show']->getPath());
105-
$this->assertEquals('MyBlogBundle:Blog:show', $routes['blog_show']->getDefault('_controller'));
106-
$this->assertEquals('123', $routes['blog_show']->getDefault('foo'));
107-
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
108-
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
109-
$this->assertEquals('{locale}.example.com', $routes['blog_show']->getHost());
104+
105+
foreach ($routes as $route) {
106+
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
107+
$this->assertSame('123', $route->getDefault('foo'));
108+
$this->assertSame('\d+', $route->getRequirement('foo'));
109+
$this->assertSame('bar', $route->getOption('foo'));
110+
$this->assertSame('', $route->getHost());
111+
}
110112
}
111113
}

0 commit comments

Comments
 (0)