Dashboard sipadu mbip
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TraceableEventDispatcher.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, $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. // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
  41. // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
  42. // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
  43. // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
  44. // which must be caught.
  45. try {
  46. $this->stopwatch->openSection($token);
  47. } catch (\LogicException $e) {
  48. }
  49. break;
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function afterDispatch(string $eventName, $event)
  56. {
  57. switch ($eventName) {
  58. case KernelEvents::CONTROLLER_ARGUMENTS:
  59. $this->stopwatch->start('controller', 'section');
  60. break;
  61. case KernelEvents::RESPONSE:
  62. $token = $event->getResponse()->headers->get('X-Debug-Token');
  63. $this->stopwatch->stopSection($token);
  64. break;
  65. case KernelEvents::TERMINATE:
  66. // In the special case described in the `preDispatch` method above, the `$token` section
  67. // does not exist, then closing it throws an exception which must be caught.
  68. $token = $event->getResponse()->headers->get('X-Debug-Token');
  69. try {
  70. $this->stopwatch->stopSection($token);
  71. } catch (\LogicException $e) {
  72. }
  73. break;
  74. }
  75. }
  76. }