From cf3adf2efab9ffb85fb322f7bc53931a69866d64 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 23 Apr 2013 14:43:06 +0200 Subject: [PATCH] [FrameworkBundle] Made TemplateNameParser able to parse templates in the @-notation (#5660) --- .../Templating/TemplateNameParser.php | 19 ++++++++-- .../Templating/TemplateNameParserTest.php | 36 ++++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php index ef2ae0800886a..9a3e0334e0d6d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php @@ -56,11 +56,24 @@ public function parse($name) throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name)); } - if (!preg_match('/^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) { - throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); + if (!$name) { + throw new \InvalidArgumentException('Template names should not be empty.'); } - $template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]); + // The following blocks are optimized for speed. Named sub-patterns + // should not be used because they significantly lower the performance + // of the code. + if ('@' !== $name[0] && preg_match('#^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$#', $name, $matches)) { + $template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]); + } elseif (preg_match('#^(@([^/]+)/)?(([^/]+)/)?(.+)\.([^\.]+)\.([^\.]+)$#', $name, $matches)) { + if ($matches[2]) { + $matches[2] .= 'Bundle'; + } + + $template = new TemplateReference($matches[2], $matches[4], $matches[5], $matches[6], $matches[7]); + } else { + throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); + } if ($template->get('bundle')) { try { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php index 2a1544c91b207..6d72a5eec5ffc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php @@ -44,25 +44,37 @@ protected function tearDown() /** * @dataProvider getLogicalNameToTemplateProvider */ - public function testParse($name, $ref) + public function testParseLogicalName($logicalName, $atName, $ref) { - $template = $this->parser->parse($name); + $template = $this->parser->parse($logicalName); - $this->assertEquals($template->getLogicalName(), $ref->getLogicalName()); - $this->assertEquals($template->getLogicalName(), $name); + $this->assertEquals($logicalName, $template->getLogicalName()); + $this->assertEquals($logicalName, $ref->getLogicalName()); + } + + /** + * @dataProvider getLogicalNameToTemplateProvider + */ + public function testParseAtName($logicalName, $atName, $ref) + { + $template = $this->parser->parse($atName); + + $this->assertEquals($logicalName, $template->getLogicalName()); + $this->assertEquals($logicalName, $ref->getLogicalName()); } public function getLogicalNameToTemplateProvider() { return array( - array('FooBundle:Post:index.html.php', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php')), - array('FooBundle:Post:index.html.twig', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'twig')), - array('FooBundle:Post:index.xml.php', new TemplateReference('FooBundle', 'Post', 'index', 'xml', 'php')), - array('SensioFooBundle:Post:index.html.php', new TemplateReference('SensioFooBundle', 'Post', 'index', 'html', 'php')), - array('SensioCmsFooBundle:Post:index.html.php', new TemplateReference('SensioCmsFooBundle', 'Post', 'index', 'html', 'php')), - array(':Post:index.html.php', new TemplateReference('', 'Post', 'index', 'html', 'php')), - array('::index.html.php', new TemplateReference('', '', 'index', 'html', 'php')), - array('FooBundle:Post:foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')), + array('FooBundle:Post:index.html.php', '@Foo/Post/index.html.php', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php')), + array('FooBundle:Post:index.html.twig', '@Foo/Post/index.html.twig', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'twig')), + array('FooBundle:Post:index.xml.php', '@Foo/Post/index.xml.php', new TemplateReference('FooBundle', 'Post', 'index', 'xml', 'php')), + array('SensioFooBundle:Post:index.html.php', '@SensioFoo/Post/index.html.php', new TemplateReference('SensioFooBundle', 'Post', 'index', 'html', 'php')), + array('SensioCmsFooBundle:Post:index.html.php', '@SensioCmsFoo/Post/index.html.php', new TemplateReference('SensioCmsFooBundle', 'Post', 'index', 'html', 'php')), + array('FooBundle::index.html.php', '@Foo/index.html.php', new TemplateReference('FooBundle', '', 'index', 'html', 'php')), + array(':Post:index.html.php', 'Post/index.html.php', new TemplateReference('', 'Post', 'index', 'html', 'php')), + array('::index.html.php', 'index.html.php', new TemplateReference('', '', 'index', 'html', 'php')), + array('FooBundle:Post:foo.bar.index.html.php', '@Foo/Post/foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')), ); }