platform/src/Storefront/Theme/Twig/ThemeNamespaceHierarchyBuilder.php line 37

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.     /**
  12.      * @var array
  13.      */
  14.     private $themes;
  15.     /**
  16.      * @var ThemeInheritanceBuilderInterface
  17.      */
  18.     private $themeInheritanceBuilder;
  19.     public function __construct(ThemeInheritanceBuilderInterface $themeInheritanceBuilder)
  20.     {
  21.         $this->themeInheritanceBuilder $themeInheritanceBuilder;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             KernelEvents::REQUEST => 'requestEvent',
  27.             KernelEvents::EXCEPTION => 'requestEvent',
  28.         ];
  29.     }
  30.     public function requestEvent(RequestEvent $event): void
  31.     {
  32.         $request $event->getRequest();
  33.         $this->themes $this->detectedThemes($request);
  34.     }
  35.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  36.     {
  37.         if (empty($this->themes)) {
  38.             return $namespaceHierarchy;
  39.         }
  40.         return $this->themeInheritanceBuilder->build($namespaceHierarchy$this->themes);
  41.     }
  42.     private function detectedThemes(Request $request): array
  43.     {
  44.         // get name if theme is not inherited
  45.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  46.         if (!$theme) {
  47.             // get theme name from base theme because for inherited themes the name is always null
  48.             $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  49.         }
  50.         if (!$theme) {
  51.             return [];
  52.         }
  53.         $themes[$theme] = true;
  54.         $themes['Storefront'] = true;
  55.         return $themes;
  56.     }
  57. }