improve code

This commit is contained in:
Daniel Seifert 2022-08-18 14:18:43 +02:00
parent 9b1a1b24ed
commit a57605a729
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
8 changed files with 81 additions and 44 deletions

View File

@ -1,19 +1,21 @@
<?php <?php
use OxidEsales\Eshop\Core\DatabaseProvider;
require __DIR__ . '/../../../../source/bootstrap.php'; require __DIR__ . '/../../../../source/bootstrap.php';
\D3StartSQLLog('Query for 100 items cheaper than 49,99'); D3StartSQLLog( 'Query for 100 items cheaper than 49,99');
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
$list = $db->getAll('SELECT * '.PHP_EOL.'FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]); $list = $db->getAll('SELECT * '.PHP_EOL.'FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
\D3StopSQLLog(); D3StopSQLLog();
// or // or
\D3StartSQLLog(); D3StartSQLLog();
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]); $list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
\D3StopSQLLog(); D3StopSQLLog();

View File

@ -18,7 +18,8 @@ namespace D3\OxidSqlLogger\Extensions;
use D3\OxidSqlLogger\Handler\d3FirePHPHandler; use D3\OxidSqlLogger\Handler\d3FirePHPHandler;
use D3\OxidSqlLogger\OxidSQLLogger; use D3\OxidSqlLogger\OxidSQLLogger;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use \FirePHP; use Exception;
use FirePHP;
use Monolog\Handler\AbstractProcessingHandler; use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger; use Monolog\Logger;
use OxidEsales\EshopCommunity\Core\Database\Adapter\Doctrine\Database; use OxidEsales\EshopCommunity\Core\Database\Adapter\Doctrine\Database;

View File

