TimeDataCollector.php 3.6 KB

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