<?php
declare(strict_types=1);
namespace App\Platform\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RequestContextSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly string $appUrl)
{
}
public function onKernelRequest(RequestEvent $event): void
{
$requestContext = $this->urlGenerator->getContext();
$parsedUrl = parse_url($this->appUrl);
if ($parsedUrl === false) {
return;
}
if (isset($parsedUrl['host'])) {
$requestContext->setHost($parsedUrl['host']);
}
if (isset($parsedUrl['scheme'])) {
$requestContext->setScheme($parsedUrl['scheme']);
}
if (isset($parsedUrl['port'])) {
$requestContext->setHttpsPort($parsedUrl['port']);
$requestContext->setHttpPort($parsedUrl['port']);
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}