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

Skip to content

[Routing] make xml loader more tolerant #7268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
$node->removeAttribute('pattern');
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 2 + ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prevents backtracking. So its a performance thing (which I also implemented for UrlMatching/route compiling some time ago).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it's a micro-optim which:

  • makes the code unclear,
  • is really useless as you want to use the compiled version where perf matters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well its standard syntax for regular expressions. Just less known.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw where could there be any backtracking here ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but thanks for the tip++ anyway!)


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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@

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

<xsd:simpleType name="word">
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z]){3,}"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="stringlist">
<xsd:list itemType="word"/>
</xsd:simpleType>

<xsd:complexType name="routes">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="import" type="import" />
Expand All @@ -49,8 +39,8 @@
<xsd:attribute name="path" type="xsd:string" />
<xsd:attribute name="pattern" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="stringlist" />
<xsd:attribute name="methods" type="stringlist" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="import">
Expand All @@ -60,8 +50,8 @@
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="prefix" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="stringlist" />
<xsd:attribute name="methods" type="stringlist" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="element">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route pattern="/test"></route>
<route path="/test"></route>
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<r:route id="blog_show" pattern="/blog/{slug}" host="{_locale}.example.com">
<r:route id="blog_show" path="/blog/{slug}" host="{_locale}.example.com">
<r:default key="_controller">MyBundle:Blog:show</r:default>
<requirement xmlns="http://symfony.com/schema/routing" key="slug">\w+</requirement>
<r2:requirement xmlns:r2="http://symfony.com/schema/routing" key="_locale">en|fr|de</r2:requirement>
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/Routing/Tests/Fixtures/nonvalid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog_show" pattern="/blog/{slug}">
<route id="blog_show" path="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<option key="segment_separators">/</option>
<!-- </route> -->
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog_show" pattern="/blog/{slug}">
<route id="blog_show" path="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<option key="compiler_class">RouteCompiler</option>
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
$collection->add('blog_show', new Route(
'/blog/{slug}',
array('_controller' => 'MyBlogBundle:Blog:show'),
array('_method' => 'GET', 'locale' => '\w+', '_scheme' => 'https'),
array('locale' => '\w+'),
array('compiler_class' => 'RouteCompiler'),
'{locale}.example.com'
'{locale}.example.com',
array('https'),
array('GET','POST','put','OpTiOnS')
));
$collection->add('blog_show_legacy', new Route(
'/blog/{slug}',
array('_controller' => 'MyBlogBundle:Blog:show'),
array('locale' => '\w+'),
array('_method' => 'GET|POST|put|OpTiOnS', '_scheme' => 'https', 'locale' => '\w+',),
array('compiler_class' => 'RouteCompiler'),
'{locale}.example.com',
array('https'),
array('GET')
'{locale}.example.com'
));

return $collection;
6 changes: 3 additions & 3 deletions src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

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

<route id="blog_show_legacy" pattern="/blog/{slug}" host="{locale}.example.com">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="_method">GET</requirement>
<requirement key="_scheme">https</requirement>
<requirement key="_method">GET|POST|put|OpTiOnS</requirement>
<requirement key="_scheme">hTTps</requirement>
<requirement key="locale">\w+</requirement>
<option key="compiler_class">RouteCompiler</option>
</route>
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
blog_show:
path: /blog/{slug}
defaults: { _controller: MyBlogBundle:Blog:show }
defaults: { _controller: "MyBundle:Blog:show" }
host: "{locale}.example.com"
requirements: { 'locale': '\w+' }
methods: ['GET']
methods: ['GET','POST','put','OpTiOnS']
schemes: ['https']
options:
compiler_class: RouteCompiler

