vendor/symfony/http-kernel/EventListener/SurrogateListener.php line 39

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\HttpCache\HttpCache;
  14. use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. /**
  17. * SurrogateListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for Surrogates.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @final
  22. */
  23. class SurrogateListener implements EventSubscriberInterface
  24. {
  25. private $surrogate;
  26. public function __construct(?SurrogateInterface $surrogate = null)
  27. {
  28. $this->surrogate = $surrogate;
  29. }
  30. /**
  31. * Filters the Response.
  32. */
  33. public function onKernelResponse(ResponseEvent $event)
  34. {
  35. if (!$event->isMainRequest()) {
  36. return;
  37. }
  38. $kernel = $event->getKernel();
  39. $surrogate = $this->surrogate;
  40. if ($kernel instanceof HttpCache) {
  41. $surrogate = $kernel->getSurrogate();
  42. if (null !== $this->surrogate && $this->surrogate->getName() !== $surrogate->getName()) {
  43. $surrogate = $this->surrogate;
  44. }
  45. }
  46. if (null === $surrogate) {
  47. return;
  48. }
  49. $surrogate->addSurrogateControl($event->getResponse());
  50. }
  51. public static function getSubscribedEvents(): array
  52. {
  53. return [
  54. KernelEvents::RESPONSE => 'onKernelResponse',
  55. ];
  56. }
  57. }