From a551e9b857e6b224793ade87555324589ae4da22 Mon Sep 17 00:00:00 2001 From: Tobias Matthaiou Date: Tue, 20 Aug 2019 23:34:33 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + README.md | 19 ++++++++++++++ composer.json | 23 ++++++++++++++++ example/simple.php | 10 +++++++ src/OxidEsalesDatabase.php | 33 +++++++++++++++++++++++ src/OxidSQLLogger.php | 54 ++++++++++++++++++++++++++++++++++++++ src/functions.php | 14 ++++++++++ 7 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 example/simple.php create mode 100644 src/OxidEsalesDatabase.php create mode 100644 src/OxidSQLLogger.php create mode 100644 src/functions.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..dd5bcb1 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +Oxid SQL Logger +--------------- + +Returns all SQL queries in the console browser. + +## Install + +composer require --dev tumtum/oxid-sql-logger + +## Usage + +```php +\StartSQLLog(); + +$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); +$list = $db->getAll('SELECT * FROM oxarticle WHERE oxprice < ? LIMIT 100', [49.99]); + +\StopSQLLog(); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8a3affc --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "tumtum/oxid-sql-logger", + "description": "Returns all SQL queries in the browser.", + "type": "library", + "require": { + "monolog/monolog": "^1" + }, + "license": "GPL-3.0", + "autoload": { + "psr-4": { + "tm\\oxid\\sql\\logger\\": "./src" + }, + "files": [ + "./src/functions.php" + ] + }, + "authors": [ + { + "name": "Tobias Matthaiou", + "email": "developer@tobimat.eu" + } + ] +} diff --git a/example/simple.php b/example/simple.php new file mode 100644 index 0000000..326c53f --- /dev/null +++ b/example/simple.php @@ -0,0 +1,10 @@ +getAll('SELECT * FROM oxarticle WHERE oxprice < ? LIMIT 100', [49.99]); + +\StopSQLLog(); diff --git a/src/OxidEsalesDatabase.php b/src/OxidEsalesDatabase.php new file mode 100644 index 0000000..b3d85d8 --- /dev/null +++ b/src/OxidEsalesDatabase.php @@ -0,0 +1,33 @@ + + * Date: 2019-08-20 + * Time: 21:56 + */ + +namespace tm\oxid\sql\logger; + +use Doctrine\DBAL\Configuration; + +/** + * Class OxidEsalesDatabase + * Is a depenction injection Helper Class + * + * @package tm\oxid\sql\logger + */ +class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database +{ + public static function enableLogger() + { + $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); + $dbalConfig = $database->getConnection()->getConfiguration(); + $dbalConfig->setSQLLogger(new OxidSQLLogger()); + } + + public static function disableLogger() + { + $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); + $dbalConfig = $database->getConnection()->getConfiguration(); + $dbalConfig->setSQLLogger(null); + } +} diff --git a/src/OxidSQLLogger.php b/src/OxidSQLLogger.php new file mode 100644 index 0000000..68fd02b --- /dev/null +++ b/src/OxidSQLLogger.php @@ -0,0 +1,54 @@ + + * Date: 2019-08-20 + * Time: 21:33 + */ + +namespace tm\oxid\sql\logger; + +use Doctrine\DBAL\Logging\SQLLogger; +use Monolog; + +/** + * Class OxidSQLLogger + * @package tm\oxid\sql\logger + */ +class OxidSQLLogger implements SQLLogger +{ + /** + * @inheritDoc + */ + public function __construct() + { + if (!Monolog\Registry::hasLogger('sql')) { + Monolog\Registry::addLogger( + new Monolog\Logger( + 'sql', + [ + new Monolog\Handler\BrowserConsoleHandler(), + ], + [ + new Monolog\Processor\IntrospectionProcessor(Monolog\Logger::DEBUG, ['tm\\oxid\\sql\\logger', 'Doctrine\\DBAL\\Connection', 'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database']), + new Monolog\Processor\PsrLogMessageProcessor(), + ] + ) + ); + } + } + + /** + * @inheritDoc + */ + public function startQuery($sql, array $params = null, array $types = null) + { + Monolog\Registry::sql()->addDebug($sql, ['params' => $params, 'types' => $types]); + } + + /** + * @inheritDoc + */ + public function stopQuery() + { + } +} diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..5c2dec9 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,14 @@ + + * Date: 2019-08-20 + * Time: 23:11 + */ + +function StartSQLLog() { + \tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger(); +} + +function StopSQLLog() { + \tm\oxid\sql\logger\OxidEsalesDatabase::disableLogger(); +}