vendor/symfony/web-link/EventListener/AddLinkHeaderListener.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\WebLink\EventListener;
  11. use Psr\Link\LinkProviderInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\WebLink\HttpHeaderSerializer;
  16. /**
  17.  * Adds the Link HTTP header to the response.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  *
  21.  * @final
  22.  */
  23. class AddLinkHeaderListener implements EventSubscriberInterface
  24. {
  25.     private $serializer;
  26.     public function __construct()
  27.     {
  28.         $this->serializer = new HttpHeaderSerializer();
  29.     }
  30.     public function onKernelResponse(ResponseEvent $event)
  31.     {
  32.         if (!$event->isMasterRequest()) {
  33.             return;
  34.         }
  35.         $linkProvider $event->getRequest()->attributes->get('_links');
  36.         if (!$linkProvider instanceof LinkProviderInterface || !$links $linkProvider->getLinks()) {
  37.             return;
  38.         }
  39.         $event->getResponse()->headers->set('Link'$this->serializer->serialize($links), false);
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [KernelEvents::RESPONSE => 'onKernelResponse'];
  47.     }
  48. }