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

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