EventDataCollector.php 3.5KB

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