vendor/nelmio/security-bundle/EventListener/ReferrerPolicyListener.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Nelmio SecurityBundle.
  4. *
  5. * (c) Nelmio <hello@nelm.io>
  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 Nelmio\SecurityBundle\EventListener;
  11. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. /**
  15. * Referrer Policy Listener.
  16. *
  17. * @author Andrej Hudec <pulzarraider@gmail.com>
  18. * @final
  19. */
  20. class ReferrerPolicyListener
  21. {
  22. private $policies;
  23. public function __construct(array $policies)
  24. {
  25. $this->policies = $policies;
  26. }
  27. /**
  28. * @param FilterResponseEvent|ResponseEvent $e
  29. */
  30. public function onKernelResponse($e)
  31. {
  32. // Compatibility with Symfony < 5 and Symfony >=5
  33. if (!$e instanceof FilterResponseEvent && !$e instanceof ResponseEvent) {
  34. throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : FilterResponseEvent::class, \is_object($e) ? \get_class($e) : \gettype($e)));
  35. }
  36. if (HttpKernelInterface::MASTER_REQUEST !== $e->getRequestType()) {
  37. return;
  38. }
  39. $response = $e->getResponse();
  40. $response->headers->set('Referrer-Policy', implode(', ', $this->policies));
  41. }
  42. }