TimeDataCollector.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\HttpKernel\KernelInterface;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Symfony\Component\Stopwatch\StopwatchEvent;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @final
  20. */
  21. class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
  22. {
  23. private ?KernelInterface $kernel;
  24. private ?Stopwatch $stopwatch;
  25. public function __construct(?KernelInterface $kernel = null, ?Stopwatch $stopwatch = null)
  26. {
  27. $this->kernel = $kernel;
  28. $this->stopwatch = $stopwatch;
  29. $this->data = ['events' => [], 'stopwatch_installed' => false, 'start_time' => 0];
  30. }
  31. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
  32. {
  33. if (null !== $this->kernel) {
  34. $startTime = $this->kernel->getStartTime();
  35. } else {
  36. $startTime = $request->server->get('REQUEST_TIME_FLOAT');
  37. }
  38. $this->data = [
  39. 'token' => $request->attributes->get('_stopwatch_token'),
  40. 'start_time' => $startTime * 1000,
  41. 'events' => [],
  42. 'stopwatch_installed' => class_exists(Stopwatch::class, false),
  43. ];
  44. }
  45. public function reset(): void
  46. {
  47. $this->data = ['events' => [], 'stopwatch_installed' => false, 'start_time' => 0];
  48. $this->stopwatch?->reset();
  49. }
  50. public function lateCollect(): void
  51. {
  52. if (null !== $this->stopwatch && isset($this->data['token'])) {
  53. $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  54. }
  55. unset($this->data['token']);
  56. }
  57. /**
  58. * @param StopwatchEvent[] $events The request events
  59. */
  60. public function setEvents(array $events): void
  61. {
  62. foreach ($events as $event) {
  63. $event->ensureStopped();
  64. }
  65. $this->data['events'] = $events;
  66. }
  67. /**
  68. * @return StopwatchEvent[]
  69. */
  70. public function getEvents(): array
  71. {
  72. return $this->data['events'];
  73. }
  74. /**
  75. * Gets the request elapsed time.
  76. */
  77. public function getDuration(): float
  78. {
  79. if (!isset($this->data['events']['__section__'])) {
  80. return 0;
  81. }
  82. $lastEvent = $this->data['events']['__section__'];
  83. return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
  84. }
  85. /**
  86. * Gets the initialization time.
  87. *
  88. * This is the time spent until the beginning of the request handling.
  89. */
  90. public function getInitTime(): float
  91. {
  92. if (!isset($this->data['events']['__section__'])) {
  93. return 0;
  94. }
  95. return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
  96. }
  97. public function getStartTime(): float
  98. {
  99. return $this->data['start_time'];
  100. }
  101. public function isStopwatchInstalled(): bool
  102. {
  103. return $this->data['stopwatch_installed'];
  104. }
  105. public function getName(): string
  106. {
  107. return 'time';
  108. }
  109. }