[HttpKernel] Minor fixes in the Stopwatch
This commit is contained in:
parent
009e6d739e
commit
c0ad1ac170
@ -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)
|
||||
{
|
||||
$event = $this->stop('section');
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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')));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user