菜谱项目

TimeDataCollector.php 3.1KB

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