<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Templating;
use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* TemplateNameParser parsers template name from the short notation
* "bundle:section:template.engine.format" to an array of
* template parameters.
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
class TemplateNameParser extends BaseTemplateNameParser
{
protected $kernel;
* Constructor.
* @param KernelInterface $kernel A KernelInterface instance
public function __construct(KernelInterface $kernel)
$this->kernel = $kernel;
}
* {@inheritdoc}
public function parse($name)
if (is_array($name)) {
return $name;
// normalize name
$name = str_replace(':/' , ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
if (false !== strpos($name, '..')) {
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
$parts = explode(':', $name);
if (3 !== count($parts)) {
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
$elements = explode('.', $parts[2]);
if (3 !== count($elements)) {
$parameters = array(
'bundle' => $parts[0],
'controller' => $parts[1],
'name' => $elements[0],
'format' => $elements[1],
'engine' => $elements[2],
);
if ($parameters['bundle']) {
try {
$this->kernel->getBundle($parameters['bundle']);
} catch (\Exception $e) {
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
return $parameters;