vendor/shopware/core/Framework/Adapter/Twig/EntityTemplateLoader.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DependencyInjection\CompilerPass\TwigLoaderConfigCompilerPass;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Contracts\Service\ResetInterface;
  7. use Twig\Error\LoaderError;
  8. use Twig\Loader\LoaderInterface;
  9. use Twig\Source;
  10. class EntityTemplateLoader implements LoaderInterfaceEventSubscriberInterfaceResetInterface
  11. {
  12.     private array $databaseTemplateCache = [];
  13.     private Connection $connection;
  14.     private string $environment;
  15.     public function __construct(Connection $connectionstring $environment)
  16.     {
  17.         $this->connection $connection;
  18.         $this->environment $environment;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return ['app_template.written' => 'reset'];
  23.     }
  24.     /**
  25.      * @deprecated tag:v6.5.0 will be removed, use `reset()` instead
  26.      */
  27.     public function clearInternalCache(): void
  28.     {
  29.         $this->reset();
  30.     }
  31.     public function reset(): void
  32.     {
  33.         $this->databaseTemplateCache = [];
  34.     }
  35.     public function getSourceContext(string $name): Source
  36.     {
  37.         $template $this->findDatabaseTemplate($name);
  38.         if (!$template) {
  39.             throw new LoaderError(sprintf('Template "%s" is not defined.'$name));
  40.         }
  41.         return new Source($template['template'], $name);
  42.     }
  43.     public function getCacheKey(string $name): string
  44.     {
  45.         return $name;
  46.     }
  47.     public function isFresh(string $nameint $time): bool
  48.     {
  49.         $template $this->findDatabaseTemplate($name);
  50.         if (!$template) {
  51.             return false;
  52.         }
  53.         return $template['updatedAt'] === null || $template['updatedAt']->getTimestamp() < $time;
  54.     }
  55.     /**
  56.      * @return bool
  57.      */
  58.     public function exists(string $name)
  59.     {
  60.         $template $this->findDatabaseTemplate($name);
  61.         if (!$template) {
  62.             return false;
  63.         }
  64.         return true;
  65.     }
  66.     private function findDatabaseTemplate(string $name): ?array
  67.     {
  68.         /*
  69.          * In dev env app templates are directly loaded over the filesystem
  70.          * @see TwigLoaderConfigCompilerPass::addAppTemplatePaths()
  71.          */
  72.         if ($this->environment === 'dev') {
  73.             return null;
  74.         }
  75.         $templateName $this->splitTemplateName($name);
  76.         $namespace $templateName['namespace'];
  77.         $path $templateName['path'];
  78.         if (empty($this->databaseTemplateCache)) {
  79.             $templates $this->connection->fetchAll('
  80.                 SELECT
  81.                     `app_template`.`path` AS `path`,
  82.                     `app_template`.`template` AS `template`,
  83.                     `app_template`.`updated_at` AS `updatedAt`,
  84.                     `app`.`name` AS `namespace`
  85.                 FROM `app_template`
  86.                 INNER JOIN `app` ON `app_template`.`app_id` = `app`.`id`
  87.                 WHERE `app_template`.`active` = 1 AND `app`.`active` = 1
  88.             ');
  89.             /** @var array $template */
  90.             foreach ($templates as $template) {
  91.                 $this->databaseTemplateCache[$template['path']][$template['namespace']] = [
  92.                     'template' => $template['template'],
  93.                     'updatedAt' => $template['updatedAt'] ? new \DateTimeImmutable($template['updatedAt']) : null,
  94.                 ];
  95.             }
  96.         }
  97.         if (\array_key_exists($path$this->databaseTemplateCache) && \array_key_exists($namespace$this->databaseTemplateCache[$path])) {
  98.             return $this->databaseTemplateCache[$path][$namespace];
  99.         }
  100.         // we have already loaded all DB templates
  101.         // if the namespace is not included return null
  102.         return $this->databaseTemplateCache[$path][$namespace] = null;
  103.     }
  104.     private function splitTemplateName(string $template): array
  105.     {
  106.         // remove static template inheritance prefix
  107.         if (mb_strpos($template'@') !== 0) {
  108.             return ['path' => $template'namespace' => ''];
  109.         }
  110.         // remove "@"
  111.         $template mb_substr($template1);
  112.         $template explode('/'$template);
  113.         $namespace array_shift($template);
  114.         $template implode('/'$template);
  115.         return ['path' => $template'namespace' => $namespace];
  116.     }
  117. }