Refactored the collector to separate the loggers per connection
This commit is contained in:
parent
4bf9ebc9a0
commit
3b260d268b
@ -24,15 +24,27 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
*/
|
||||
class DoctrineDataCollector extends DataCollector
|
||||
{
|
||||
private $registry;
|
||||
private $connections;
|
||||
private $managers;
|
||||
private $logger;
|
||||
private $loggers = array();
|
||||
|
||||
public function __construct(ManagerRegistry $registry, DebugStack $logger = null)
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
$this->registry = $registry;
|
||||
$this->connections = $registry->getConnectionNames();
|
||||
$this->managers = $registry->getManagerNames();
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the stack logger for a connection.
|
||||
*
|
||||
* @param string $name
|
||||
* @param DebugStack $logger
|
||||
*/
|
||||
public function addLogger($name, DebugStack $logger)
|
||||
{
|
||||
$this->loggers[$name] = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,8 +52,13 @@ class DoctrineDataCollector extends DataCollector
|
||||
*/
|
||||
public function collect(Request $request, Response $response, \Exception $exception = null)
|
||||
{
|
||||
$queries = array();
|
||||
foreach ($this->loggers as $name => $logger) {
|
||||
$queries[$name] = $this->sanitizeQueries($logger->queries);
|
||||
}
|
||||
|
||||
$this->data = array(
|
||||
'queries' => null !== $this->logger ? $this->sanitizeQueries($this->logger->queries) : array(),
|
||||
'queries' => $queries,
|
||||
'connections' => $this->connections,
|
||||
'managers' => $this->managers,
|
||||
);
|
||||
@ -59,7 +76,7 @@ class DoctrineDataCollector extends DataCollector
|
||||
|
||||
public function getQueryCount()
|
||||
{
|
||||
return count($this->data['queries']);
|
||||
return array_sum(array_map('count', $this->data['queries']));
|
||||
}
|
||||
|
||||
public function getQueries()
|
||||
@ -70,8 +87,10 @@ class DoctrineDataCollector extends DataCollector
|
||||
public function getTime()
|
||||
{
|
||||
$time = 0;
|
||||
foreach ($this->data['queries'] as $query) {
|
||||
$time += $query['executionMS'];
|
||||
foreach ($this->data['queries'] as $queries) {
|
||||
foreach ($queries as $query) {
|
||||
$time += $query['executionMS'];
|
||||
}
|
||||
}
|
||||
|
||||
return $time;
|
||||
|
@ -79,7 +79,7 @@ class DoctrineDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
$c->collect(new Request(), new Response());
|
||||
|
||||
$collected_queries = $c->getQueries();
|
||||
$this->assertEquals($expected, $collected_queries[0]['params'][0]);
|
||||
$this->assertEquals($expected, $collected_queries['default'][0]['params'][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,7 +95,7 @@ class DoctrineDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
$c = unserialize(serialize($c));
|
||||
|
||||
$collected_queries = $c->getQueries();
|
||||
$this->assertEquals($expected, $collected_queries[0]['params'][0]);
|
||||
$this->assertEquals($expected, $collected_queries['default'][0]['params'][0]);
|
||||
}
|
||||
|
||||
public function paramProvider()
|
||||
@ -126,6 +126,9 @@ class DoctrineDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
$logger = $this->getMock('Doctrine\DBAL\Logging\DebugStack');
|
||||
$logger->queries = $queries;
|
||||
|
||||
return new DoctrineDataCollector($registry, $logger);
|
||||
$collector = new DoctrineDataCollector($registry);
|
||||
$collector->addLogger('default', $logger);
|
||||
|
||||
return $collector;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user