src/EventSubscriber/HeaderSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. final class HeaderSubscriber implements EventSubscriberInterface {
  10.     
  11.     public static function getSubscribedEvents() {
  12.         return [
  13.             KernelEvents::RESPONSE => ['addHeaders'EventPriorities::POST_RESPOND],
  14.         ];
  15.     }
  16.     
  17.     public function addHeaders(ResponseEvent $event): void {
  18.         $request $event->getRequest();
  19.         if (($data $request->attributes->get('data')) && $data instanceof Paginator) {
  20.             $response $event->getResponse();
  21.             $response->headers->add([
  22.                 'X-Total' => $data->getTotalItems(),
  23.             ]);
  24.             
  25.             $event->setResponse($response);
  26.         }
  27.         
  28.     }
  29. }