lib/netgen/content-browser/bundle/EventListener/SetBackendListener.php line 35

  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Bundle\ContentBrowserBundle\EventListener;
  4. use Netgen\ContentBrowser\Registry\BackendRegistry;
  5. use Netgen\ContentBrowser\Utils\BackwardsCompatibility\MainRequestEventTrait;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. final class SetBackendListener implements EventSubscriberInterface
  10. {
  11.     use MainRequestEventTrait;
  12.     private ContainerInterface $container;
  13.     private BackendRegistry $backendRegistry;
  14.     public function __construct(ContainerInterface $containerBackendRegistry $backendRegistry)
  15.     {
  16.         $this->container $container;
  17.         $this->backendRegistry $backendRegistry;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [KernelEvents::REQUEST => ['onKernelRequest'1]];
  22.     }
  23.     /**
  24.      * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
  25.      */
  26.     public function onKernelRequest($event): void
  27.     {
  28.         if (!$this->isMainRequest($event)) {
  29.             return;
  30.         }
  31.         $attributes $event->getRequest()->attributes;
  32.         if ($attributes->get(SetIsApiRequestListener::API_FLAG_NAME) !== true) {
  33.             return;
  34.         }
  35.         if (!$attributes->has('itemType')) {
  36.             return;
  37.         }
  38.         $backend $this->backendRegistry->getBackend($attributes->get('itemType'));
  39.         $this->container->set('netgen_content_browser.backend'$backend);
  40.     }
  41. }