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

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