vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php line 82

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