src/App/Subscriber/Routing/AppContextSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Routing;
  3. use Cms\CoreBundle\Service\ContextManager;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\Routing\RouterInterface;
  9. final class AppContextSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var RouterInterface
  13.      */
  14.     protected RouterInterface $router;
  15.     /**
  16.      * @var ParameterBagInterface
  17.      */
  18.     protected ParameterBagInterface $parameters;
  19.     /**
  20.      * @var ContextManager
  21.      */
  22.     protected ContextManager $cm;
  23.     /**
  24.      * @param RouterInterface $router
  25.      * @param ParameterBagInterface $parameters
  26.      * @param ContextManager $cm
  27.      */
  28.     public function __construct(RouterInterface $routerParameterBagInterface $parametersContextManager $cm)
  29.     {
  30.         $this->router $router;
  31.         $this->parameters $parameters;
  32.         $this->cm $cm;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             // NOTE: using "9" for priority as that comes after router but before firewall
  41.             KernelEvents::REQUEST => ['onKernelRequest'9],
  42.         ];
  43.     }
  44.     /**
  45.      * @param RequestEvent $event
  46.      */
  47.     public function onKernelRequest(RequestEvent $event): void
  48.     {
  49.         // obtain the context
  50.         $context $this->router->getContext();
  51.         // set the environment variable
  52.         $context->setParameter(
  53.             'cluster',
  54.             $this->parameters->get('app.routing.cluster')
  55.         );
  56.         // set the shard if we have something to set
  57.         if ( ! empty($this->cm->getGlobalContext()->getTenant())) {
  58.             $context->setParameter(
  59.                 'shard',
  60.                 $this->cm->getGlobalContext()->getTenant()->getSlug()
  61.             );
  62.         }
  63.     }
  64. }