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

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.         ];
  28.     }
  29.     public function requestEvent(RequestEvent $event): void
  30.     {
  31.         $request $event->getRequest();
  32.         $this->themes $this->detectedThemes($request);
  33.     }
  34.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  35.     {
  36.         if (empty($this->themes)) {
  37.             return $namespaceHierarchy;
  38.         }
  39.         return $this->themeInheritanceBuilder->build($namespaceHierarchy$this->themes);
  40.     }
  41.     private function detectedThemes(Request $request): array
  42.     {
  43.         // detect active themes of request
  44.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  45.         if (!$theme) {
  46.             return [];
  47.         }
  48.         $themes = [
  49.             $theme => true,
  50.         ];
  51.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  52.         if ($theme) {
  53.             $themes[$theme] = true;
  54.         }
  55.         if (!isset($themes['Storefront'])) {
  56.             $themes['Storefront'] = true;
  57.         }
  58.         return $themes;
  59.     }
  60. }