vendor/symfony/http-kernel/EventListener/ResponseListener.php line 37

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\HttpKernel\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15.  * ResponseListener fixes the Response headers based on the Request.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @final
  20.  */
  21. class ResponseListener implements EventSubscriberInterface
  22. {
  23.     private $charset;
  24.     public function __construct(string $charset)
  25.     {
  26.         $this->charset $charset;
  27.     }
  28.     /**
  29.      * Filters the Response.
  30.      */
  31.     public function onKernelResponse(ResponseEvent $event)
  32.     {
  33.         if (!$event->isMasterRequest()) {
  34.             return;
  35.         }
  36.         $response $event->getResponse();
  37.         if (null === $response->getCharset()) {
  38.             $response->setCharset($this->charset);
  39.         }
  40.         $response->prepare($event->getRequest());
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             KernelEvents::RESPONSE => 'onKernelResponse',
  46.         ];
  47.     }
  48. }