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

  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(private readonly ThemeInheritanceBuilderInterface $themeInheritanceBuilder, private readonly SalesChannelThemeLoader $salesChannelThemeLoader)
  28.     {
  29.     }
  30.     /**
  31.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::REQUEST => 'requestEvent',
  37.             KernelEvents::EXCEPTION => 'requestEvent',
  38.             DocumentTemplateRendererParameterEvent::class => 'onDocumentRendering',
  39.         ];
  40.     }
  41.     public function requestEvent(RequestEvent $event): void
  42.     {
  43.         $request $event->getRequest();
  44.         $this->themes $this->detectedThemes($request);
  45.     }
  46.     public function onDocumentRendering(DocumentTemplateRendererParameterEvent $event): void
  47.     {
  48.         $parameters $event->getParameters();
  49.         if (!\array_key_exists('context'$parameters)) {
  50.             return;
  51.         }
  52.         /** @var SalesChannelContext $context */
  53.         $context $parameters['context'];
  54.         $themes = [];
  55.         $theme $this->salesChannelThemeLoader->load($context->getSalesChannelId());
  56.         if (empty($theme['themeName'])) {
  57.             return;
  58.         }
  59.         $themes[$theme['themeName']] = true;
  60.         $themes['Storefront'] = true;
  61.         $this->themes $themes;
  62.     }
  63.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  64.     {
  65.         if (empty($this->themes)) {
  66.             return $namespaceHierarchy;
  67.         }
  68.         return $this->themeInheritanceBuilder->build($namespaceHierarchy$this->themes);
  69.     }
  70.     public function reset(): void
  71.     {
  72.         $this->themes = [];
  73.     }
  74.     /**
  75.      * @return array<int|string, bool>
  76.      */
  77.     private function detectedThemes(Request $request): array
  78.     {
  79.         $themes = [];
  80.         // get name if theme is not inherited
  81.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  82.         if (!$theme) {
  83.             // get theme name from base theme because for inherited themes the name is always null
  84.             $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  85.         }
  86.         if (!$theme) {
  87.             return [];
  88.         }
  89.         $themes[$theme] = true;
  90.         $themes['Storefront'] = true;
  91.         return $themes;
  92.     }
  93. }