RequestCloseSubscriber.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8. * Subscriber for all responses.
  9. */
  10. class RequestCloseSubscriber implements EventSubscriberInterface {
  11. /**
  12. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  13. */
  14. protected $moduleHandler;
  15. /**
  16. * Constructs a new RequestCloseSubscriber instance.
  17. *
  18. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  19. * The module handler.
  20. */
  21. public function __construct(ModuleHandlerInterface $module_handler) {
  22. $this->moduleHandler = $module_handler;
  23. }
  24. /**
  25. * Performs end of request tasks.
  26. *
  27. * @todo The body of this function has just been copied almost verbatim from
  28. * drupal_page_footer(). There's probably a lot in here that needs to get
  29. * removed/changed. Also, if possible, do more light-weight shutdowns on
  30. * AJAX requests.
  31. *
  32. * @param Symfony\Component\HttpKernel\Event\PostResponseEvent $event
  33. * The Event to process.
  34. */
  35. public function onTerminate(PostResponseEvent $event) {
  36. $this->moduleHandler->writeCache();
  37. }
  38. /**
  39. * Registers the methods in this class that should be listeners.
  40. *
  41. * @return array
  42. * An array of event listener definitions.
  43. */
  44. public static function getSubscribedEvents() {
  45. $events[KernelEvents::TERMINATE][] = ['onTerminate', 100];
  46. return $events;
  47. }
  48. }