vendor/shopware/platform/src/Storefront/Theme/Twig/ThemeNamespaceHierarchyBuilder.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Twig;
  3. use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\TemplateNamespaceHierarchyBuilderInterface;
  4. use Shopware\Core\SalesChannelRequest;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class ThemeNamespaceHierarchyBuilder implements TemplateNamespaceHierarchyBuilderInterfaceEventSubscriberInterface
  10. {
  11.     private array $themes = [];
  12.     private ThemeInheritanceBuilderInterface $themeInheritanceBuilder;
  13.     public function __construct(ThemeInheritanceBuilderInterface $themeInheritanceBuilder)
  14.     {
  15.         $this->themeInheritanceBuilder $themeInheritanceBuilder;
  16.     }
  17.     /**
  18.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  19.      */
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             KernelEvents::REQUEST => 'requestEvent',
  24.             KernelEvents::EXCEPTION => 'requestEvent',
  25.         ];
  26.     }
  27.     public function requestEvent(RequestEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         $this->themes $this->detectedThemes($request);
  31.     }
  32.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  33.     {
  34.         if (empty($this->themes)) {
  35.             return $namespaceHierarchy;
  36.         }
  37.         return $this->themeInheritanceBuilder->build($namespaceHierarchy$this->themes);
  38.     }
  39.     private function detectedThemes(Request $request): array
  40.     {
  41.         // get name if theme is not inherited
  42.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  43.         if (!$theme) {
  44.             // get theme name from base theme because for inherited themes the name is always null
  45.             $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  46.         }
  47.         if (!$theme) {
  48.             return [];
  49.         }
  50.         $themes[$theme] = true;
  51.         $themes['Storefront'] = true;
  52.         return $themes;
  53.     }
  54. }