TraceableEventDispatcher.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Debug;
  11. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpKernel\Profiler\Profiler;
  15. /**
  16. * Collects some data about event listeners.
  17. *
  18. * This event dispatcher delegates the dispatching to another one.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class TraceableEventDispatcher extends BaseTraceableEventDispatcher
  23. {
  24. /**
  25. * Sets the profiler.
  26. *
  27. * The traceable event dispatcher does not use the profiler anymore.
  28. * The job is now done directly by the Profiler listener and the
  29. * data collectors themselves.
  30. *
  31. * @param Profiler|null $profiler A Profiler instance
  32. *
  33. * @deprecated since version 2.4, to be removed in 3.0.
  34. */
  35. public function setProfiler(Profiler $profiler = null)
  36. {
  37. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function preDispatch($eventName, Event $event)
  43. {
  44. switch ($eventName) {
  45. case KernelEvents::REQUEST:
  46. $this->stopwatch->openSection();
  47. break;
  48. case KernelEvents::VIEW:
  49. case KernelEvents::RESPONSE:
  50. // stop only if a controller has been executed
  51. if ($this->stopwatch->isStarted('controller')) {
  52. $this->stopwatch->stop('controller');
  53. }
  54. break;
  55. case KernelEvents::TERMINATE:
  56. $token = $event->getResponse()->headers->get('X-Debug-Token');
  57. // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
  58. // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
  59. // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
  60. // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
  61. // which must be caught.
  62. try {
  63. $this->stopwatch->openSection($token);
  64. } catch (\LogicException $e) {
  65. }
  66. break;
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function postDispatch($eventName, Event $event)
  73. {
  74. switch ($eventName) {
  75. case KernelEvents::CONTROLLER:
  76. $this->stopwatch->start('controller', 'section');
  77. break;
  78. case KernelEvents::RESPONSE:
  79. $token = $event->getResponse()->headers->get('X-Debug-Token');
  80. $this->stopwatch->stopSection($token);
  81. break;
  82. case KernelEvents::TERMINATE:
  83. // In the special case described in the `preDispatch` method above, the `$token` section
  84. // does not exist, then closing it throws an exception which must be caught.
  85. $token = $event->getResponse()->headers->get('X-Debug-Token');
  86. try {
  87. $this->stopwatch->stopSection($token);
  88. } catch (\LogicException $e) {
  89. }
  90. break;
  91. }
  92. }
  93. }