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.
*
* The id parameter is used to retrieve the events from this section.
*
* @see getSectionEvents
*
* @param string $id The identifier of the section
*/
public function stopSection($id)
{
$this->stop('section');
if ($id) {
if (null !== $id) {
$this->sections[$id] = $this->events;
}
@ -74,9 +78,8 @@ class Stopwatch
if (!isset($this->events[$name])) {
$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));
}
$this->events[$name]->stop();
return $this->events[$name];
return $this->events[$name]->stop();
}
/**
@ -106,9 +107,7 @@ class Stopwatch
*/
public function lap($name)
{
$this->stop($name);
return $this->start($name);
return $this->stop($name)->start();
}
/**

View File

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

View File

@ -73,15 +73,22 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
$stopwatch->stop('foo');
$stopwatch->start('bar', 'cat');
$stopwatch->stop('bar');
$stopwatch->stopSection(1);
$stopwatch->stopSection('1');
$stopwatch->startSection();
$stopwatch->start('foobar', 'cat');
$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
$this->assertEquals(3, count($stopwatch->getSectionEvents(1)));
$this->assertEquals(2, count($stopwatch->getSectionEvents(2)));
$this->assertEquals(3, count($stopwatch->getSectionEvents('1')));
$this->assertEquals(2, count($stopwatch->getSectionEvents('2')));
$this->assertEquals(2, count($stopwatch->getSectionEvents('0')));
}
}