vendor/rawafed/theme-bundle/Twig/ThemeTemplatesLoader.php line 58

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