src/EventSubscriber/ContentLifecycleSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\ContentLifecycleManager;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class ContentLifecycleSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private readonly ContentLifecycleManager $contentLifecycleManager,
  12.     ) {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             KernelEvents::CONTROLLER => ['onKernelController', -10],
  18.         ];
  19.     }
  20.     public function onKernelController(ControllerEvent $event): void
  21.     {
  22.         if (!$event->isMainRequest()) {
  23.             return;
  24.         }
  25.         if ($this->isStaticAsset($event->getRequest())) {
  26.             return;
  27.         }
  28.         $this->contentLifecycleManager->closeExpiredContent();
  29.     }
  30.     private function isStaticAsset(Request $request): bool
  31.     {
  32.         $path $request->getPathInfo();
  33.         return (bool) preg_match('#^/(build|assets|css|js|fonts|img|images|favicon\.ico|robots\.txt|_wdt|_profiler)#'$path);
  34.     }
  35. }