src/EventSubscriber/RequestCookieDomainSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Nellapp\Bundle\SDKBundle\Enum\AppEnum;
  4. use Nellapp\Bundle\SDKBundle\Routing\Utils\DockerRoutingUtils;
  5. use Nellapp\Bundle\SDKBundle\Routing\Utils\DomainsRoutingUtils;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\KernelEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class RequestCookieDomainSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private DomainsRoutingUtils $domainsRoutingUtils,
  13.     )
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::REQUEST => ['__invoke'300], // really high priority. Before session started
  20.         ];
  21.     }
  22.     public function __invoke(KernelEvent $event): void
  23.     {
  24.         if (session_status() === PHP_SESSION_ACTIVE) {
  25.             return;
  26.         }
  27.         $request $event->getRequest();
  28.         $host $request->getHost();
  29.         $subdomainsPattern implode('|'array_map('preg_quote'$this->domainsRoutingUtils->getSubdomainsForApp())); // transform subdomains array in regex friendly string
  30.         $regex '/^(' $subdomainsPattern ')\.(.+)$/';
  31.         if (preg_match($regex$host$matches)) {
  32.             $baseDomain $matches[2]; // drosalys.com
  33.             session_set_cookie_params([
  34.                 'domain' => '.' $baseDomain,
  35.             ]);
  36.         }
  37.     }
  38. }