vendor/liip/theme-bundle/Twig/Loader/FilesystemLoader.php line 63

Open in your IDE?
  1. <?php
  2. namespace Liip\ThemeBundle\Twig\Loader;
  3. use Symfony\Component\Config\FileLocatorInterface;
  4. use Symfony\Component\Templating\TemplateNameParserInterface;
  5. use Symfony\Component\Templating\TemplateReferenceInterface;
  6. use Liip\ThemeBundle\ActiveTheme;
  7. use Twig\Error\LoaderError as TwigLoaderError;
  8. use Twig\Loader\FilesystemLoader as TwigFilesystemLoader;
  9. class FilesystemLoader extends TwigFilesystemLoader
  10. {
  11.     protected $locator;
  12.     protected $parser;
  13.     /**
  14.      * @var ActiveTheme|null
  15.      */
  16.     protected $activeTheme;
  17.     /**
  18.      * Constructor.
  19.      *
  20.      * @see TwigBundle own FilesystemLoader
  21.      *
  22.      * @param FileLocatorInterface        $locator  A FileLocatorInterface instance
  23.      * @param TemplateNameParserInterface $parser   A TemplateNameParserInterface instance
  24.      * @param string|null                 $rootPath The root path common to all relative paths (null for getcwd())
  25.      */
  26.     public function __construct(FileLocatorInterface $locatorTemplateNameParserInterface $parser$rootPath null)
  27.     {
  28.         parent::__construct(array(), $rootPath);
  29.         $this->locator $locator;
  30.         $this->parser $parser;
  31.     }
  32.     /**
  33.      * Define the active theme
  34.      *
  35.      * @param ActiveTheme $activeTheme
  36.      */
  37.     public function setActiveTheme(ActiveTheme $activeTheme null)
  38.     {
  39.         $this->activeTheme $activeTheme;
  40.     }
  41.     /**
  42.      * Returns the path to the template file.
  43.      *
  44.      * The file locator is used to locate the template when the naming convention
  45.      * is the symfony one (i.e. the name can be parsed).
  46.      * Otherwise the template is located using the locator from the twig library.
  47.      *
  48.      * @param string|TemplateReferenceInterface $template The template
  49.      * @param bool                              $throw    When true, a \Twig\Error\LoaderError exception will be thrown if a template could not be found
  50.      *
  51.      * @return string The path to the template file
  52.      *
  53.      * @throws \Twig\Error\LoaderError if the template could not be found
  54.      */
  55.     protected function findTemplate($template$throw true)
  56.     {
  57.         $logicalName = (string) $template;
  58.         if ($this->activeTheme) {
  59.             $logicalName .= '|'.$this->activeTheme->getName();
  60.         }
  61.         if (isset($this->cache[$logicalName])) {
  62.             return $this->cache[$logicalName];
  63.         }
  64.         $file null;
  65.         $previous null;
  66.         try {
  67.             $templateReference $this->parser->parse($template);
  68.             $file $this->locator->locate($templateReference);
  69.         } catch (\Exception $e) {
  70.             $previous $e;
  71.             // for BC
  72.             try {
  73.                 $file parent::findTemplate((string) $template);
  74.             } catch (TwigLoaderError $e) {
  75.                 $previous $e;
  76.             }
  77.         }
  78.         if (false === $file || null === $file) {
  79.             if ($throw) {
  80.                 throw new TwigLoaderError(sprintf('Unable to find template "%s".'$logicalName), -1null$previous);
  81.             }
  82.             
  83.             return false;
  84.         }
  85.         return $this->cache[$logicalName] = $file;
  86.     }
  87. }