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

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