diff --git a/src/DatabaseConnection.php b/src/DatabaseConnection.php index 55dde42a..30ac36d7 100644 --- a/src/DatabaseConnection.php +++ b/src/DatabaseConnection.php @@ -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); diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 6236acc6..2696ac20 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -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'); diff --git a/src/PDOEx.php b/src/PDOEx.php new file mode 100644 index 00000000..8146c6e8 --- /dev/null +++ b/src/PDOEx.php @@ -0,0 +1,18 @@ +queryCount; + return parent::prepare($statement, $driverOptions); + } + + public function getQueryCount() + { + return $this->queryCount; + } +} diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index d05701da..ab1a6a4a 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -1,7 +1,7 @@ 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,