TimeDataCollector.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. protected $kernel;
  24. protected $stopwatch;
  25. public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
  26. {
  27. $this->kernel = $kernel;
  28. $this->stopwatch = $stopwatch;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function collect(Request $request, Response $response, \Throwable $exception = null)
  34. {
  35. if (null !== $this->kernel) {
  36. $startTime = $this->kernel->getStartTime();
  37. } else {
  38. $startTime = $request->server->get('REQUEST_TIME_FLOAT');
  39. }
  40. $this->data = [
  41. 'token' => $response->headers->get('X-Debug-Token'),
  42. 'start_time' => $startTime * 1000,
  43. 'events' => [],
  44. 'stopwatch_installed' => class_exists(Stopwatch::class, false),
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function reset()
  51. {
  52. $this->data = [];
  53. if (null !== $this->stopwatch) {
  54. $this->stopwatch->reset();
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function lateCollect()
  61. {
  62. if (null !== $this->stopwatch && isset($this->data['token'])) {
  63. $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  64. }
  65. unset($this->data['token']);
  66. }
  67. /**
  68. * Sets the request events.
  69. *
  70. * @param StopwatchEvent[] $events The request events
  71. */
  72. public function setEvents(array $events)
  73. {
  74. foreach ($events as $event) {
  75. $event->ensureStopped();
  76. }
  77. $this->data['events'] = $events;
  78. }
  79. /**
  80. * Gets the request events.
  81. *
  82. * @return StopwatchEvent[] The request events
  83. */
  84. public function getEvents()
  85. {
  86. return $this->data['events'];
  87. }
  88. /**
  89. * Gets the request elapsed time.
  90. *
  91. * @return float The elapsed time
  92. */
  93. public function getDuration()
  94. {
  95. if (!isset($this->data['events']['__section__'])) {
  96. return 0;
  97. }
  98. $lastEvent = $this->data['events']['__section__'];
  99. return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
  100. }
  101. /**
  102. * Gets the initialization time.
  103. *
  104. * This is the time spent until the beginning of the request handling.
  105. *
  106. * @return float The elapsed time
  107. */
  108. public function getInitTime()
  109. {
  110. if (!isset($this->data['events']['__section__'])) {
  111. return 0;
  112. }
  113. return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
  114. }
  115. /**
  116. * Gets the request time.
  117. *
  118. * @return float
  119. */
  120. public function getStartTime()
  121. {
  122. return $this->data['start_time'];
  123. }
  124. /**
  125. * @return bool whether or not the stopwatch component is installed
  126. */
  127. public function isStopwatchInstalled()
  128. {
  129. return $this->data['stopwatch_installed'];
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getName()
  135. {
  136. return 'time';
  137. }
  138. }