You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.2 KiB

3 years ago
  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\KernelEvents;
  13. /**
  14. * Collects some data about event listeners.
  15. *
  16. * This event dispatcher delegates the dispatching to another one.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class TraceableEventDispatcher extends BaseTraceableEventDispatcher
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function beforeDispatch(string $eventName, object $event)
  26. {
  27. switch ($eventName) {
  28. case KernelEvents::REQUEST:
  29. $this->stopwatch->openSection();
  30. break;
  31. case KernelEvents::VIEW:
  32. case KernelEvents::RESPONSE:
  33. // stop only if a controller has been executed
  34. if ($this->stopwatch->isStarted('controller')) {
  35. $this->stopwatch->stop('controller');
  36. }
  37. break;
  38. case KernelEvents::TERMINATE:
  39. $token = $event->getResponse()->headers->get('X-Debug-Token');
  40. if (null === $token) {
  41. break;
  42. }
  43. // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
  44. // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
  45. // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
  46. // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
  47. // which must be caught.
  48. try {
  49. $this->stopwatch->openSection($token);
  50. } catch (\LogicException $e) {
  51. }
  52. break;
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function afterDispatch(string $eventName, object $event)
  59. {
  60. switch ($eventName) {
  61. case KernelEvents::CONTROLLER_ARGUMENTS:
  62. $this->stopwatch->start('controller', 'section');
  63. break;
  64. case KernelEvents::RESPONSE:
  65. $token = $event->getResponse()->headers->get('X-Debug-Token');
  66. if (null === $token) {
  67. break;
  68. }
  69. $this->stopwatch->stopSection($token);
  70. break;
  71. case KernelEvents::TERMINATE:
  72. // In the special case described in the `preDispatch` method above, the `$token` section
  73. // does not exist, then closing it throws an exception which must be caught.
  74. $token = $event->getResponse()->headers->get('X-Debug-Token');
  75. if (null === $token) {
  76. break;
  77. }
  78. try {
  79. $this->stopwatch->stopSection($token);
  80. } catch (\LogicException $e) {
  81. }
  82. break;
  83. }
  84. }
  85. }