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

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