No Description

TimeDataCollector.php 3.7KB

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