<?php
namespace App\EventSubscriber;
use App\Service\ContentLifecycleManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ContentLifecycleSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly ContentLifecycleManager $contentLifecycleManager,
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => ['onKernelController', -10],
];
}
public function onKernelController(ControllerEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
if ($this->isStaticAsset($event->getRequest())) {
return;
}
$this->contentLifecycleManager->closeExpiredContent();
}
private function isStaticAsset(Request $request): bool
{
$path = $request->getPathInfo();
return (bool) preg_match('#^/(build|assets|css|js|fonts|img|images|favicon\.ico|robots\.txt|_wdt|_profiler)#', $path);
}
}