merged branch vicb/stopwatch (PR #3088)

Commits
-------

c0ad1ac [HttpKernel] Minor fixes in the Stopwatch

Discussion
----------

[HttpKernel] Minor fixes in the Stopwatch

Not a breakthrough, fixing `'0'` handling at 2 places, some re factoring (fluid interface)
This commit is contained in:
Fabien Potencier 2012-01-11 08:14:28 +01:00
commit 1b59259c00
3 changed files with 31 additions and 16 deletions

View File

@ -42,13 +42,17 @@ class Stopwatch
/** /**
* Stops the last started section. * Stops the last started section.
* *
* The id parameter is used to retrieve the events from this section.
*
* @see getSectionEvents
*
* @param string $id The identifier of the section * @param string $id The identifier of the section
*/ */
public function stopSection($id) public function stopSection($id)
{ {
$this->stop('section'); $this->stop('section');
if ($id) { if (null !== $id) {
$this->sections[$id] = $this->events; $this->sections[$id] = $this->events;
} }
@ -74,9 +78,8 @@ class Stopwatch
if (!isset($this->events[$name])) { if (!isset($this->events[$name])) {
$this->events[$name] = new StopwatchEvent($this->origin, $category); $this->events[$name] = new StopwatchEvent($this->origin, $category);
} }
$this->events[$name]->start();
return $this->events[$name]; return $this->events[$name]->start();
} }
/** /**
@ -92,9 +95,7 @@ class Stopwatch
throw new \LogicException(sprintf('Event "%s" is not started.', $name)); throw new \LogicException(sprintf('Event "%s" is not started.', $name));
} }
$this->events[$name]->stop(); return $this->events[$name]->stop();
return $this->events[$name];
} }
/** /**
@ -106,9 +107,7 @@ class Stopwatch
*/ */
public function lap($name) public function lap($name)
{ {
$this->stop($name); return $this->stop($name)->start();
return $this->start($name);
} }
/** /**

View File

@ -32,7 +32,7 @@ class StopwatchEvent
public function __construct($origin, $category = null) public function __construct($origin, $category = null)
{ {
$this->origin = $origin; $this->origin = $origin;
$this->category = $category ?: 'default'; $this->category = is_string($category) ? $category : 'default';
$this->started = array(); $this->started = array();
$this->periods = array(); $this->periods = array();
} }
@ -59,14 +59,20 @@ class StopwatchEvent
/** /**
* Starts a new event period. * Starts a new event period.
*
* @return StopwatchEvent The event.
*/ */
public function start() public function start()
{ {
$this->started[] = $this->getNow(); $this->started[] = $this->getNow();
return $this;
} }
/** /**
* Stops the last started event period. * Stops the last started event period.
*
* @return StopwatchEvent The event.
*/ */
public function stop() public function stop()
{ {
@ -75,15 +81,18 @@ class StopwatchEvent
} }
$this->periods[] = array(array_pop($this->started), $this->getNow()); $this->periods[] = array(array_pop($this->started), $this->getNow());
return $this;
} }
/** /**
* Stops the current period and then starts a new one. * Stops the current period and then starts a new one.
*
* @return StopwatchEvent The event.
*/ */
public function lap() public function lap()
{ {
$this->stop(); return $this->stop()->start();
$this->start();
} }
/** /**

View File

@ -73,15 +73,22 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
$stopwatch->stop('foo'); $stopwatch->stop('foo');
$stopwatch->start('bar', 'cat'); $stopwatch->start('bar', 'cat');
$stopwatch->stop('bar'); $stopwatch->stop('bar');
$stopwatch->stopSection(1); $stopwatch->stopSection('1');
$stopwatch->startSection(); $stopwatch->startSection();
$stopwatch->start('foobar', 'cat'); $stopwatch->start('foobar', 'cat');
$stopwatch->stop('foobar'); $stopwatch->stop('foobar');
$stopwatch->stopSection(2); $stopwatch->stopSection('2');
$stopwatch->startSection();
$stopwatch->start('foobar', 'cat');
$stopwatch->stop('foobar');
$stopwatch->stopSection('0');
// the section is an event by itself // the section is an event by itself
$this->assertEquals(3, count($stopwatch->getSectionEvents(1))); $this->assertEquals(3, count($stopwatch->getSectionEvents('1')));
$this->assertEquals(2, count($stopwatch->getSectionEvents(2))); $this->assertEquals(2, count($stopwatch->getSectionEvents('2')));
$this->assertEquals(2, count($stopwatch->getSectionEvents('0')));
} }
} }