EventDataCollector.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\DataCollector;
  11. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18. * EventDataCollector.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. *
  22. * @final
  23. */
  24. class EventDataCollector extends DataCollector implements LateDataCollectorInterface
  25. {
  26. protected $dispatcher;
  27. private $requestStack;
  28. private $currentRequest;
  29. public function __construct(EventDispatcherInterface $dispatcher = null, RequestStack $requestStack = null)
  30. {
  31. $this->dispatcher = $dispatcher;
  32. $this->requestStack = $requestStack;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function collect(Request $request, Response $response, \Throwable $exception = null)
  38. {
  39. $this->currentRequest = $this->requestStack && $this->requestStack->getMasterRequest() !== $request ? $request : null;
  40. $this->data = [
  41. 'called_listeners' => [],
  42. 'not_called_listeners' => [],
  43. 'orphaned_events' => [],
  44. ];
  45. }
  46. public function reset()
  47. {
  48. $this->data = [];
  49. if ($this->dispatcher instanceof ResetInterface) {
  50. $this->dispatcher->reset();
  51. }
  52. }
  53. public function lateCollect()
  54. {
  55. if ($this->dispatcher instanceof TraceableEventDispatcher) {
  56. $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
  57. $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
  58. $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
  59. }
  60. $this->data = $this->cloneVar($this->data);
  61. }
  62. /**
  63. * Sets the called listeners.
  64. *
  65. * @param array $listeners An array of called listeners
  66. *
  67. * @see TraceableEventDispatcher
  68. */
  69. public function setCalledListeners(array $listeners)
  70. {
  71. $this->data['called_listeners'] = $listeners;
  72. }
  73. /**
  74. * Gets the called listeners.
  75. *
  76. * @return array An array of called listeners
  77. *
  78. * @see TraceableEventDispatcher
  79. */
  80. public function getCalledListeners()
  81. {
  82. return $this->data['called_listeners'];
  83. }
  84. /**
  85. * Sets the not called listeners.
  86. *
  87. * @see TraceableEventDispatcher
  88. */
  89. public function setNotCalledListeners(array $listeners)
  90. {
  91. $this->data['not_called_listeners'] = $listeners;
  92. }
  93. /**
  94. * Gets the not called listeners.
  95. *
  96. * @return array
  97. *
  98. * @see TraceableEventDispatcher
  99. */
  100. public function getNotCalledListeners()
  101. {
  102. return $this->data['not_called_listeners'];
  103. }
  104. /**
  105. * Sets the orphaned events.
  106. *
  107. * @param array $events An array of orphaned events
  108. *
  109. * @see TraceableEventDispatcher
  110. */
  111. public function setOrphanedEvents(array $events)
  112. {
  113. $this->data['orphaned_events'] = $events;
  114. }
  115. /**
  116. * Gets the orphaned events.
  117. *
  118. * @return array An array of orphaned events
  119. *
  120. * @see TraceableEventDispatcher
  121. */
  122. public function getOrphanedEvents()
  123. {
  124. return $this->data['orphaned_events'];
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getName()
  130. {
  131. return 'events';
  132. }
  133. }