vendor/symfony/twig-bundle/Loader/FilesystemLoader.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\TwigBundle\Loader;
  11. @trigger_error('The '.FilesystemLoader::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig notation for templates instead.', \E_USER_DEPRECATED);
  12. use Symfony\Component\Config\FileLocatorInterface;
  13. use Symfony\Component\Templating\TemplateNameParserInterface;
  14. use Symfony\Component\Templating\TemplateReferenceInterface;
  15. use Twig\Error\LoaderError;
  16. use Twig\Loader\FilesystemLoader as BaseFilesystemLoader;
  17. /**
  18.  * FilesystemLoader extends the default Twig filesystem loader
  19.  * to work with the Symfony paths and template references.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  *
  23.  * @deprecated since version 4.3, to be removed in 5.0; use Twig notation for templates instead.
  24.  */
  25. class FilesystemLoader extends BaseFilesystemLoader
  26. {
  27.     protected $locator;
  28.     protected $parser;
  29.     /**
  30.      * @param string|null $rootPath The root path common to all relative paths (null for getcwd())
  31.      */
  32.     public function __construct(FileLocatorInterface $locatorTemplateNameParserInterface $parserstring $rootPath null)
  33.     {
  34.         parent::__construct([], $rootPath);
  35.         $this->locator $locator;
  36.         $this->parser $parser;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      *
  41.      * The name parameter might also be a TemplateReferenceInterface.
  42.      *
  43.      * @return bool
  44.      */
  45.     public function exists($name)
  46.     {
  47.         return parent::exists((string) $name);
  48.     }
  49.     /**
  50.      * Returns the path to the template file.
  51.      *
  52.      * The file locator is used to locate the template when the naming convention
  53.      * is the symfony one (i.e. the name can be parsed).
  54.      * Otherwise the template is located using the locator from the twig library.
  55.      *
  56.      * @param string|TemplateReferenceInterface $template The template
  57.      * @param bool                              $throw    When true, a LoaderError exception will be thrown if a template could not be found
  58.      *
  59.      * @return string The path to the template file
  60.      *
  61.      * @throws LoaderError if the template could not be found
  62.      */
  63.     protected function findTemplate($template$throw true)
  64.     {
  65.         $logicalName = (string) $template;
  66.         if (isset($this->cache[$logicalName])) {
  67.             return $this->cache[$logicalName];
  68.         }
  69.         $file null;
  70.         try {
  71.             $file parent::findTemplate($logicalName);
  72.         } catch (LoaderError $e) {
  73.             $twigLoaderException $e;
  74.             // for BC
  75.             try {
  76.                 $template $this->parser->parse($template);
  77.                 $file $this->locator->locate($template);
  78.             } catch (\Exception $e) {
  79.             }
  80.         }
  81.         if (false === $file || null === $file) {
  82.             if ($throw) {
  83.                 throw $twigLoaderException;
  84.             }
  85.             return null;
  86.         }
  87.         return $this->cache[$logicalName] = $file;
  88.     }
  89. }