blog_show_legacy:
pattern: /blog/{slug}
defaults: { _controller: MyBlogBundle:Blog:show }
defaults: { _controller: "MyBundle:Blog:show" }
host: "{locale}.example.com"
requirements: { '_method': 'GET', 'locale': '\w+', _scheme: 'https' }
requirements: { '_method': 'GET|POST|put|OpTiOnS', _scheme: https, 'locale': '\w+' }
options:
compiler_class: RouteCompiler
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="validpattern.xml" prefix="/{foo}" host="{locale}.example.com">
<import resource="validpattern.xml" prefix="/{foo}" host="">
<default key="foo">123</default>
<requirement key="foo">\d+</requirement>
<option key="foo">bar</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
blog_show:
_blog:
resource: validpattern.yml
prefix: /{foo}
defaults: { 'foo': '123' }
requirements: { 'foo': '\d+' }
options: { 'foo': 'bar' }
host: "{locale}.example.com"
host: ""
12 changes: 6 additions & 6 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function testLoadWithRoute()
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);

foreach ($routes as $route) {
$this->assertEquals('/blog/{slug}', $route->getPath());
$this->assertEquals('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
$this->assertEquals('GET', $route->getRequirement('_method'));
$this->assertEquals('https', $route->getRequirement('_scheme'));
$this->assertEquals('{locale}.example.com', $route->getHost());
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));
$this->assertSame('/blog/{slug}', $route->getPath());
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
$this->assertSame('{locale}.example.com', $route->getHost());
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
$this->assertEquals(array('https'), $route->getSchemes());
}
}
}
41 changes: 21 additions & 20 deletions src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ public function testLoadWithRoute()

$this->assertCount(2, $routes, 'Two routes are loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
$route = $routes['blog_show'];
$this->assertEquals('/blog/{slug}', $route->getPath());
$this->assertEquals('MyBundle:Blog:show', $route->getDefault('_controller'));
$this->assertEquals('GET', $route->getRequirement('_method'));
$this->assertEquals('https', $route->getRequirement('_scheme'));
$this->assertEquals('\w+', $route->getRequirement('locale'));
$this->assertEquals('{locale}.example.com', $route->getHost());
$this->assertEquals('RouteCompiler', $route->getOption('compiler_class'));

foreach ($routes as $route) {
$this->assertSame('/blog/{slug}', $route->getPath());
$this->assertSame('{locale}.example.com', $route->getHost());
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
$this->assertSame('\w+', $route->getRequirement('locale'));
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
$this->assertEquals(array('https'), $route->getSchemes());
}
}

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

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

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

foreach ($routes as $route) {
$this->assertEquals('/{foo}/blog/{slug}', $routes['blog_show']->getPath());
$this->assertEquals('MyBundle:Blog:show', $routes['blog_show']->getDefault('_controller'));
$this->assertEquals('123', $routes['blog_show']->getDefault('foo'));
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
$this->assertEquals('{locale}.example.com', $routes['blog_show']->getHost());
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
$this->assertSame('123', $route->getDefault('foo'));
$this->assertSame('\d+', $route->getRequirement('foo'));
$this->assertSame('bar', $route->getOption('foo'));
$this->assertSame('', $route->getHost());
}
}

Expand Down
30 changes: 16 additions & 14 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testLoadSpecialRouteName()
$this->assertSame('/true', $route->getPath());
}

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

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

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

$this->assertCount(2, $routes, 'Two routes are loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
$this->assertEquals('/{foo}/blog/{slug}', $routes['blog_show']->getPath());
$this->assertEquals('MyBlogBundle:Blog:show', $routes['blog_show']->getDefault('_controller'));
$this->assertEquals('123', $routes['blog_show']->getDefault('foo'));
$this->assertEquals('\d+', $routes['blog_show']->getRequirement('foo'));
$this->assertEquals('bar', $routes['blog_show']->getOption('foo'));
$this->assertEquals('{locale}.example.com', $routes['blog_show']->getHost());

foreach ($routes as $route) {
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
$this->assertSame('123', $route->getDefault('foo'));
$this->assertSame('\d+', $route->getRequirement('foo'));
$this->assertSame('bar', $route->getOption('foo'));
$this->assertSame('', $route->getHost());
}
}
}