vendor/shopware/storefront/Theme/Twig/ThemeTemplateFinder.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Twig;
  3. use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\NamespaceHierarchyBuilder;
  4. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  5. use Shopware\Core\SalesChannelRequest;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Twig\Environment;
  11. use Twig\Loader\LoaderInterface;
  12. /**
  13.  * @deprecated tag:v6.3.0 will be removed, use ThemeNamespaceHierarchyBuilder instead
  14.  */
  15. class ThemeTemplateFinder extends TemplateFinder implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var ThemeInheritanceBuilderInterface
  19.      */
  20.     private $inheritanceBuilder;
  21.     public function __construct(
  22.         Environment $twig,
  23.         LoaderInterface $loader,
  24.         string $cacheDir,
  25.         ThemeInheritanceBuilderInterface $inheritanceBuilder,
  26.         ?NamespaceHierarchyBuilder $hierarchyBuilder null
  27.     ) {
  28.         parent::__construct($twig$loader$cacheDir$hierarchyBuilder);
  29.         $this->inheritanceBuilder $inheritanceBuilder;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             KernelEvents::REQUEST => 'requestEvent',
  35.         ];
  36.     }
  37.     public function requestEvent(RequestEvent $event): void
  38.     {
  39.         $request $event->getRequest();
  40.         $themes $this->detectedThemes($request);
  41.         if (empty($themes)) {
  42.             return;
  43.         }
  44.         $this->bundles $this->inheritanceBuilder->build($this->bundles$themes);
  45.     }
  46.     private function detectedThemes(Request $request): array
  47.     {
  48.         // detect active themes of request
  49.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
  50.         if (!$theme) {
  51.             return [];
  52.         }
  53.         $themes = [
  54.             $theme => true,
  55.         ];
  56.         $theme $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
  57.         if ($theme) {
  58.             $themes[$theme] = true;
  59.         }
  60.         return $themes;
  61.     }
  62. }