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

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\DevOps\Environment\EnvironmentHelper;
  5. use Shopware\Core\Framework\DependencyInjection\CompilerPass\TwigLoaderConfigCompilerPass;
  6. use Shopware\Core\Framework\Feature;
  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.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  14.  */
  15. class EntityTemplateLoader implements LoaderInterfaceEventSubscriberInterfaceResetInterface
  16. {
  17.     /**
  18.      * @var array<string, array<string, array{template: string, updatedAt: \DateTimeInterface|null}|null>>
  19.      */
  20.     private array $databaseTemplateCache = [];
  21.     private Connection $connection;
  22.     private string $environment;
  23.     /**
  24.      * @internal
  25.      */
  26.     public function __construct(Connection $connectionstring $environment)
  27.     {
  28.         $this->connection $connection;
  29.         $this->environment $environment;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return ['app_template.written' => 'reset'];
  34.     }
  35.     /**
  36.      * @deprecated tag:v6.5.0 will be removed, use `reset()` instead
  37.      */
  38.     public function clearInternalCache(): void
  39.     {
  40.         Feature::triggerDeprecationOrThrow(
  41.             'v6.5.0.0',
  42.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''reset()')
  43.         );
  44.         $this->reset();
  45.     }
  46.     public function reset(): void
  47.     {
  48.         $this->databaseTemplateCache = [];
  49.     }
  50.     public function getSourceContext(string $name): Source
  51.     {
  52.         $template $this->findDatabaseTemplate($name);
  53.         if (!$template) {
  54.             throw new LoaderError(sprintf('Template "%s" is not defined.'$name));
  55.         }
  56.         return new Source($template['template'], $name);
  57.     }
  58.     public function getCacheKey(string $name): string
  59.     {
  60.         return $name;
  61.     }
  62.     public function isFresh(string $nameint $time): bool
  63.     {
  64.         $template $this->findDatabaseTemplate($name);
  65.         if (!$template) {
  66.             return false;
  67.         }
  68.         return $template['updatedAt'] === null || $template['updatedAt']->getTimestamp() < $time;
  69.     }
  70.     /**
  71.      * @return bool
  72.      */
  73.     public function exists(string $name)
  74.     {
  75.         $template $this->findDatabaseTemplate($name);
  76.         if (!$template) {
  77.             return false;
  78.         }
  79.         return true;
  80.     }
  81.     /**
  82.      * @return array{template: string, updatedAt: \DateTimeInterface|null}|null
  83.      */
  84.     private function findDatabaseTemplate(string $name): ?array
  85.     {
  86.         if (EnvironmentHelper::getVariable('DISABLE_EXTENSIONS'false)) {
  87.             return null;
  88.         }
  89.         /*
  90.          * In dev env app templates are directly loaded over the filesystem
  91.          * @see TwigLoaderConfigCompilerPass::addAppTemplatePaths()
  92.          */
  93.         if ($this->environment === 'dev') {
  94.             return null;
  95.         }
  96.         $templateName $this->splitTemplateName($name);
  97.         $namespace $templateName['namespace'];
  98.         $path $templateName['path'];
  99.         if (empty($this->databaseTemplateCache)) {
  100.             /** @var array<array{path: string, template: string, updatedAt: string|null, namespace: string}> $templates */
  101.             $templates $this->connection->fetchAllAssociative('
  102.                 SELECT
  103.                     `app_template`.`path` AS `path`,
  104.                     `app_template`.`template` AS `template`,
  105.                     `app_template`.`updated_at` AS `updatedAt`,
  106.                     `app`.`name` AS `namespace`
  107.                 FROM `app_template`
  108.                 INNER JOIN `app` ON `app_template`.`app_id` = `app`.`id`
  109.                 WHERE `app_template`.`active` = 1 AND `app`.`active` = 1
  110.             ');
  111.             foreach ($templates as $template) {
  112.                 $this->databaseTemplateCache[$template['path']][$template['namespace']] = [
  113.                     'template' => $template['template'],
  114.                     'updatedAt' => $template['updatedAt'] ? new \DateTimeImmutable($template['updatedAt']) : null,
  115.                 ];
  116.             }
  117.         }
  118.         if (\array_key_exists($path$this->databaseTemplateCache) && \array_key_exists($namespace$this->databaseTemplateCache[$path])) {
  119.             return $this->databaseTemplateCache[$path][$namespace];
  120.         }
  121.         /** @deprecated tag:v6.5.0 - only for intermediate backwards compatibility */
  122.         if (
  123.             \array_key_exists('../' $path$this->databaseTemplateCache)
  124.             && \array_key_exists($namespace$this->databaseTemplateCache['../' $path])
  125.         ) {
  126.             return $this->databaseTemplateCache['../' $path][$namespace];
  127.         }
  128.         // we have already loaded all DB templates
  129.         // if the namespace is not included return null
  130.         return $this->databaseTemplateCache[$path][$namespace] = null;
  131.     }
  132.     /**
  133.      * @return array{namespace: string, path: string}
  134.      */
  135.     private function splitTemplateName(string $template): array
  136.     {
  137.         // remove static template inheritance prefix
  138.         if (mb_strpos($template'@') !== 0) {
  139.             return ['path' => $template'namespace' => ''];
  140.         }
  141.         // remove "@"
  142.         $template mb_substr($template1);
  143.         $template explode('/'$template);
  144.         $namespace array_shift($template);
  145.         $template implode('/'$template);
  146.         return ['path' => $template'namespace' => $namespace];
  147.     }
  148. }