Added query count statistics to responses

This commit is contained in:
Marcin Kurczewski 2014-10-04 13:28:04 +02:00
parent 69edaa2159
commit 332ca8e4b7
4 changed files with 27 additions and 2 deletions

View file

@ -35,7 +35,7 @@ class DatabaseConnection
$cwd = getcwd();
if ($this->config->getDataDirectory())
chdir($this->config->getDataDirectory());
$this->pdo = new \PDO($this->config->database->dsn, $this->config->database->user,
$this->pdo = new PDOEx($this->config->database->dsn, $this->config->database->user,
$this->config->database->password);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
chdir($cwd);

View file

@ -4,17 +4,20 @@ namespace Szurubooru;
final class Dispatcher
{
private $router;
private $databaseConnection;
private $authService;
private $tokenService;
public function __construct(
\Szurubooru\Router $router,
\Szurubooru\DatabaseConnection $databaseConnection,
\Szurubooru\Helpers\HttpHelper $httpHelper,
\Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\TokenService $tokenService,
\Szurubooru\ControllerRepository $controllerRepository)
{
$this->router = $router;
$this->databaseConnection = $databaseConnection;
$this->httpHelper = $httpHelper;
//if script fails prematurely, mark it as fail from advance
@ -48,6 +51,7 @@ final class Dispatcher
}
$end = microtime(true);
$json['__time'] = $end - $start;
$json['__queries'] = $this->databaseConnection->getPDO()->getQueryCount();
$this->httpHelper->setResponseCode($code);
$this->httpHelper->setHeader('Content-Type', 'application/json');

18
src/PDOEx.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace Szurubooru;
class PDOEx extends \PDO
{
private $queryCount = 0;
public function prepare($statement, $driverOptions = [])
{
++ $this->queryCount;
return parent::prepare($statement, $driverOptions);
}
public function getQueryCount()
{
return $this->queryCount;
}
}

View file

@ -1,7 +1,7 @@
<?php
namespace Szurubooru\Tests;
final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
private $routerMock;
private $httpHelperMock;
@ -34,6 +34,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
$actual = $dispatcher->run('GET', '/');
unset($actual['__time']);
unset($actual['__queries']);
$this->assertEquals($expected, $actual);
}
@ -50,6 +51,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
$actual = $dispatcher->run('GET', '/');
unset($actual['__time']);
unset($actual['__queries']);
$this->assertEquals($expected, $actual);
}
@ -67,6 +69,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
{
return new \Szurubooru\Dispatcher(
$this->routerMock,
$this->databaseConnection,
$this->httpHelperMock,
$this->authServiceMock,
$this->tokenServiceMock,