This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php
Fabien Potencier 7e573f4f8a Merge branch '2.3' into 2.5
* 2.3:
  Revert "[DependencyInjection] backport perf optim"
  [WebProfilerBundle] replaced pattern to path attribute in routes definitions.
  [FrameworkBundle][Template name] avoid  error message for the shortcut notation.
  [DependencyInjection] perf optim: call dirname() at most 5x
  [DependencyInjection] backport perf optim
  [2.3] Remove possible call_user_func()

Conflicts:
	src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
	src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php
	src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php
	src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php
2014-12-11 20:53:06 +01:00

75 lines
2.1 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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\TemplateReferenceInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
/**
* TemplateNameParser converts template names from the short notation
* "bundle:section:template.format.engine" to TemplateReferenceInterface
* instances.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateNameParser extends BaseTemplateNameParser
{
protected $kernel;
protected $cache = array();
/**
* Constructor.
*
* @param KernelInterface $kernel A KernelInterface instance
*/
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* {@inheritdoc}
*/
public function parse($name)
{
if ($name instanceof TemplateReferenceInterface) {
return $name;
} elseif (isset($this->cache[$name])) {
return $this->cache[$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));
}
if (!preg_match('/^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {
return parent::parse($name);
}
$template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
if ($template->get('bundle')) {
try {
$this->kernel->getBundle($template->get('bundle'));
} catch (\Exception $e) {
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
}
}
return $this->cache[$name] = $template;
}
}