@ -16,15 +16,22 @@
namespace D3\OxidSqlLogger\Handler; namespace D3\OxidSqlLogger\Handler;
use D3\OxidSqlLogger\Extensions\d3FirePHP; use D3\OxidSqlLogger\Extensions\d3FirePHP;
use Exception;
use Monolog\Handler\AbstractProcessingHandler;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
class d3FirePHPHandler extends \Monolog\Handler\AbstractProcessingHandler class d3FirePHPHandler extends AbstractProcessingHandler
{ {
const ADD_TRACE = 'addTrace'; const ADD_TRACE = 'addTrace';
/**
* @param array $record
*
* @throws Exception
*/
protected function write(array $record): void protected function write(array $record): void
{ {
$options = Registry::getConfig()->getConfigParam(d3FirePHPOptions); $options = Registry::getConfig()->getConfigParam('d3FirePHPOptions');
$options = isset($options) && is_array($options) ? $options : []; $options = isset($options) && is_array($options) ? $options : [];
$fp = d3FirePHP::getInstance(true); $fp = d3FirePHP::getInstance(true);
@ -36,7 +43,7 @@ class d3FirePHPHandler extends \Monolog\Handler\AbstractProcessingHandler
$fp->log( $record['formatted'], $record['message']); $fp->log( $record['formatted'], $record['message']);
if (in_array(self::ADD_TRACE, $options)) { if (in_array(self::ADD_TRACE, $options)) {
$fp->trace( 'trace', 'trace' ); $fp->trace( 'trace');
$fp->groupEnd(); $fp->groupEnd();
} }
} }

View File

@ -10,6 +10,7 @@ namespace D3\OxidSqlLogger;
use Monolog; use Monolog;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Traversable;
/** /**
* Class Factory * Class Factory
@ -77,7 +78,7 @@ class LoggerFactory
{ {
return function_exists('is_iterable') ? return function_exists('is_iterable') ?
is_iterable($obj) : is_iterable($obj) :
is_array($obj) || (is_object($obj) && ($obj instanceof \Traversable)); is_array($obj) || ( $obj instanceof Traversable);
} }
/** /**
@ -93,7 +94,7 @@ class LoggerFactory
$context = (new OutputFormatterStyle('yellow'))->apply('%context%'); $context = (new OutputFormatterStyle('yellow'))->apply('%context%');
$newline = PHP_EOL . str_repeat(' ', 10); $newline = PHP_EOL . str_repeat(' ', 10);
$ttl_color = "$channel $level_name: $message {$newline} $context {$newline} %extra%" . PHP_EOL; $ttl_color = "$channel $level_name: $message $newline $context $newline %extra%" . PHP_EOL;
$streamHandler->setFormatter( $streamHandler->setFormatter(
new Monolog\Formatter\LineFormatter( new Monolog\Formatter\LineFormatter(

View File

@ -9,17 +9,20 @@ namespace D3\OxidSqlLogger;
use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
/** /**
* Class OxidEsalesDatabase * Class OxidEsalesDatabase
* Is a depenction injection Helper Class * Is a depenction injection Helper Class
*/ */
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database class OxidEsalesDatabase extends Database
{ {
/** /**
* @param null $message * @param null $message
* *
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function * @deprecated use non static d3EnableLogger method or D3StartSQLLog function
*/ */
public static function enableLogger($message = null) public static function enableLogger($message = null)
@ -29,21 +32,21 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
} }
/** /**
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
*/ */
public function d3EnableLogger($message) public function d3EnableLogger($message)
{ {
$trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS); $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
/** @var Configuration $dbalConfig */ /** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration(); $dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger( $dbalConfig->setSQLLogger(
new OxidSQLLogger( new OxidSQLLogger(
isset($trace[1]['file']) ? $trace[1]['file'] : null, $trace[1]['file'] ?? null,
isset($trace[1]['line']) ? $trace[1]['line'] : null, $trace[1]['line'] ?? null,
isset($trace[2]['class']) ? $trace[2]['class'] : null, $trace[2]['class'] ?? null,
isset($trace[2]['function']) ? $trace[2]['function'] : null, $trace[2]['function'] ?? null,
$message $message
) )
); );
@ -51,7 +54,7 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
/** /**
* @return SQLLogger|null * @return SQLLogger|null
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
* @deprecated use non static d3GetLogger method * @deprecated use non static d3GetLogger method
*/ */
public static function getLogger() public static function getLogger()
@ -62,18 +65,18 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
/** /**
* @return SQLLogger|null * @return SQLLogger|null
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
*/ */
public function d3GetLogger() public function d3GetLogger()
{ {
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
/** @var Configuration $dbalConfig */ /** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration(); $dbalConfig = $database->getConnection()->getConfiguration();
return $dbalConfig->getSQLLogger(); return $dbalConfig->getSQLLogger();
} }
/** /**
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function * @deprecated use non static d3DisableLogger method or D3StopSQLLog function
*/ */
public static function disableLogger() public static function disableLogger()
@ -83,13 +86,13 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
} }
/** /**
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
*/ */
public function d3DisableLogger() public function d3DisableLogger()
{ {
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
/** @var Configuration $dbalConfig */ /** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration(); $dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger(null); $dbalConfig->setSQLLogger();
} }
} }

View File

@ -11,6 +11,7 @@ use D3\ModCfg\Application\Model\d3database;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use Monolog; use Monolog;
use NilPortugues\Sql\QueryFormatter\Formatter; use NilPortugues\Sql\QueryFormatter\Formatter;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
/** /**
* Class OxidSQLLogger * Class OxidSQLLogger
@ -29,7 +30,11 @@ class OxidSQLLogger implements SQLLogger
private $SQLQuery = null; private $SQLQuery = null;
/** /**
* @inheritDoc * @param $file
* @param $line
* @param $class
* @param $function
* @param null $message
*/ */
public function __construct($file, $line, $class, $function, $message = null) public function __construct($file, $line, $class, $function, $message = null)
{ {
@ -45,7 +50,11 @@ class OxidSQLLogger implements SQLLogger
} }
/** /**
* @inheritDoc * @param string $sql
* @param array|null $params
* @param array|null $types
*
* @throws DatabaseConnectionException
*/ */
public function startQuery($sql, array $params = null, array $types = null) public function startQuery($sql, array $params = null, array $types = null)
{ {
@ -68,9 +77,9 @@ class OxidSQLLogger implements SQLLogger
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException * @throws DatabaseConnectionException
*/ */
public function getPreparedStatementQuery(&$sql, $params = []) public function getPreparedStatementQuery(&$sql, array $params = [])
{ {
if (class_exists(d3database::class) if (class_exists(d3database::class)
&& method_exists(d3database::class, 'getPreparedStatementQuery') && method_exists(d3database::class, 'getPreparedStatementQuery')
@ -92,7 +101,7 @@ class OxidSQLLogger implements SQLLogger
$formatter = new Formatter(); $formatter = new Formatter();
Monolog\Registry::sql()->addDebug( Monolog\Registry::sql()->addDebug(
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ? $this->message : $this->SQLQuery->getSql() ), '['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ?: $this->SQLQuery->getSql() ),
[ [
'query' => $formatter->format($this->SQLQuery->getSql()), 'query' => $formatter->format($this->SQLQuery->getSql()),
'params' => $this->SQLQuery->getParams(), 'params' => $this->SQLQuery->getParams(),

View File

@ -16,7 +16,7 @@ class SQLQuery
/** /**
* @var float|null * @var float|null
*/ */
private $start_time = null; private $start_time;
/** /**
* @var float|null * @var float|null
@ -46,9 +46,6 @@ class SQLQuery
private $logStartingFunction; private $logStartingFunction;
/**
* @inheritDoc
*/
public function __construct() public function __construct()
{ {
$this->start_time = microtime(true); $this->start_time = microtime(true);
@ -224,6 +221,7 @@ class SQLQuery
* *
* @param float $microtime * @param float $microtime
* @param string $format The format to display (printf format) * @param string $format The format to display (printf format)
* @param int $round
* @return string * @return string
*/ */
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3) private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3)

View File

@ -1,27 +1,43 @@
<?php <?php
use D3\OxidSqlLogger\OxidEsalesDatabase;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
/** /**
* @author Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/ */
/**
* @param string $message
*
* @throws DatabaseConnectionException
*/
function D3StartSQLLog($message = null) { function D3StartSQLLog($message = null) {
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
$database->d3EnableLogger($message); $database->d3EnableLogger($message);
} }
/**
* @throws DatabaseConnectionException
*/
function D3StopSQLLog() function D3StopSQLLog()
{ {
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
$database->d3DisableLogger(); $database->d3DisableLogger();
} }
/**
* @param $message
*
* @throws DatabaseConnectionException
*/
function D3AddSQLLogItem($message) function D3AddSQLLogItem($message)
{ {
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
$database->d3GetLogger()->startQuery($message); $database->d3GetLogger()->startQuery($message);
$database->d3GetLogger()->stopQuery($message); $database->d3GetLogger()->stopQuery();
} }