2011-10-17 09:27:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\HttpKernel\DataCollector;
|
|
|
|
|
2014-01-01 08:20:41 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2017-10-10 12:14:08 +01:00
|
|
|
use Symfony\Component\Stopwatch\Stopwatch;
|
2019-07-28 21:46:51 +01:00
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent;
|
2011-10-17 09:27:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
2013-09-30 06:59:16 +01:00
|
|
|
class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
|
2011-10-17 09:27:10 +01:00
|
|
|
{
|
|
|
|
protected $kernel;
|
2013-09-29 16:17:25 +01:00
|
|
|
protected $stopwatch;
|
2011-10-17 09:27:10 +01:00
|
|
|
|
2017-10-10 12:14:08 +01:00
|
|
|
public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
|
2011-10-17 09:27:10 +01:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
2013-09-29 16:17:25 +01:00
|
|
|
$this->stopwatch = $stopwatch;
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null)
|
|
|
|
{
|
2013-04-23 10:21:35 +01:00
|
|
|
if (null !== $this->kernel) {
|
|
|
|
$startTime = $this->kernel->getStartTime();
|
|
|
|
} else {
|
2017-01-04 10:32:26 +00:00
|
|
|
$startTime = $request->server->get('REQUEST_TIME_FLOAT');
|
2013-02-05 08:33:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->data = [
|
2014-10-30 20:17:55 +00:00
|
|
|
'token' => $response->headers->get('X-Debug-Token'),
|
2013-04-23 10:21:35 +01:00
|
|
|
'start_time' => $startTime * 1000,
|
2019-01-16 09:39:14 +00:00
|
|
|
'events' => [],
|
2019-06-13 11:34:15 +01:00
|
|
|
'stopwatch_installed' => class_exists(Stopwatch::class, false),
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 10:48:08 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->data = [];
|
2017-10-10 12:14:08 +01:00
|
|
|
|
|
|
|
if (null !== $this->stopwatch) {
|
|
|
|
$this->stopwatch->reset();
|
|
|
|
}
|
2017-10-05 10:48:08 +01:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:59:16 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function lateCollect()
|
|
|
|
{
|
|
|
|
if (null !== $this->stopwatch && isset($this->data['token'])) {
|
|
|
|
$this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
|
|
|
|
}
|
|
|
|
unset($this->data['token']);
|
|
|
|
}
|
|
|
|
|
2011-10-17 09:27:10 +01:00
|
|
|
/**
|
|
|
|
* Sets the request events.
|
|
|
|
*
|
2019-07-28 21:46:51 +01:00
|
|
|
* @param StopwatchEvent[] $events The request events
|
2011-10-17 09:27:10 +01:00
|
|
|
*/
|
|
|
|
public function setEvents(array $events)
|
|
|
|
{
|
|
|
|
foreach ($events as $event) {
|
|
|
|
$event->ensureStopped();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['events'] = $events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the request events.
|
|
|
|
*
|
2019-07-28 21:46:51 +01:00
|
|
|
* @return StopwatchEvent[] The request events
|
2011-10-17 09:27:10 +01:00
|
|
|
*/
|
|
|
|
public function getEvents()
|
|
|
|
{
|
|
|
|
return $this->data['events'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the request elapsed time.
|
|
|
|
*
|
2012-01-30 15:14:38 +00:00
|
|
|
* @return float The elapsed time
|
2011-10-17 09:27:10 +01:00
|
|
|
*/
|
2012-11-10 15:55:34 +00:00
|
|
|
public function getDuration()
|
2011-10-17 09:27:10 +01:00
|
|
|
{
|
2013-07-25 15:45:03 +01:00
|
|
|
if (!isset($this->data['events']['__section__'])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-31 06:48:19 +00:00
|
|
|
$lastEvent = $this->data['events']['__section__'];
|
2011-10-17 09:27:10 +01:00
|
|
|
|
2012-11-10 15:55:34 +00:00
|
|
|
return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the initialization time.
|
|
|
|
*
|
|
|
|
* This is the time spent until the beginning of the request handling.
|
|
|
|
*
|
2012-01-30 15:14:38 +00:00
|
|
|
* @return float The elapsed time
|
2011-10-17 09:27:10 +01:00
|
|
|
*/
|
|
|
|
public function getInitTime()
|
|
|
|
{
|
2013-07-25 15:45:03 +01:00
|
|
|
if (!isset($this->data['events']['__section__'])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-31 06:48:19 +00:00
|
|
|
return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the request time.
|
|
|
|
*
|
2019-08-23 12:30:52 +01:00
|
|
|
* @return float
|
2011-10-17 09:27:10 +01:00
|
|
|
*/
|
|
|
|
public function getStartTime()
|
|
|
|
{
|
|
|
|
return $this->data['start_time'];
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:41:44 +01:00
|
|
|
/**
|
|
|
|
* @return bool whether or not the stopwatch component is installed
|
|
|
|
*/
|
|
|
|
public function isStopwatchInstalled()
|
|
|
|
{
|
|
|
|
return $this->data['stopwatch_installed'];
|
|
|
|
}
|
|
|
|
|
2011-10-17 09:27:10 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'time';
|
|
|
|
}
|
|
|
|
}
|