custom/plugins/NetiNextCmsElementBuilder/src/Subscriber/TwigSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextCmsElementBuilder\Subscriber;
  4. use NetInventors\NetiNextCmsElementBuilder\Components\CmsElementTwigLoader;
  5. use NetInventors\NetiNextCmsElementBuilder\Service\ElementCache;
  6. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  7. use Shopware\Core\Framework\Event\ShopwareSalesChannelEvent;
  8. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Twig\Environment;
  11. use Twig\Loader\ChainLoader;
  12. class TwigSubscriber implements EventSubscriberInterface
  13. {
  14.     private Environment  $twig;
  15.     private string       $rootDir;
  16.     private ElementCache $elementCache;
  17.     public function __construct(Environment $twigstring $rootDirElementCache $elementCache)
  18.     {
  19.         $this->twig         $twig;
  20.         $this->rootDir      $rootDir;
  21.         $this->elementCache $elementCache;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             GenericPageLoadedEvent::class => 'onPageLoaded',
  27.             CmsPageLoadedEvent::class     => 'onPageLoaded',
  28.         ];
  29.     }
  30.     public function onPageLoaded(ShopwareSalesChannelEvent $event): void
  31.     {
  32.         $loader $this->twig->getLoader();
  33.         switch (true) {
  34.             case $loader instanceof ChainLoader:
  35.                 $loader->addLoader(new CmsElementTwigLoader($this->rootDir$this->elementCache));
  36.                 break;
  37.             default:
  38.                 throw new \Exception('Yet unsupported loader type. FUck.');
  39.         }
  40.     }
  41. }