No Description

TraceableEventDispatcher.php 3.5KB

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\HttpKernel\Profiler\Profiler;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\EventDispatcher\Event;
  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 Deprecated since version 2.4, to be removed in 3.0.
  34. */
  35. public function setProfiler(Profiler $profiler = null)
  36. {
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function preDispatch($eventName, Event $event)
  42. {
  43. switch ($eventName) {
  44. case KernelEvents::REQUEST:
  45. $this->stopwatch->openSection();
  46. break;
  47. case KernelEvents::VIEW:
  48. case KernelEvents::RESPONSE:
  49. // stop only if a controller has been executed
  50. if ($this->stopwatch->isStarted('controller')) {
  51. $this->stopwatch->stop('controller');
  52. }
  53. break;
  54. case KernelEvents::TERMINATE:
  55. $token = $event->getResponse()->headers->get('X-Debug-Token');
  56. // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
  57. // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
  58. // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
  59. // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
  60. // which must be caught.
  61. try {
  62. $this->stopwatch->openSection($token);
  63. } catch (\LogicException $e) {
  64. }
  65. break;
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function postDispatch($eventName, Event $event)
  72. {
  73. switch ($eventName) {
  74. case KernelEvents::CONTROLLER:
  75. $this->stopwatch->start('controller', 'section');
  76. break;
  77. case KernelEvents::RESPONSE:
  78. $token = $event->getResponse()->headers->get('X-Debug-Token');
  79. $this->stopwatch->stopSection($token);
  80. break;
  81. case KernelEvents::TERMINATE:
  82. // In the special case described in the `preDispatch` method above, the `$token` section
  83. // does not exist, then closing it throws an exception which must be caught.
  84. $token = $event->getResponse()->headers->get('X-Debug-Token');
  85. try {
  86. $this->stopwatch->stopSection($token);
  87. } catch (\LogicException $e) {
  88. }
  89. break;
  90. }
  91. }
  92. }