vendor/sensio/framework-extra-bundle/src/EventListener/PsrResponseListener.php line 40

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 Sensio\Bundle\FrameworkExtraBundle\EventListener;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\KernelEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. /**
  17.  * Converts PSR-7 Response to HttpFoundation Response using the bridge.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. class PsrResponseListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var HttpFoundationFactoryInterface
  25.      */
  26.     private $httpFoundationFactory;
  27.     public function __construct(HttpFoundationFactoryInterface $httpFoundationFactory)
  28.     {
  29.         $this->httpFoundationFactory $httpFoundationFactory;
  30.     }
  31.     /**
  32.      * Do the conversion if applicable and update the response of the event.
  33.      */
  34.     public function onKernelView(KernelEvent $event)
  35.     {
  36.         $controllerResult $event->getControllerResult();
  37.         if (!$controllerResult instanceof ResponseInterface) {
  38.             return;
  39.         }
  40.         $event->setResponse($this->httpFoundationFactory->createResponse($controllerResult));
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::VIEW => 'onKernelView',
  49.         ];
  50.     }
  51. }