No Description

EventDataCollector.php 4.0KB

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