diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig index 2738ddb14c..9515272f7b 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig @@ -48,7 +48,7 @@ Threshold - ms + ms Width @@ -87,174 +87,153 @@ {% endfor %} {% endif %} - + //]]> {% endblock %} {% macro dump_request_data(token, profile, events, origin) %} diff --git a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php index 06aa36a8d8..f964d4228b 100644 --- a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php +++ b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php @@ -76,7 +76,7 @@ class Stopwatch public function start($name, $category = null) { if (!isset($this->events[$name])) { - $this->events[$name] = new StopwatchEvent($this->origin, $category); + $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category); } return $this->events[$name]->start(); diff --git a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php index 9796175ae5..f606f1bcd1 100644 --- a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php +++ b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php @@ -28,10 +28,12 @@ class StopwatchEvent * * @param integer $origin The origin time in milliseconds * @param string $category The event category + * + * @throws \InvalidArgumentException When the raw time is not valid */ public function __construct($origin, $category = null) { - $this->origin = $origin; + $this->origin = $this->formatTime($origin); $this->category = is_string($category) ? $category : 'default'; $this->started = array(); $this->periods = array(); @@ -132,7 +134,7 @@ class StopwatchEvent */ public function getEndTime() { - return count($this->periods) ? $this->periods[count($this->periods) - 1][1] : 0; + return ($count = count($this->periods)) ? $this->periods[$count - 1][1] : 0; } /** @@ -147,11 +149,29 @@ class StopwatchEvent $total += $period[1] - $period[0]; } - return sprintf('%.1f', $total); + return $this->formatTime($total); } private function getNow() { - return sprintf('%.1f', microtime(true) * 1000 - $this->origin); + return $this->formatTime(microtime(true) * 1000 - $this->origin); + } + + /** + * Formats a time. + * + * @param numerical $time A raw time + * + * @return float The formatted time + * + * @throws \InvalidArgumentException When the raw time is not valid + */ + private function formatTime($time) + { + if (!is_numeric($time)) { + throw new \InvalidArgumentException('The time must be a numerical value'); + } + + return round($time, 1); } } diff --git a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php index 5b582d99be..ba03aa32ab 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php @@ -82,7 +82,7 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface } /** - * Write data associated with the given token. + * Saves a Profile. * * @param Profile $profile A Profile instance * diff --git a/src/Symfony/Component/HttpKernel/Profiler/Profile.php b/src/Symfony/Component/HttpKernel/Profiler/Profile.php index 5caf97b71e..bed24f1d10 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/Profile.php +++ b/src/Symfony/Component/HttpKernel/Profiler/Profile.php @@ -18,7 +18,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; * * @author Fabien Potencier */ -class Profile implements \Serializable +class Profile { private $token; private $collectors; @@ -212,13 +212,8 @@ class Profile implements \Serializable return isset($this->collectors[$name]); } - public function serialize() + public function __sleep() { - return serialize(array($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->method, $this->url, $this->time)); - } - - public function unserialize($data) - { - list($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->method, $this->url, $this->time) = unserialize($data); + return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time'); } } diff --git a/src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php b/src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php index 3acf65c875..db608eaa18 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php +++ b/src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php @@ -42,7 +42,7 @@ interface ProfilerStorageInterface function read($token); /** - * Write data associated with the given token. + * Saves a Profile. * * @param Profile $profile A Profile instance * diff --git a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php index 82da5dad92..9ec05e5284 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php @@ -31,7 +31,7 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $event = new StopwatchEvent(microtime(true) * 1000); $this->assertEquals('default', $event->getCategory()); - $event = new StopwatchEvent(time(), 'cat'); + $event = new StopwatchEvent(microtime(true) * 1000, 'cat'); $this->assertEquals('cat', $event->getCategory()); } @@ -141,4 +141,12 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $end = $event->getEndTime(); $this->assertTrue($end >= 18 && $end <= 30); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidOriginThrowsAnException() + { + new StopwatchEvent("abc"); + } }