EventDataCollector.php 3.5 KB

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