vendor/symfony/framework-bundle/Templating/TemplateNameParser.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Templating;
  11. @trigger_error('The '.TemplateNameParser::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.', \E_USER_DEPRECATED);
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
  14. use Symfony\Component\Templating\TemplateReferenceInterface;
  15. /**
  16.  * TemplateNameParser converts template names from the short notation
  17.  * "bundle:section:template.format.engine" to TemplateReferenceInterface
  18.  * instances.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  *
  22.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  23.  */
  24. class TemplateNameParser extends BaseTemplateNameParser
  25. {
  26.     protected $kernel;
  27.     protected $cache = [];
  28.     public function __construct(KernelInterface $kernel)
  29.     {
  30.         $this->kernel $kernel;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function parse($name)
  36.     {
  37.         if ($name instanceof TemplateReferenceInterface) {
  38.             return $name;
  39.         } elseif (isset($this->cache[$name])) {
  40.             return $this->cache[$name];
  41.         }
  42.         // normalize name
  43.         $name preg_replace('#/{2,}#''/'str_replace('\\''/'$name));
  44.         if (str_contains($name'..')) {
  45.             throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.'$name));
  46.         }
  47.         if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/'$name$matches) || str_starts_with($name'@')) {
  48.             return parent::parse($name);
  49.         }
  50.         $template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
  51.         if ($template->get('bundle')) {
  52.             try {
  53.                 $this->kernel->getBundle($template->get('bundle'));
  54.             } catch (\Exception $e) {
  55.                 throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.'$name), 0$e);
  56.             }
  57.         }
  58.         return $this->cache[$name] = $template;
  59.     }
  60. }