Skip to content

Commit cf3adf2

Browse files
committed
[FrameworkBundle] Made TemplateNameParser able to parse templates in the @-notation (symfony#5660)
1 parent 81f37ba commit cf3adf2

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,24 @@ public function parse($name)
5656
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
5757
}
5858

59-
if (!preg_match('/^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {
60-
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
59+
if (!$name) {
60+
throw new \InvalidArgumentException('Template names should not be empty.');
6161
}
6262

63-
$template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
63+
// The following blocks are optimized for speed. Named sub-patterns
64+
// should not be used because they significantly lower the performance
65+
// of the code.
66+
if ('@' !== $name[0] && preg_match('#^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$#', $name, $matches)) {
67+
$template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
68+
} elseif (preg_match('#^(@([^/]+)/)?(([^/]+)/)?(.+)\.([^\.]+)\.([^\.]+)$#', $name, $matches)) {
69+
if ($matches[2]) {
70+
$matches[2] .= 'Bundle';
71+
}
72+
73+
$template = new TemplateReference($matches[2], $matches[4], $matches[5], $matches[6], $matches[7]);
74+
} else {
75+
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
76+
}
6477

6578
if ($template->get('bundle')) {
6679
try {

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,37 @@ protected function tearDown()
4444
/**
4545
* @dataProvider getLogicalNameToTemplateProvider
4646
*/
47-
public function testParse($name, $ref)
47+
public function testParseLogicalName($logicalName, $atName, $ref)
4848
{
49-
$template = $this->parser->parse($name);
49+
$template = $this->parser->parse($logicalName);
5050

51-
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
52-
$this->assertEquals($template->getLogicalName(), $name);
51+
$this->assertEquals($logicalName, $template->getLogicalName());
52+
$this->assertEquals($logicalName, $ref->getLogicalName());
53+
}
54+
55+
/**
56+
* @dataProvider getLogicalNameToTemplateProvider
57+
*/
58+
public function testParseAtName($logicalName, $atName, $ref)
59+
{
60+
$template = $this->parser->parse($atName);
61+
62+
$this->assertEquals($logicalName, $template->getLogicalName());
63+
$this->assertEquals($logicalName, $ref->getLogicalName());
5364
}
5465

5566
public function getLogicalNameToTemplateProvider()
5667
{
5768
return array(
58-
array('FooBundle:Post:index.html.php', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php')),
59-
array('FooBundle:Post:index.html.twig', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'twig')),
60-
array('FooBundle:Post:index.xml.php', new TemplateReference('FooBundle', 'Post', 'index', 'xml', 'php')),
61-
array('SensioFooBundle:Post:index.html.php', new TemplateReference('SensioFooBundle', 'Post', 'index', 'html', 'php')),
62-
array('SensioCmsFooBundle:Post:index.html.php', new TemplateReference('SensioCmsFooBundle', 'Post', 'index', 'html', 'php')),
63-
array(':Post:index.html.php', new TemplateReference('', 'Post', 'index', 'html', 'php')),
64-
array('::index.html.php', new TemplateReference('', '', 'index', 'html', 'php')),
65-
array('FooBundle:Post:foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')),
69+
array('FooBundle:Post:index.html.php', '@Foo/Post/index.html.php', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php')),
70+
array('FooBundle:Post:index.html.twig', '@Foo/Post/index.html.twig', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'twig')),
71+
array('FooBundle:Post:index.xml.php', '@Foo/Post/index.xml.php', new TemplateReference('FooBundle', 'Post', 'index', 'xml', 'php')),
72+
array('SensioFooBundle:Post:index.html.php', '@SensioFoo/Post/index.html.php', new TemplateReference('SensioFooBundle', 'Post', 'index', 'html', 'php')),
73+
array('SensioCmsFooBundle:Post:index.html.php', '@SensioCmsFoo/Post/index.html.php', new TemplateReference('SensioCmsFooBundle', 'Post', 'index', 'html', 'php')),
74+
array('FooBundle::index.html.php', '@Foo/index.html.php', new TemplateReference('FooBundle', '', 'index', 'html', 'php')),
75+
array(':Post:index.html.php', 'Post/index.html.php', new TemplateReference('', 'Post', 'index', 'html', 'php')),
76+
array('::index.html.php', 'index.html.php', new TemplateReference('', '', 'index', 'html', 'php')),
77+
array('FooBundle:Post:foo.bar.index.html.php', '@Foo/Post/foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')),
6678
);
6779
}
6880

0 commit comments

Comments
 (0)