EventDataCollector.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Component\VarDumper\Cloner\Data;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Contracts\Service\ResetInterface;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @see TraceableEventDispatcher
  22. *
  23. * @final
  24. */
  25. class EventDataCollector extends DataCollector implements LateDataCollectorInterface
  26. {
  27. private $dispatcher;
  28. private $requestStack;
  29. private $currentRequest = null;
  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. public function collect(Request $request, Response $response, \Throwable $exception = null)
  39. {
  40. $this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
  41. $this->data = [
  42. 'called_listeners' => [],
  43. 'not_called_listeners' => [],
  44. 'orphaned_events' => [],
  45. ];
  46. }
  47. public function reset()
  48. {
  49. $this->data = [];
  50. if ($this->dispatcher instanceof ResetInterface) {
  51. $this->dispatcher->reset();
  52. }
  53. }
  54. public function lateCollect()
  55. {
  56. if ($this->dispatcher instanceof TraceableEventDispatcher) {
  57. $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
  58. $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
  59. $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
  60. }
  61. $this->data = $this->cloneVar($this->data);
  62. }
  63. /**
  64. * @see TraceableEventDispatcher
  65. */
  66. public function setCalledListeners(array $listeners)
  67. {
  68. $this->data['called_listeners'] = $listeners;
  69. }
  70. /**
  71. * @see TraceableEventDispatcher
  72. */
  73. public function getCalledListeners(): array|Data
  74. {
  75. return $this->data['called_listeners'];
  76. }
  77. /**
  78. * @see TraceableEventDispatcher
  79. */
  80. public function setNotCalledListeners(array $listeners)
  81. {
  82. $this->data['not_called_listeners'] = $listeners;
  83. }
  84. /**
  85. * @see TraceableEventDispatcher
  86. */
  87. public function getNotCalledListeners(): array|Data
  88. {
  89. return $this->data['not_called_listeners'];
  90. }
  91. /**
  92. * @param array $events An array of orphaned events
  93. *
  94. * @see TraceableEventDispatcher
  95. */
  96. public function setOrphanedEvents(array $events)
  97. {
  98. $this->data['orphaned_events'] = $events;
  99. }
  100. /**
  101. * @see TraceableEventDispatcher
  102. */
  103. public function getOrphanedEvents(): array|Data
  104. {
  105. return $this->data['orphaned_events'];
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getName(): string
  111. {
  112. return 'events';
  113. }
  114. }