vendor/shopware/storefront/Theme/Twig/ThemeNamespaceHierarchyBuilder.php line 49

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Twig;
  3. use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
  4. use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\TemplateNamespaceHierarchyBuilderInterface;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\SalesChannelRequest;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Theme\SalesChannelThemeLoader;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Contracts\Service\ResetInterface;
  14. /**
  15.  * @internal
  16.  */
  17. #[Package('storefront')]
  18. class ThemeNamespaceHierarchyBuilder implements TemplateNamespaceHierarchyBuilderInterfaceEventSubscriberInterfaceResetInterface
  19. {
  20.     /**
  21.      * @var array<int|string, bool>
  22.      */
  23.     private array $themes = [];
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         private readonly ThemeInheritanceBuilderInterface $themeInheritanceBuilder,
  29.         private readonly SalesChannelThemeLoader $salesChannelThemeLoader
  30.     ) {
  31.     }
  32.     /**
  33.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             KernelEvents::REQUEST => 'requestEvent',
  39.             KernelEvents::EXCEPTION => 'requestEvent',
  40.             DocumentTemplateRendererParameterEvent::class => 'onDocumentRendering',
  41.         ];
  42.     }
  43.     public function requestEvent(RequestEvent $event): void
  44.     {
  45.         $request $event->getRequest();
  46.         $this->themes $this->detectedThemes($request);
  47.     }
  48.     public function onDocumentRendering(DocumentTemplateRendererParameterEvent $event): void
  49.     {
  50.         $parameters $event->getParameters();
  51.         if (!\array_key_exists('context'$parameters)) {
  52.             return;
  53.         }
  54.         /** @var SalesChannelContext $context */
  55.         $context $parameters['context'];
  56.         $themes = [];
  57.         $theme $this->salesChannelThemeLoader->load($context->getSalesChannelId());
  58.         if (empty($theme['themeName'])) {
  59.             return;
  60.         }
  61.         $themes[$theme['themeName']] = true;
  62.         $themes['Storefront'] = true;
  63.         $this->themes $themes;
  64.     }
  65.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  66.     {
  67.         if (empty($this->themes)) {
  68.             return $namespaceHierarchy;
  69.         }
  70.         return $this->themeInheritanceBuilder->build($namespaceHierarchy$this->themes);
  71.     }
  72.     public function reset(): void
  73.     {
  74.         $this->themes = [];
  75.     }
  76.     /**
  77.      * @return array<int|string, bool>
  78.      */
  79.     private function detectedThemes(Request $request): array
  80.     {
  81.         $themes = [];
  82.         // get name if theme is not inherited
  83.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  84.         if (!$theme) {
  85.             // get theme name from base theme because for inherited themes the name is always null
  86.             $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  87.         }
  88.         if (!$theme) {
  89.             return [];
  90.         }
  91.         $themes[$theme] = true;
  92.         $themes['Storefront'] = true;
  93.         return $themes;
  94.     }
  95. }