Skip to content
16 changes: 15 additions & 1 deletion src/DebugBar/DataCollector/AggregatedCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* $aggcollector['msg1']->addMessage('hello world');
* </code>
*/
class AggregatedCollector implements DataCollectorInterface, ArrayAccess
class AggregatedCollector implements DataCollectorInterface, ArrayAccess, Resettable
{
protected $name;

Expand Down Expand Up @@ -100,6 +100,20 @@ public function getSort()
return $this->sort;
}

/**
* Reset all aggregated collectors
*
* @return void
*/
public function reset()
{
foreach ($this->collectors as $collector) {
if ($collector instanceof Resettable) {
$collector->reset();
}
}
}

public function collect()
{
$aggregate = array();
Expand Down
7 changes: 6 additions & 1 deletion src/DebugBar/DataCollector/ExceptionsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Collects info about exceptions
*/
class ExceptionsCollector extends DataCollector implements Renderable
class ExceptionsCollector extends DataCollector implements Renderable, Resettable
{
protected $exceptions = array();
protected $chainExceptions = false;
Expand Down Expand Up @@ -53,6 +53,11 @@ public function getExceptions()
return $this->exceptions;
}

public function reset()
{
$this->exceptions = array();
}

public function collect()
{
return array(
Expand Down
17 changes: 16 additions & 1 deletion src/DebugBar/DataCollector/MessagesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Provides a way to log messages
*/
class MessagesCollector extends AbstractLogger implements DataCollectorInterface, MessagesAggregateInterface, Renderable
class MessagesCollector extends AbstractLogger implements DataCollectorInterface, MessagesAggregateInterface, Renderable, Resettable
{
protected $name;

Expand Down Expand Up @@ -53,6 +53,21 @@ public function getDataFormatter()
return $this->dataFormater;
}

/**
* Reset the collector to its initial state.
*
* @return void
*/
function reset()
{
$this->messages = array();
foreach ($this->aggregates as $collector) {
if ($collector instanceof ResetCollectorInterface){
$collector->reset();
}
}
}

/**
* Adds a message
*
Expand Down
19 changes: 18 additions & 1 deletion src/DebugBar/DataCollector/PDO/PDOCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use DebugBar\DataCollector\Resettable;
use DebugBar\DataCollector\TimeDataCollector;

/**
* Collects data about SQL statements executed with PDO
*/
class PDOCollector extends DataCollector implements Renderable, AssetProvider
class PDOCollector extends DataCollector implements Renderable, AssetProvider, Resettable
{
/**
* @var array|TraceablePDO[]
*/
protected $connections = array();

protected $timeCollector;
Expand Down Expand Up @@ -77,6 +81,19 @@ public function getConnections()
return $this->connections;
}

/**
* Reset the executed statements for all connections
*
* @return void
*/
public function reset()
{
foreach ($this->connections as $pdo)
{
$pdo->resetExecutedStatements();
}
}

public function collect()
{
$data = array(
Expand Down
10 changes: 10 additions & 0 deletions src/DebugBar/DataCollector/PDO/TraceablePDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public function getExecutedStatements()
return $this->executedStatements;
}

/**
* Reset the list of executed statements
*
* @return void
*/
public function resetExecutedStatements()
{
$this->executedStatements = array();
}

/**
* Returns the list of failed statements
*
Expand Down
24 changes: 24 additions & 0 deletions src/DebugBar/DataCollector/Resettable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DebugBar\DataCollector;

/**
* Resettable Interface
*/
interface Resettable
{
/**
* Reset the collector to its initial state.
*
* @return void
*/
function reset();
}
38 changes: 26 additions & 12 deletions src/DebugBar/DataCollector/TimeDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Collects info about the request duration as well as providing
* a way to log duration of any operations
*/
class TimeDataCollector extends DataCollector implements Renderable
class TimeDataCollector extends DataCollector implements Renderable, Resettable
{
/**
* @var float
Expand All @@ -43,14 +43,20 @@ class TimeDataCollector extends DataCollector implements Renderable
*/
public function __construct($requestStartTime = null)
{
if ($requestStartTime === null) {
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
$requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'];
} else {
$requestStartTime = microtime(true);
}
}
$this->requestStartTime = $requestStartTime;
if ($this->requestStartTime === null) {
$this->getRequestStartTime();
}
}

/**
* Reset thet start/end time and measures
*/
public function reset()
{
$this->requestStartTime = null;
$this->requestEndTime = null;
$this->startedMeasures = array();
}

/**
Expand Down Expand Up @@ -118,7 +124,7 @@ public function addMeasure($label, $start, $end, $params = array(), $collector =
$this->measures[] = array(
'label' => $label,
'start' => $start,
'relative_start' => $start - $this->requestStartTime,
'relative_start' => $start - $this->getRequestStartTime(),
'end' => $end,
'relative_end' => $end - $this->requestEndTime,
'duration' => $end - $start,
Expand Down Expand Up @@ -161,6 +167,14 @@ public function getMeasures()
*/
public function getRequestStartTime()
{
if ($this->requestStartTime === null) {
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
$this->requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'];
} else {
$this->requestStartTime = microtime(true);
}
}

return $this->requestStartTime;
}

Expand All @@ -182,9 +196,9 @@ public function getRequestEndTime()
public function getRequestDuration()
{
if ($this->requestEndTime !== null) {
return $this->requestEndTime - $this->requestStartTime;
return $this->requestEndTime - $this->getRequestStartTime();
}
return microtime(true) - $this->requestStartTime;
return microtime(true) - $this->getRequestStartTime();
}

public function collect()
Expand All @@ -195,7 +209,7 @@ public function collect()
}

return array(
'start' => $this->requestStartTime,
'start' => $this->getRequestStartTime(),
'end' => $this->requestEndTime,
'duration' => $this->getRequestDuration(),
'duration_str' => $this->getDataFormatter()->formatDuration($this->getRequestDuration()),
Expand Down
18 changes: 18 additions & 0 deletions src/DebugBar/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace DebugBar;

use ArrayAccess;
use DebugBar\DataCollector\Resettable;
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\Storage\StorageInterface;

Expand Down Expand Up @@ -228,6 +229,23 @@ public function collect()

return $this->data;
}

/**
* Reset the collectors and the DebugBar to the initial state.
*
* @return string
*/
public function reset()
{
foreach ($this->collectors as $collector) {
if ($collector instanceof Resettable){
$collector->reset();
}
}

$this->requestId = null;
$this->data = null;
}

/**
* Returns collected data
Expand Down