TimeDataCollector.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = array(
  40. 'token' => $response->headers->get('X-Debug-Token'),
  41. 'start_time' => $startTime * 1000,
  42. 'events' => array(),
  43. );
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function reset()
  49. {
  50. $this->data = array();
  51. if (null !== $this->stopwatch) {
  52. $this->stopwatch->reset();
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function lateCollect()
  59. {
  60. if (null !== $this->stopwatch && isset($this->data['token'])) {
  61. $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  62. }
  63. unset($this->data['token']);
  64. }
  65. /**
  66. * Sets the request events.
  67. *
  68. * @param array $events The request events
  69. */
  70. public function setEvents(array $events)
  71. {
  72. foreach ($events as $event) {
  73. $event->ensureStopped();
  74. }
  75. $this->data['events'] = $events;
  76. }
  77. /**
  78. * Gets the request events.
  79. *
  80. * @return array The request events
  81. */
  82. public function getEvents()
  83. {
  84. return $this->data['events'];
  85. }
  86. /**
  87. * Gets the request elapsed time.
  88. *
  89. * @return float The elapsed time
  90. */
  91. public function getDuration()
  92. {
  93. if (!isset($this->data['events']['__section__'])) {
  94. return 0;
  95. }
  96. $lastEvent = $this->data['events']['__section__'];
  97. return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
  98. }
  99. /**
  100. * Gets the initialization time.
  101. *
  102. * This is the time spent until the beginning of the request handling.
  103. *
  104. * @return float The elapsed time
  105. */
  106. public function getInitTime()
  107. {
  108. if (!isset($this->data['events']['__section__'])) {
  109. return 0;
  110. }
  111. return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
  112. }
  113. /**
  114. * Gets the request time.
  115. *
  116. * @return int The time
  117. */
  118. public function getStartTime()
  119. {
  120. return $this->data['start_time'];
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getName()
  126. {
  127. return 'time';
  128. }
  129. }