EventDataCollector.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
  15. /**
  16. * EventDataCollector.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class EventDataCollector extends DataCollector implements LateDataCollectorInterface
  21. {
  22. protected $dispatcher;
  23. public function __construct(EventDispatcherInterface $dispatcher = null)
  24. {
  25. $this->dispatcher = $dispatcher;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function collect(Request $request, Response $response, \Exception $exception = null)
  31. {
  32. $this->data = array(
  33. 'called_listeners' => array(),
  34. 'not_called_listeners' => array(),
  35. );
  36. }
  37. public function lateCollect()
  38. {
  39. if ($this->dispatcher instanceof TraceableEventDispatcherInterface) {
  40. $this->setCalledListeners($this->dispatcher->getCalledListeners());
  41. $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners());
  42. }
  43. $this->data = $this->cloneVar($this->data);
  44. }
  45. /**
  46. * Sets the called listeners.
  47. *
  48. * @param array $listeners An array of called listeners
  49. *
  50. * @see TraceableEventDispatcherInterface
  51. */
  52. public function setCalledListeners(array $listeners)
  53. {
  54. $this->data['called_listeners'] = $listeners;
  55. }
  56. /**
  57. * Gets the called listeners.
  58. *
  59. * @return array An array of called listeners
  60. *
  61. * @see TraceableEventDispatcherInterface
  62. */
  63. public function getCalledListeners()
  64. {
  65. return $this->data['called_listeners'];
  66. }
  67. /**
  68. * Sets the not called listeners.
  69. *
  70. * @param array $listeners An array of not called listeners
  71. *
  72. * @see TraceableEventDispatcherInterface
  73. */
  74. public function setNotCalledListeners(array $listeners)
  75. {
  76. $this->data['not_called_listeners'] = $listeners;
  77. }
  78. /**
  79. * Gets the not called listeners.
  80. *
  81. * @return array An array of not called listeners
  82. *
  83. * @see TraceableEventDispatcherInterface
  84. */
  85. public function getNotCalledListeners()
  86. {
  87. return $this->data['not_called_listeners'];
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getName()
  93. {
  94. return 'events';
  95. }
  96. }