vendor/sylius/theme-bundle/src/Twig/ThemeFilesystemLoader.php line 65

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;
  12. use Symfony\Component\Config\FileLocatorInterface;
  13. use Symfony\Component\Templating\TemplateNameParserInterface;
  14. use Symfony\Component\Templating\TemplateReference;
  15. use Twig\Loader\ExistsLoaderInterface;
  16. use Twig\Loader\LoaderInterface;
  17. use Twig\Source;
  18. final class ThemeFilesystemLoader implements LoaderInterfaceExistsLoaderInterface
  19. {
  20.     /** @var LoaderInterface */
  21.     private $decoratedLoader;
  22.     /** @var FileLocatorInterface */
  23.     private $templateLocator;
  24.     /** @var TemplateNameParserInterface */
  25.     private $templateNameParser;
  26.     /** @var string[] */
  27.     private $cache = [];
  28.     public function __construct(
  29.         LoaderInterface $decoratedLoader,
  30.         FileLocatorInterface $templateLocator,
  31.         TemplateNameParserInterface $templateNameParser
  32.     ) {
  33.         $this->decoratedLoader $decoratedLoader;
  34.         $this->templateLocator $templateLocator;
  35.         $this->templateNameParser $templateNameParser;
  36.     }
  37.     /**
  38.      * @param string|TemplateReference $name
  39.      */
  40.     public function getSourceContext($name): Source
  41.     {
  42.         try {
  43.             $path $this->findTemplate($name);
  44.             return new Source((string) file_get_contents($path), (string) $name$path);
  45.         } catch (\Exception $exception) {
  46.             /** @psalm-suppress ImplicitToStringCast */
  47.             return $this->decoratedLoader->getSourceContext($name);
  48.         }
  49.     }
  50.     /**
  51.      * @param string|TemplateReference $name
  52.      */
  53.     public function getCacheKey($name): string
  54.     {
  55.         try {
  56.             return $this->findTemplate($name);
  57.         } catch (\Exception $exception) {
  58.             /** @psalm-suppress ImplicitToStringCast */
  59.             return $this->decoratedLoader->getCacheKey($name);
  60.         }
  61.     }
  62.     /**
  63.      * @param string|TemplateReference $name
  64.      */
  65.     public function isFresh($name$time): bool
  66.     {
  67.         try {
  68.             return filemtime($this->findTemplate($name)) <= $time;
  69.         } catch (\Exception $exception) {
  70.             /** @psalm-suppress ImplicitToStringCast */
  71.             return $this->decoratedLoader->isFresh($name$time);
  72.         }
  73.     }
  74.     /**
  75.      * @param string|TemplateReference $name
  76.      */
  77.     public function exists($name): bool
  78.     {
  79.         try {
  80.             return stat($this->findTemplate($name)) !== false;
  81.         } catch (\Exception $exception) {
  82.             /** @psalm-suppress ImplicitToStringCast */
  83.             return $this->decoratedLoader->exists($name);
  84.         }
  85.     }
  86.     /**
  87.      * @param string|TemplateReference $logicalName
  88.      */
  89.     private function findTemplate($logicalName): string
  90.     {
  91.         $logicalName = (string) $logicalName;
  92.         if (isset($this->cache[$logicalName])) {
  93.             return $this->cache[$logicalName];
  94.         }
  95.         $template $this->templateNameParser->parse($logicalName);
  96.         /**
  97.          * @var string
  98.          * @psalm-suppress ImplicitToStringCast
  99.          */
  100.         $file $this->templateLocator->locate($template);
  101.         return $this->cache[$logicalName] = $file;
  102.     }
  103. }