drop PHP 7 support, improve code

This commit is contained in:
Daniel Seifert 2024-02-02 10:59:40 +01:00
parent 2b6ab8f809
commit e47817b95d
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
9 changed files with 181 additions and 222 deletions

View File

@ -3,6 +3,7 @@
"description": "Returns all SQL queries in the browser.", "description": "Returns all SQL queries in the browser.",
"type": "library", "type": "library",
"require": { "require": {
"php": "^8.0",
"monolog/monolog": "^1", "monolog/monolog": "^1",
"nilportugues/sql-query-formatter": "^1.2.2", "nilportugues/sql-query-formatter": "^1.2.2",
"firephp/firephp-core": "^0.5.3" "firephp/firephp-core": "^0.5.3"

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\DatabaseProvider;
require __DIR__ . '/../../../../source/bootstrap.php'; require __DIR__ . '/../../../../source/bootstrap.php';

View File

@ -1,18 +1,18 @@
<?php <?php
/** /**
* This Software is the property of Data Development and is protected * For the full copyright and license information, please view the LICENSE
* by copyright law - it is NOT Freeware. * file that was distributed with this source code.
* Any unauthorized use of this software without a valid license *
* is a violation of the license agreement and will be prosecuted by * https://www.d3data.de
* civil and criminal law.
* http://www.shopmodule.com
* *
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch) * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com * @link https://www.oxidmodule.com
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger\Extensions; namespace D3\OxidSqlLogger\Extensions;
use D3\OxidSqlLogger\Handler\d3FirePHPHandler; use D3\OxidSqlLogger\Handler\d3FirePHPHandler;
@ -32,7 +32,7 @@ class d3FirePHP extends FirePHP
* @param boolean $autoCreate * @param boolean $autoCreate
* @return FirePHP * @return FirePHP
*/ */
public static function getInstance($autoCreate = false) public static function getInstance($autoCreate = false): FirePHP
{ {
if ($autoCreate === true && !self::$instance) { if ($autoCreate === true && !self::$instance) {
self::init(); self::init();
@ -45,7 +45,7 @@ class d3FirePHP extends FirePHP
* *
* @return FirePHP * @return FirePHP
*/ */
public static function init() public static function init(): FirePHP
{ {
return self::setInstance(new self()); return self::setInstance(new self());
} }
@ -71,7 +71,7 @@ class d3FirePHP extends FirePHP
* @return true * @return true
* @throws Exception * @throws Exception
*/ */
public function trace($label) public function trace($label): bool
{ {
return $this->fb($label, $label, FirePHP::TRACE, array( return $this->fb($label, $label, FirePHP::TRACE, array(
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) 'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)

View File

@ -1,18 +1,18 @@
<?php <?php
/** /**
* This Software is the property of Data Development and is protected * For the full copyright and license information, please view the LICENSE
* by copyright law - it is NOT Freeware. * file that was distributed with this source code.
* Any unauthorized use of this software without a valid license *
* is a violation of the license agreement and will be prosecuted by * https://www.d3data.de
* civil and criminal law.
* http://www.shopmodule.com
* *
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch) * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com * @link https://www.oxidmodule.com
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger\Handler; namespace D3\OxidSqlLogger\Handler;
use D3\OxidSqlLogger\Extensions\d3FirePHP; use D3\OxidSqlLogger\Extensions\d3FirePHP;

View File

@ -5,23 +5,21 @@
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger; 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 LoggerFactory class LoggerFactory
{ {
/** /**
* @param $name * @param $name
* @return Monolog\Logger * @return Monolog\Logger
*/ */
public function create($name) public function create($name): Monolog\Logger
{ {
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors()); return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
} }
@ -29,12 +27,12 @@ class LoggerFactory
/** /**
* @return array * @return array
*/ */
private function getHandlers() private function getHandlers(): array
{ {
if (PHP_SAPI == 'cli') { if (PHP_SAPI == 'cli') {
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers'); $configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers');
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ? $handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
$this->getInstancesFromHandlerList($configuredHandlers) : $this->getInstancesFromHandlerList($configuredHandlers) :
[ [
$this->getStreamHandler() $this->getStreamHandler()
@ -42,7 +40,7 @@ class LoggerFactory
} else { } else {
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers'); $configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ? $handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
$this->getInstancesFromHandlerList($configuredHandlers) : $this->getInstancesFromHandlerList($configuredHandlers) :
[ [
$this->getBrowserConsoleHandler(), $this->getBrowserConsoleHandler(),
@ -58,7 +56,7 @@ class LoggerFactory
* *
* @return array * @return array
*/ */
private function getInstancesFromHandlerList(array $classNames) private function getInstancesFromHandlerList(array $classNames): array
{ {
return array_map( return array_map(
function($className){ function($className){
@ -68,23 +66,10 @@ class LoggerFactory
); );
} }
/**
* polyfill for is_iterable() - available from PHP 7.1
* @param $obj
*
* @return bool
*/
private function is_iterable($obj)
{
return function_exists('is_iterable') ?
is_iterable($obj) :
is_array($obj) || ( $obj instanceof Traversable);
}
/** /**
* @return Monolog\Handler\StreamHandler * @return Monolog\Handler\StreamHandler
*/ */
private function getStreamHandler() private function getStreamHandler(): Monolog\Handler\StreamHandler
{ {
$streamHandler = new Monolog\Handler\StreamHandler('php://stderr'); $streamHandler = new Monolog\Handler\StreamHandler('php://stderr');
@ -111,7 +96,7 @@ class LoggerFactory
/** /**
* @return Monolog\Handler\BrowserConsoleHandler * @return Monolog\Handler\BrowserConsoleHandler
*/ */
private function getBrowserConsoleHandler() private function getBrowserConsoleHandler(): Monolog\Handler\BrowserConsoleHandler
{ {
return new Monolog\Handler\BrowserConsoleHandler(); return new Monolog\Handler\BrowserConsoleHandler();
} }
@ -119,7 +104,7 @@ class LoggerFactory
/** /**
* @return Monolog\Handler\FirePHPHandler * @return Monolog\Handler\FirePHPHandler
*/ */
private function getFirePHPHandler() private function getFirePHPHandler(): Monolog\Handler\FirePHPHandler
{ {
return new Monolog\Handler\FirePHPHandler(); return new Monolog\Handler\FirePHPHandler();
} }
@ -127,10 +112,17 @@ class LoggerFactory
/** /**
* @return array * @return array
*/ */
private function getProcessors() private function getProcessors(): array
{ {
return [ return [
new Monolog\Processor\IntrospectionProcessor(Monolog\Logger::DEBUG, ['D3\\OxidSqlLogger', 'Doctrine\\DBAL\\Connection', 'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database']), new Monolog\Processor\IntrospectionProcessor(
Monolog\Logger::DEBUG,
[
'D3\\OxidSqlLogger',
'Doctrine\\DBAL\\Connection',
'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database'
]
),
new Monolog\Processor\PsrLogMessageProcessor(), new Monolog\Processor\PsrLogMessageProcessor(),
]; ];
} }

View File

@ -5,46 +5,35 @@
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger; 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\Database\Adapter\Doctrine\Database;
use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class OxidEsalesDatabase
* Is a depenction injection Helper Class
*/
class OxidEsalesDatabase extends Database class OxidEsalesDatabase extends Database
{ {
/** /**
* @param null $message * @param string|null $message
* *
* @throws DatabaseConnectionException * @return void
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public static function enableLogger($message = null) public function d3EnableLogger(string $message = null): void
{ {
$database = oxNew(OxidEsalesDatabase::class); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$database->d3EnableLogger($message);
}
/** $this->d3GetConfiguration()->setSQLLogger(
* @throws DatabaseConnectionException
*/
public function d3EnableLogger($message)
{
$trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
/** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger(
new OxidSQLLogger( new OxidSQLLogger(
$trace[1]['file'] ?? null, $trace[1]['file'] ?? null,
$trace[1]['line'] ?? null, (int) $trace[1]['line'] ?? null,
$trace[2]['class'] ?? null, $trace[2]['class'] ?? null,
$trace[2]['function'] ?? null, $trace[2]['function'] ?? null,
$message $message
@ -54,45 +43,31 @@ class OxidEsalesDatabase extends Database
/** /**
* @return SQLLogger|null * @return SQLLogger|null
* @throws DatabaseConnectionException * @throws ContainerExceptionInterface
* @deprecated use non static d3GetLogger method * @throws NotFoundExceptionInterface
*/ */
public static function getLogger() public function d3GetLogger(): ?SQLLogger
{ {
$database = oxNew(OxidEsalesDatabase::class); return $this->d3GetConfiguration()->getSQLLogger();
return $database->d3GetLogger();
} }
/** /**
* @return SQLLogger|null * @throws ContainerExceptionInterface
* @throws DatabaseConnectionException * @throws NotFoundExceptionInterface
*/ */
public function d3GetLogger() public function d3DisableLogger(): void
{ {
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC); $this->d3GetConfiguration()->setSQLLogger();
/** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration();
return $dbalConfig->getSQLLogger();
} }
/** /**
* @throws DatabaseConnectionException * @return Configuration|null
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public static function disableLogger() protected function d3GetConfiguration(): ?Configuration
{ {
$database = oxNew(OxidEsalesDatabase::class); return ContainerFactory::getInstance()->getContainer()
$database->d3DisableLogger(); ->get(ConnectionProviderInterface::class)->get()->getConfiguration();
}
/**
* @throws DatabaseConnectionException
*/
public function d3DisableLogger()
{
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
/** @var Configuration $dbalConfig */
$dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger();
} }
} }

View File

@ -5,44 +5,39 @@
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com> * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger; namespace D3\OxidSqlLogger;
use D3\ModCfg\Application\Model\d3database; 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 implements SQLLogger class OxidSQLLogger implements SQLLogger
{ {
public $message; public string $message;
public $logStartingFile; public string $logStartingFile;
public $logStartingLine; public int $logStartingLine;
public $logStartingClass; public string $logStartingClass;
public $logStartingFunction; public string $logStartingFunction;
private SQLQuery|null $SQLQuery = null;
/** /**
* @var SQLQuery * @param string $file
* @param int $line
* @param string $class
* @param string $function
* @param string|null $message
*/ */
private $SQLQuery = null; public function __construct(string $file, int $line, string $class, string $function, string $message = null)
/**
* @param $file
* @param $line
* @param $class
* @param $function
* @param null $message
*/
public function __construct($file, $line, $class, $function, $message = null)
{ {
if (!Monolog\Registry::hasLogger('sql')) { if (!Monolog\Registry::hasLogger('sql')) {
Monolog\Registry::addLogger((new LoggerFactory())->create('sql')); Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
} }
$this->message = $message; $this->message = $message;
$this->logStartingFile = $file; $this->logStartingFile = $file;
$this->logStartingLine = $line; $this->logStartingLine = $line;
$this->logStartingClass = $class; $this->logStartingClass = $class;
@ -53,39 +48,36 @@ class OxidSQLLogger implements SQLLogger
* @param string $sql * @param string $sql
* @param array|null $params * @param array|null $params
* @param array|null $types * @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): void
{ {
if ($this->SQLQuery) { if ($this->SQLQuery) {
$this->SQLQuery->setCanceled(); $this->SQLQuery->setCanceled();
$this->stopQuery(); $this->stopQuery();
} }
$this->getPreparedStatementQuery($sql, $params ?? []); $this->getPreparedStatementQuery( $sql, $params ?? [], $types ?? []);
$this->SQLQuery = (new SQLQuery()) ->setSql($sql) $this->SQLQuery = (new SQLQuery())->setSql($sql)
->setParams($params) ->setParams($params)
->setTypes($types) ->setTypes($types)
->setLogStartingFile($this->logStartingFile) ->setLogStartingFile($this->logStartingFile)
->setLogStartingLine($this->logStartingLine) ->setLogStartingLine($this->logStartingLine)
->setLogStartingClass($this->logStartingClass) ->setLogStartingClass($this->logStartingClass)
->setLogStartingFunction($this->logStartingFunction); ->setLogStartingFunction($this->logStartingFunction);
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @throws DatabaseConnectionException * @param array $types
*/ */
public function getPreparedStatementQuery(&$sql, array $params = []) public function getPreparedStatementQuery(string &$sql, array $params = [], array $types = []): void
{ {
if (class_exists(d3database::class) if (class_exists(d3database::class)
&& method_exists(d3database::class, 'getPreparedStatementQuery') && method_exists(d3database::class, 'getPreparedStatementQuery')
&& is_array($params)
&& count($params) && count($params)
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params)) && ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params, $types))
&& strlen(trim($query)) && strlen(trim($query))
) { ) {
$sql = $query; $sql = $query;
@ -95,13 +87,13 @@ class OxidSQLLogger implements SQLLogger
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function stopQuery() public function stopQuery(): void
{ {
if ($this->SQLQuery) { if ($this->SQLQuery) {
$formatter = new Formatter(); $formatter = new Formatter();
Monolog\Registry::sql()->addDebug( Monolog\Registry::sql()->addDebug(
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $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

@ -1,50 +1,42 @@
<?php <?php
/** /**
* Autor: Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* Date: 2019-08-20 * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* Time: 21:56
*/ */
declare(strict_types=1);
namespace D3\OxidSqlLogger; namespace D3\OxidSqlLogger;
/**
* Class SQLQuery
* @package tm\oxid\sql\logger
*/
class SQLQuery class SQLQuery
{ {
/** /**
* @var float|null * @var float
*/ */
private $start_time; private float $start_time;
/** /**
* @var float|null * @var float
*/ */
private $stop_time = null; private float $stop_time = 0.0;
/** /**
* @var string * @var string
*/ */
private $sql = ''; private string $sql = '';
/** private ?array $parameters = null;
* @var null
*/
private $params = null;
/** private ?array $parameterTypes = null;
* @var null
*/
private $types = null;
private $logStartingFile; private string $logStartingFile;
private $logStartingLine; private int $logStartingLine;
private $logStartingClass; private string $logStartingClass;
private $logStartingFunction; private string $logStartingFunction;
public function __construct() public function __construct()
{ {
@ -54,7 +46,7 @@ class SQLQuery
/** /**
* @return string * @return string
*/ */
public function getSql() public function getSql(): string
{ {
return $this->sql; return $this->sql;
} }
@ -63,79 +55,79 @@ class SQLQuery
* @param string $sql * @param string $sql
* @return SQLQuery * @return SQLQuery
*/ */
public function setSql($sql) public function setSql(string $sql): static
{ {
$this->sql = $sql; $this->sql = $sql;
return $this; return $this;
} }
/** /**
* @return null * @return array|null
*/ */
public function getParams() public function getParams(): ?array
{ {
return $this->params; return $this->parameters;
} }
/** /**
* @param null $params * @param array|null $params
*
* @return SQLQuery * @return SQLQuery
*/ */
public function setParams($params) public function setParams(array $params = null): static
{ {
$this->params = $params; $this->parameters = $params;
return $this; return $this;
} }
/** /**
* @return null * @return array|null
*/ */
public function getTypes() public function getTypes(): ?array
{ {
return $this->types; return $this->parameterTypes;
} }
/** /**
* @param null $types * @param array|null $types
*
* @return SQLQuery * @return SQLQuery
*/ */
public function setTypes($types) public function setTypes(array $types = null): static
{ {
$this->types = $types; $this->parameterTypes = $types;
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getLogStartingFile() public function getLogStartingFile(): string
{ {
return $this->logStartingFile; return $this->logStartingFile;
} }
/** /**
* @param $file * @param string $file
* @return SQLQuery * @return SQLQuery
*/ */
public function setLogStartingFile($file) public function setLogStartingFile(string $file): static
{ {
$this->logStartingFile = $file; $this->logStartingFile = $file;
return $this; return $this;
} }
/** public function getLogStartingLine(): int
* @return string
*/
public function getLogStartingLine()
{ {
return $this->logStartingLine; return $this->logStartingLine;
} }
/** /**
* @param $line * @param int $line
*
* @return SQLQuery * @return SQLQuery
*/ */
public function setLogStartingLine($line) public function setLogStartingLine(int $line): static
{ {
$this->logStartingLine = $line; $this->logStartingLine = $line;
return $this; return $this;
@ -144,16 +136,17 @@ class SQLQuery
/** /**
* @return string * @return string
*/ */
public function getLogStartingClass() public function getLogStartingClass(): string
{ {
return $this->logStartingClass; return $this->logStartingClass;
} }
/** /**
* @param $classname * @param string $classname
*
* @return SQLQuery * @return SQLQuery
*/ */
public function setLogStartingClass($classname) public function setLogStartingClass(string $classname): static
{ {
$this->logStartingClass = $classname; $this->logStartingClass = $classname;
return $this; return $this;
@ -162,16 +155,17 @@ class SQLQuery
/** /**
* @return string * @return string
*/ */
public function getLogStartingFunction() public function getLogStartingFunction(): string
{ {
return $this->logStartingFunction; return $this->logStartingFunction;
} }
/** /**
* @param $functionname * @param string $functionname
*
* @return SQLQuery * @return SQLQuery
*/ */
public function setLogStartingFunction($functionname) public function setLogStartingFunction(string $functionname): static
{ {
$this->logStartingFunction = $functionname; $this->logStartingFunction = $functionname;
return $this; return $this;
@ -180,25 +174,25 @@ class SQLQuery
/** /**
* Statement was cancelled prematurely, an error was thrown. * Statement was cancelled prematurely, an error was thrown.
* *
* @return $this * @return SQLQuery
*/ */
public function setCanceled() public function setCanceled(): static
{ {
$this->start_time = null; $this->start_time = 0.0;
return $this; return $this;
} }
/** /**
* Returns elapsed time * Return elapsed time
* @return float|string * @return float
*/ */
public function getElapsedTime() public function getElapsedTime(): float
{ {
if ($this->start_time === null) { if ($this->start_time === 0.0) {
return 'Statement canceled'; return 0.0;
} }
if ($this->stop_time === null) { if ($this->stop_time === 0.0) {
$end_time = microtime(true); $end_time = microtime(true);
$this->stop_time = $end_time - $this->start_time; $this->stop_time = $end_time - $this->start_time;
} }
@ -207,29 +201,25 @@ class SQLQuery
} }
/** /**
* Returns a human readable elapsed time * Returns a human-readable elapsed time
* *
* @return string * @return string
*/ */
public function getReadableElapsedTime() public function getReadableElapsedTime(): string
{ {
return $this->readableElapsedTime($this->getElapsedTime()); return $this->readableElapsedTime($this->getElapsedTime());
} }
/** /**
* Returns a human readable elapsed time * Returns a human-readable elapsed time
* *
* @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 * @param int $round
* @return string * @return string
*/ */
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3) protected function readableElapsedTime(float $microtime, string $format = '%.3f%s', int $round = 3): string
{ {
if (is_string($microtime)) {
return $microtime;
}
if ($microtime >= 1) { if ($microtime >= 1) {
$unit = 's'; $unit = 's';
$time = round($microtime, $round); $time = round($microtime, $round);
@ -237,7 +227,7 @@ class SQLQuery
$unit = 'ms'; $unit = 'ms';
$time = round($microtime*1000); $time = round($microtime*1000);
$format = preg_replace('/(%.[\d]+f)/', '%d', $format); $format = preg_replace('/(%.\d+f)/', '%d', $format);
} }
return sprintf($format, $time, $unit); return sprintf($format, $time, $unit);

View File

@ -1,28 +1,34 @@
<?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>
*/ */
declare(strict_types=1);
use D3\OxidSqlLogger\OxidEsalesDatabase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/** /**
* @param string $message * @param string|null $message
* *
* @throws DatabaseConnectionException * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
function D3StartSQLLog($message = null) { function D3StartSQLLog(string $message = null): void
{
/** @var OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew( OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
$database->d3EnableLogger($message); $database->d3EnableLogger($message);
} }
/** /**
* @throws DatabaseConnectionException * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
function D3StopSQLLog() function D3StopSQLLog(): void
{ {
/** @var OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew( OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
@ -32,9 +38,10 @@ function D3StopSQLLog()
/** /**
* @param $message * @param $message
* *
* @throws DatabaseConnectionException * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
function D3AddSQLLogItem($message) function D3AddSQLLogItem($message): void
{ {
/** @var OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew( OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);