Skip to content

Commit 2efe461

Browse files
committed
Allow retrieving unstopped stopwatch events
Section: - added method getEvent() Stopwatch: - added method getEvent() StopwatchTest: - modified testStart() to test for getEvent() method - added testUnknownEvent()
1 parent d3d097d commit 2efe461

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public function lap($name)
125125
return end($this->activeSections)->stopEvent($name)->start();
126126
}
127127

128+
/**
129+
* Returns a specific event by name
130+
*
131+
* @param string $name The event name
132+
*
133+
* @return StopwatchEvent A StopwatchEvent instance
134+
*/
135+
public function getEvent($name)
136+
{
137+
return end($this->activeSections)->getEvent($name);
138+
}
139+
128140
/**
129141
* Gets all events for a given section.
130142
*
@@ -293,6 +305,24 @@ public function lap($name)
293305
return $this->stopEvent($name)->start();
294306
}
295307

308+
/**
309+
* Returns a specific event by name
310+
*
311+
* @param string $name The event name
312+
*
313+
* @return StopwatchEvent The event
314+
*
315+
* @throws \LogicException When the event is not known
316+
*/
317+
public function getEvent($name)
318+
{
319+
if (!isset($this->events[$name])) {
320+
throw new \LogicException(sprintf('Event "%s" is not known.', $name));
321+
}
322+
323+
return $this->events[$name];
324+
}
325+
296326
/**
297327
* Returns the events from this section.
298328
*

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testStart()
2929

3030
$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
3131
$this->assertEquals('cat', $event->getCategory());
32+
$this->assertSame($event, $stopwatch->getEvent('foo'));
3233
}
3334

3435
public function testIsStarted()
@@ -92,6 +93,15 @@ public function testLap()
9293
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
9394
}
9495

96+
/**
97+
* @expectedException \LogicException
98+
*/
99+
public function testUnknownEvent()
100+
{
101+
$stopwatch = new Stopwatch();
102+
$stopwatch->getEvent('foo');
103+
}
104+
95105
/**
96106
* @expectedException \LogicException
97107
*/

0 commit comments

Comments
 (0)