vendor/sylius/theme-bundle/src/Twig/Loader/ThemedTemplateLoader.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ThemeBundle\Twig\Loader;
  12. use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
  13. use Sylius\Bundle\ThemeBundle\Twig\Locator\TemplateLocatorInterface;
  14. use Sylius\Bundle\ThemeBundle\Twig\Locator\TemplateNotFoundException;
  15. use Symfony\Component\Templating\TemplateReferenceInterface;
  16. use Twig\Loader\LoaderInterface as TwigLoaderInterface;
  17. use Twig\Source;
  18. final class ThemedTemplateLoader implements LoaderInterface
  19. {
  20.     private TwigLoaderInterface $decoratedLoader;
  21.     private TemplateLocatorInterface $templateLocator;
  22.     private ThemeContextInterface $themeContext;
  23.     public function __construct(
  24.         TwigLoaderInterface $decoratedLoader,
  25.         TemplateLocatorInterface $templateLocator,
  26.         ThemeContextInterface $themeContext,
  27.     ) {
  28.         $this->decoratedLoader $decoratedLoader;
  29.         $this->templateLocator $templateLocator;
  30.         $this->themeContext $themeContext;
  31.     }
  32.     /**
  33.      * @param string|TemplateReferenceInterface $name
  34.      */
  35.     public function getSourceContext($name): Source
  36.     {
  37.         try {
  38.             $path $this->locateTemplate($name);
  39.             /** @psalm-suppress RedundantCastGivenDocblockType */
  40.             return new Source((string) file_get_contents($path), (string) $name$path);
  41.         } catch (TemplateNotFoundException \InvalidArgumentException $exception) {
  42.             /** @psalm-suppress PossiblyInvalidArgument */
  43.             return $this->decoratedLoader->getSourceContext($name);
  44.         }
  45.     }
  46.     /**
  47.      * @param string|TemplateReferenceInterface $name
  48.      */
  49.     public function getCacheKey($name): string
  50.     {
  51.         try {
  52.             return $this->locateTemplate($name);
  53.         } catch (TemplateNotFoundException \InvalidArgumentException $exception) {
  54.             /** @psalm-suppress PossiblyInvalidArgument */
  55.             return $this->decoratedLoader->getCacheKey($name);
  56.         }
  57.     }
  58.     /**
  59.      * @param string|TemplateReferenceInterface $name
  60.      * @param int $time
  61.      */
  62.     public function isFresh($name$time): bool
  63.     {
  64.         try {
  65.             return filemtime($this->locateTemplate($name)) <= $time;
  66.         } catch (TemplateNotFoundException \InvalidArgumentException $exception) {
  67.             /** @psalm-suppress PossiblyInvalidArgument */
  68.             return $this->decoratedLoader->isFresh($name$time);
  69.         }
  70.     }
  71.     /**
  72.      * @param string|TemplateReferenceInterface $name
  73.      */
  74.     public function exists($name): bool
  75.     {
  76.         try {
  77.             return stat($this->locateTemplate($name)) !== false;
  78.         } catch (TemplateNotFoundException \InvalidArgumentException $exception) {
  79.             /** @psalm-suppress PossiblyInvalidArgument */
  80.             return $this->decoratedLoader->exists($name);
  81.         }
  82.     }
  83.     /**
  84.      * @psalm-assert string $template
  85.      *
  86.      * @param string|TemplateReferenceInterface $template
  87.      *
  88.      * @throws TemplateNotFoundException|\InvalidArgumentException
  89.      */
  90.     private function locateTemplate($template): string
  91.     {
  92.         if ($template instanceof TemplateReferenceInterface) {
  93.             // Symfony 4.x still pushes TemplateReferenceInterface to Twig loader (especially when warming up cache)
  94.             throw new \InvalidArgumentException(sprintf('Instances of "%s" are not supported.'TemplateReferenceInterface::class));
  95.         }
  96.         $theme $this->themeContext->getTheme();
  97.         if ($theme === null) {
  98.             throw new TemplateNotFoundException($template, []);
  99.         }
  100.         return $this->templateLocator->locate($template$theme);
  101.     }
  102. }