Skip to content

[FrameworkBundle] Made TemplateNameParser able to parse templates in the @-notation (#5660) #7818

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
);
}

Expand Down