ResponseGeneratorSubscriber.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7. * Response subscriber to add X-Generator header tag.
  8. */
  9. class ResponseGeneratorSubscriber implements EventSubscriberInterface {
  10. /**
  11. * Sets extra X-Generator header on successful responses.
  12. *
  13. * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  14. * The event to process.
  15. */
  16. public function onRespond(FilterResponseEvent $event) {
  17. if (!$event->isMasterRequest()) {
  18. return;
  19. }
  20. $response = $event->getResponse();
  21. // Set the generator in the HTTP header.
  22. list($version) = explode('.', \Drupal::VERSION, 2);
  23. $response->headers->set('X-Generator', 'Drupal ' . $version . ' (https://www.drupal.org)');
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function getSubscribedEvents() {
  29. $events[KernelEvents::RESPONSE][] = ['onRespond'];
  30. return $events;
  31. }
  32. }