lib/netgen/content-browser/bundle/EventListener/SetConfigListener.php line 44

  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Bundle\ContentBrowserBundle\EventListener;
  4. use Netgen\ContentBrowser\Config\Configuration;
  5. use Netgen\ContentBrowser\Event\ConfigLoadEvent;
  6. use Netgen\ContentBrowser\Event\ContentBrowserEvents;
  7. use Netgen\ContentBrowser\Exceptions\InvalidArgumentException;
  8. use Netgen\ContentBrowser\Exceptions\RuntimeException;
  9. use Netgen\ContentBrowser\Utils\BackwardsCompatibility\MainRequestEventTrait;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Kernel;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use function is_array;
  16. use function sprintf;
  17. final class SetConfigListener implements EventSubscriberInterface
  18. {
  19.     use MainRequestEventTrait;
  20.     private ContainerInterface $container;
  21.     private EventDispatcherInterface $eventDispatcher;
  22.     public function __construct(ContainerInterface $containerEventDispatcherInterface $eventDispatcher)
  23.     {
  24.         $this->container $container;
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [KernelEvents::REQUEST => ['onKernelRequest'1]];
  30.     }
  31.     /**
  32.      * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
  33.      */
  34.     public function onKernelRequest($event): void
  35.     {
  36.         if (!$this->isMainRequest($event)) {
  37.             return;
  38.         }
  39.         $request $event->getRequest();
  40.         $attributes $request->attributes;
  41.         if ($attributes->get(SetIsApiRequestListener::API_FLAG_NAME) !== true) {
  42.             return;
  43.         }
  44.         if (!$attributes->has('itemType')) {
  45.             return;
  46.         }
  47.         $config $this->loadConfig($attributes->get('itemType'));
  48.         $customParams Kernel::VERSION_ID >= 50100 ?
  49.             $request->query->all('customParams') :
  50.             $request->query->get('customParams') ?? [];
  51.         if (!is_array($customParams)) {
  52.             throw new RuntimeException(
  53.                 sprintf(
  54.                     'Invalid custom parameters specification for "%s" item type.',
  55.                     $attributes->get('itemType'),
  56.                 ),
  57.             );
  58.         }
  59.         $config->addParameters($customParams);
  60.         $configLoadEvent = new ConfigLoadEvent($config);
  61.         Kernel::VERSION_ID >= 40300 ?
  62.             $this->eventDispatcher->dispatch($configLoadEventContentBrowserEvents::CONFIG_LOAD) :
  63.             $this->eventDispatcher->dispatch(ContentBrowserEvents::CONFIG_LOAD$configLoadEvent);
  64.         $this->container->set('netgen_content_browser.config'$config);
  65.     }
  66.     /**
  67.      * Loads the configuration for provided item type from the container.
  68.      *
  69.      * @throws \Netgen\ContentBrowser\Exceptions\InvalidArgumentException If config could not be found
  70.      */
  71.     private function loadConfig(string $itemType): Configuration
  72.     {
  73.         $service 'netgen_content_browser.config.' $itemType;
  74.         if (!$this->container->has($service)) {
  75.             throw new InvalidArgumentException(
  76.                 sprintf(
  77.                     'Configuration for "%s" item type does not exist.',
  78.                     $itemType,
  79.                 ),
  80.             );
  81.         }
  82.         $config $this->container->get($service);
  83.         if (!$config instanceof Configuration) {
  84.             throw new InvalidArgumentException(
  85.                 sprintf(
  86.                     'Configuration for "%s" item type is invalid.',
  87.                     $itemType,
  88.                 ),
  89.             );
  90.         }
  91.         return $config;
  92.     }
  93. }