Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
fae28866eb | |
|
1aac154bcb | |
|
e47817b95d |
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in(__DIR__)
|
||||
;
|
||||
|
||||
$config = new PhpCsFixer\Config();
|
||||
return $config->setRules([
|
||||
'@PHP80Migration' => true,
|
||||
'@PSR12' => true
|
||||
])
|
||||
->setFinder($finder)
|
||||
;
|
|
@ -3,10 +3,15 @@
|
|||
"description": "Returns all SQL queries in the browser.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"php": "^8.0",
|
||||
"monolog/monolog": "^1",
|
||||
"nilportugues/sql-query-formatter": "^1.2.2",
|
||||
"firephp/firephp-core": "^0.5.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "~3.13.0",
|
||||
"phpstan/phpstan": "^1.10"
|
||||
},
|
||||
"license": "GPL-3.0",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
@ -26,5 +31,10 @@
|
|||
"email": "info@shopmodule.com",
|
||||
"homepage": "https://www.d3data.de"
|
||||
}
|
||||
]
|
||||
],
|
||||
"scripts": {
|
||||
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config=vendor/d3/oxid-sql-logger/.php-cs-fixer.php",
|
||||
|
||||
"phpstan": "./vendor/bin/phpstan --configuration=vendor/d3/oxid-sql-logger/phpstan.neon analyse"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||
|
||||
require __DIR__ . '/../../../../source/bootstrap.php';
|
||||
|
||||
D3StartSQLLog( 'Query for 100 items cheaper than 49,99');
|
||||
D3StartSQLLog('Query for 100 items cheaper than 49,99');
|
||||
|
||||
$db = DatabaseProvider::getDb( 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]);
|
||||
|
||||
D3StopSQLLog();
|
||||
|
@ -15,7 +17,7 @@ D3StopSQLLog();
|
|||
|
||||
D3StartSQLLog();
|
||||
|
||||
$db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$db = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||
|
||||
D3StopSQLLog();
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
parameters:
|
||||
scanFiles:
|
||||
- ../../oxid-esales/oxideshop-ce/source/bootstrap.php
|
||||
- ../../oxid-esales/oxideshop-ce/source/oxfunctions.php
|
||||
- ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php
|
||||
paths:
|
||||
- ./src
|
||||
- ./example
|
||||
level: 9
|
||||
phpVersion: 80300
|
||||
checkMissingIterableValueType: false
|
||||
treatPhpDocTypesAsCertain: false
|
||||
featureToggles:
|
||||
disableRuntimeReflectionProvider: true
|
|
@ -1,18 +1,18 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This Software is the property of Data Development and is protected
|
||||
* by copyright law - it is NOT Freeware.
|
||||
* Any unauthorized use of this software without a valid license
|
||||
* is a violation of the license agreement and will be prosecuted by
|
||||
* civil and criminal law.
|
||||
* http://www.shopmodule.com
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link http://www.oxidmodule.com
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger\Extensions;
|
||||
|
||||
use D3\OxidSqlLogger\Handler\d3FirePHPHandler;
|
||||
|
@ -32,7 +32,7 @@ class d3FirePHP extends FirePHP
|
|||
* @param boolean $autoCreate
|
||||
* @return FirePHP
|
||||
*/
|
||||
public static function getInstance($autoCreate = false)
|
||||
public static function getInstance($autoCreate = false): FirePHP
|
||||
{
|
||||
if ($autoCreate === true && !self::$instance) {
|
||||
self::init();
|
||||
|
@ -45,7 +45,7 @@ class d3FirePHP extends FirePHP
|
|||
*
|
||||
* @return FirePHP
|
||||
*/
|
||||
public static function init()
|
||||
public static function init(): FirePHP
|
||||
{
|
||||
return self::setInstance(new self());
|
||||
}
|
||||
|
@ -68,13 +68,13 @@ class d3FirePHP extends FirePHP
|
|||
*
|
||||
* @see FirePHP::TRACE
|
||||
* @param string $label
|
||||
* @return true
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trace($label)
|
||||
public function trace($label): bool
|
||||
{
|
||||
return $this->fb($label, $label, FirePHP::TRACE, array(
|
||||
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
|
||||
));
|
||||
return $this->fb($label, $label, FirePHP::TRACE, [
|
||||
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This Software is the property of Data Development and is protected
|
||||
* by copyright law - it is NOT Freeware.
|
||||
* Any unauthorized use of this software without a valid license
|
||||
* is a violation of the license agreement and will be prosecuted by
|
||||
* civil and criminal law.
|
||||
* http://www.shopmodule.com
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link http://www.oxidmodule.com
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger\Handler;
|
||||
|
||||
use D3\OxidSqlLogger\Extensions\d3FirePHP;
|
||||
|
@ -22,7 +22,7 @@ use OxidEsales\Eshop\Core\Registry;
|
|||
|
||||
class d3FirePHPHandler extends AbstractProcessingHandler
|
||||
{
|
||||
const ADD_TRACE = 'addTrace';
|
||||
public const ADD_TRACE = 'addTrace';
|
||||
|
||||
/**
|
||||
* @param array $record
|
||||
|
@ -37,14 +37,14 @@ class d3FirePHPHandler extends AbstractProcessingHandler
|
|||
$fp = d3FirePHP::getInstance(true);
|
||||
|
||||
if (in_array(self::ADD_TRACE, $options)) {
|
||||
$fp->group( $record['message'], [ 'Collapsed' => true ] );
|
||||
$fp->group($record['message'], [ 'Collapsed' => true ]);
|
||||
}
|
||||
|
||||
$fp->log( $record['formatted'], $record['message']);
|
||||
$fp->log($record['formatted'], $record['message']);
|
||||
|
||||
if (in_array(self::ADD_TRACE, $options)) {
|
||||
$fp->trace( 'trace');
|
||||
$fp->trace('trace');
|
||||
$fp->groupEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,23 +5,23 @@
|
|||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use Monolog;
|
||||
use Monolog\Logger;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* Class Factory
|
||||
*/
|
||||
class LoggerFactory
|
||||
{
|
||||
/**
|
||||
* @param $name
|
||||
* @return Monolog\Logger
|
||||
* @param string $name
|
||||
*
|
||||
* @return Logger
|
||||
*/
|
||||
public function create($name)
|
||||
public function create(string $name): Monolog\Logger
|
||||
{
|
||||
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
|
||||
}
|
||||
|
@ -29,24 +29,24 @@ class LoggerFactory
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getHandlers()
|
||||
private function getHandlers(): array
|
||||
{
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers');
|
||||
|
||||
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ?
|
||||
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
|
||||
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||
[
|
||||
$this->getStreamHandler()
|
||||
$this->getStreamHandler(),
|
||||
];
|
||||
} else {
|
||||
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
|
||||
|
||||
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ?
|
||||
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
|
||||
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||
[
|
||||
$this->getBrowserConsoleHandler(),
|
||||
$this->getFirePHPHandler()
|
||||
$this->getFirePHPHandler(),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -54,37 +54,24 @@ class LoggerFactory
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $classNames
|
||||
* @param iterable $classNames
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getInstancesFromHandlerList(array $classNames)
|
||||
private function getInstancesFromHandlerList(iterable $classNames): array
|
||||
{
|
||||
return array_map(
|
||||
function($className){
|
||||
function ($className) {
|
||||
return new $className();
|
||||
},
|
||||
$classNames
|
||||
(array) $classNames
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private function getStreamHandler()
|
||||
private function getStreamHandler(): Monolog\Handler\StreamHandler
|
||||
{
|
||||
$streamHandler = new Monolog\Handler\StreamHandler('php://stderr');
|
||||
|
||||
|
@ -111,7 +98,7 @@ class LoggerFactory
|
|||
/**
|
||||
* @return Monolog\Handler\BrowserConsoleHandler
|
||||
*/
|
||||
private function getBrowserConsoleHandler()
|
||||
private function getBrowserConsoleHandler(): Monolog\Handler\BrowserConsoleHandler
|
||||
{
|
||||
return new Monolog\Handler\BrowserConsoleHandler();
|
||||
}
|
||||
|
@ -119,7 +106,7 @@ class LoggerFactory
|
|||
/**
|
||||
* @return Monolog\Handler\FirePHPHandler
|
||||
*/
|
||||
private function getFirePHPHandler()
|
||||
private function getFirePHPHandler(): Monolog\Handler\FirePHPHandler
|
||||
{
|
||||
return new Monolog\Handler\FirePHPHandler();
|
||||
}
|
||||
|
@ -127,10 +114,17 @@ class LoggerFactory
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getProcessors()
|
||||
private function getProcessors(): array
|
||||
{
|
||||
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(),
|
||||
];
|
||||
}
|
||||
|
|
|
@ -5,48 +5,38 @@
|
|||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use Doctrine\DBAL\Configuration;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
|
||||
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
||||
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* @param null $message
|
||||
* @param string|null $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public static function enableLogger($message = null)
|
||||
public function d3EnableLogger(string $message = null): void
|
||||
{
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
|
||||
/**
|
||||
* @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(
|
||||
$this->d3GetConfiguration()->setSQLLogger(
|
||||
new OxidSQLLogger(
|
||||
$trace[1]['file'] ?? null,
|
||||
$trace[1]['line'] ?? null,
|
||||
$trace[2]['class'] ?? null,
|
||||
$trace[2]['function'] ?? null,
|
||||
$trace[1]['file'] ?? '',
|
||||
array_key_exists('line', $trace[1]) ? (int) $trace[1]['line'] : 0,
|
||||
$trace[2]['class'] ?? '',
|
||||
$trace[2]['function'] ?? '',
|
||||
$message
|
||||
)
|
||||
);
|
||||
|
@ -54,45 +44,33 @@ class OxidEsalesDatabase extends Database
|
|||
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3GetLogger method
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public static function getLogger()
|
||||
public function d3GetLogger(): ?SQLLogger
|
||||
{
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
return $database->d3GetLogger();
|
||||
return $this->d3GetConfiguration()->getSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws DatabaseConnectionException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function d3GetLogger()
|
||||
public function d3DisableLogger(): void
|
||||
{
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
return $dbalConfig->getSQLLogger();
|
||||
$this->d3GetConfiguration()->setSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function
|
||||
* @return Configuration
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public static function disableLogger()
|
||||
protected function d3GetConfiguration(): Configuration
|
||||
{
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3DisableLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function d3DisableLogger()
|
||||
{
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger();
|
||||
/** @var ConnectionProvider $connectionProvider */
|
||||
$connectionProvider = ContainerFactory::getInstance()->getContainer()
|
||||
->get(ConnectionProviderInterface::class);
|
||||
return $connectionProvider->get()->getConfiguration();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,44 +5,39 @@
|
|||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use D3\ModCfg\Application\Model\d3database;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Monolog;
|
||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* Class OxidSQLLogger
|
||||
*/
|
||||
class OxidSQLLogger implements SQLLogger
|
||||
{
|
||||
public $message;
|
||||
public $logStartingFile;
|
||||
public $logStartingLine;
|
||||
public $logStartingClass;
|
||||
public $logStartingFunction;
|
||||
public string $message;
|
||||
public string $logStartingFile;
|
||||
public int $logStartingLine;
|
||||
public string $logStartingClass;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param $class
|
||||
* @param $function
|
||||
* @param null $message
|
||||
*/
|
||||
public function __construct($file, $line, $class, $function, $message = null)
|
||||
public function __construct(string $file, int $line, string $class, string $function, string $message = null)
|
||||
{
|
||||
if (!Monolog\Registry::hasLogger('sql')) {
|
||||
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
|
||||
}
|
||||
|
||||
$this->message = $message;
|
||||
$this->message = (string) $message;
|
||||
$this->logStartingFile = $file;
|
||||
$this->logStartingLine = $line;
|
||||
$this->logStartingClass = $class;
|
||||
|
@ -53,39 +48,36 @@ class OxidSQLLogger implements SQLLogger
|
|||
* @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): void
|
||||
{
|
||||
if ($this->SQLQuery) {
|
||||
$this->SQLQuery->setCanceled();
|
||||
$this->stopQuery();
|
||||
}
|
||||
|
||||
$this->getPreparedStatementQuery($sql, $params ?? []);
|
||||
$this->getPreparedStatementQuery($sql, $params ?? [], $types ?? []);
|
||||
|
||||
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
||||
->setParams($params)
|
||||
->setTypes($types)
|
||||
->setLogStartingFile($this->logStartingFile)
|
||||
->setLogStartingLine($this->logStartingLine)
|
||||
->setLogStartingClass($this->logStartingClass)
|
||||
->setLogStartingFunction($this->logStartingFunction);
|
||||
$this->SQLQuery = (new SQLQuery())->setSql($sql)
|
||||
->setParams($params)
|
||||
->setTypes($types)
|
||||
->setLogStartingFile($this->logStartingFile)
|
||||
->setLogStartingLine($this->logStartingLine)
|
||||
->setLogStartingClass($this->logStartingClass)
|
||||
->setLogStartingFunction($this->logStartingFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @throws DatabaseConnectionException
|
||||
* @param array $params
|
||||
* @param array $types
|
||||
*/
|
||||
public function getPreparedStatementQuery(&$sql, array $params = [])
|
||||
public function getPreparedStatementQuery(string &$sql, array $params = [], array $types = []): void
|
||||
{
|
||||
if (class_exists(d3database::class)
|
||||
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
||||
&& is_array($params)
|
||||
&& count($params)
|
||||
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params))
|
||||
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params, $types))
|
||||
&& strlen(trim($query))
|
||||
) {
|
||||
$sql = $query;
|
||||
|
@ -95,13 +87,14 @@ class OxidSQLLogger implements SQLLogger
|
|||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function stopQuery()
|
||||
public function stopQuery(): void
|
||||
{
|
||||
if ($this->SQLQuery) {
|
||||
$formatter = new Formatter();
|
||||
|
||||
// @phpstan-ignore-next-line
|
||||
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()),
|
||||
'params' => $this->SQLQuery->getParams(),
|
||||
|
|
164
src/SQLQuery.php
164
src/SQLQuery.php
|
@ -1,50 +1,42 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 21:56
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
/**
|
||||
* Class SQLQuery
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
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
|
||||
*/
|
||||
private $sql = '';
|
||||
private string $sql = '';
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $params = null;
|
||||
private ?array $parameters = null;
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $types = null;
|
||||
|
||||
private $logStartingFile;
|
||||
|
||||
private $logStartingLine;
|
||||
|
||||
private $logStartingClass;
|
||||
|
||||
private $logStartingFunction;
|
||||
private ?array $parameterTypes = null;
|
||||
|
||||
private string $logStartingFile;
|
||||
|
||||
private int $logStartingLine;
|
||||
|
||||
private string $logStartingClass;
|
||||
|
||||
private string $logStartingFunction;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@ -54,88 +46,88 @@ class SQLQuery
|
|||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSql()
|
||||
public function getSql(): string
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @return SQLQuery
|
||||
* @return static
|
||||
*/
|
||||
public function setSql($sql)
|
||||
public function setSql(string $sql): static
|
||||
{
|
||||
$this->sql = $sql;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
* @return array|null
|
||||
*/
|
||||
public function getParams()
|
||||
public function getParams(): ?array
|
||||
{
|
||||
return $this->params;
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $params
|
||||
* @return SQLQuery
|
||||
* @param array|null $params
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setParams($params)
|
||||
public function setParams(array $params = null): static
|
||||
{
|
||||
$this->params = $params;
|
||||
$this->parameters = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
* @return array|null
|
||||
*/
|
||||
public function getTypes()
|
||||
public function getTypes(): ?array
|
||||
{
|
||||
return $this->types;
|
||||
return $this->parameterTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $types
|
||||
* @return SQLQuery
|
||||
* @param array|null $types
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setTypes($types)
|
||||
public function setTypes(array $types = null): static
|
||||
{
|
||||
$this->types = $types;
|
||||
$this->parameterTypes = $types;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFile()
|
||||
public function getLogStartingFile(): string
|
||||
{
|
||||
return $this->logStartingFile;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return SQLQuery
|
||||
* @param string $file
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingFile($file)
|
||||
public function setLogStartingFile(string $file): static
|
||||
{
|
||||
$this->logStartingFile = $file;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingLine()
|
||||
public function getLogStartingLine(): int
|
||||
{
|
||||
return $this->logStartingLine;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $line
|
||||
* @return SQLQuery
|
||||
* @param int $line
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingLine($line)
|
||||
public function setLogStartingLine(int $line): static
|
||||
{
|
||||
$this->logStartingLine = $line;
|
||||
return $this;
|
||||
|
@ -144,16 +136,17 @@ class SQLQuery
|
|||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingClass()
|
||||
public function getLogStartingClass(): string
|
||||
{
|
||||
return $this->logStartingClass;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $classname
|
||||
* @return SQLQuery
|
||||
* @param string $classname
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingClass($classname)
|
||||
public function setLogStartingClass(string $classname): static
|
||||
{
|
||||
$this->logStartingClass = $classname;
|
||||
return $this;
|
||||
|
@ -162,16 +155,17 @@ class SQLQuery
|
|||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFunction()
|
||||
public function getLogStartingFunction(): string
|
||||
{
|
||||
return $this->logStartingFunction;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $functionname
|
||||
* @return SQLQuery
|
||||
* @param string $functionname
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingFunction($functionname)
|
||||
public function setLogStartingFunction(string $functionname): static
|
||||
{
|
||||
$this->logStartingFunction = $functionname;
|
||||
return $this;
|
||||
|
@ -180,25 +174,25 @@ class SQLQuery
|
|||
/**
|
||||
* Statement was cancelled prematurely, an error was thrown.
|
||||
*
|
||||
* @return $this
|
||||
* @return static
|
||||
*/
|
||||
public function setCanceled()
|
||||
public function setCanceled(): static
|
||||
{
|
||||
$this->start_time = null;
|
||||
$this->start_time = 0.0;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns elapsed time
|
||||
* @return float|string
|
||||
* Return elapsed time
|
||||
* @return float
|
||||
*/
|
||||
public function getElapsedTime()
|
||||
public function getElapsedTime(): float
|
||||
{
|
||||
if ($this->start_time === null) {
|
||||
return 'Statement canceled';
|
||||
if ($this->start_time === 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if ($this->stop_time === null) {
|
||||
if ($this->stop_time === 0.0) {
|
||||
$end_time = microtime(true);
|
||||
$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
|
||||
*/
|
||||
public function getReadableElapsedTime()
|
||||
public function getReadableElapsedTime(): string
|
||||
{
|
||||
return $this->readableElapsedTime($this->getElapsedTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable elapsed time
|
||||
* Returns a human-readable elapsed time
|
||||
*
|
||||
* @param float $microtime
|
||||
* @param string $format The format to display (printf format)
|
||||
* @param int $round
|
||||
* @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) {
|
||||
$unit = 's';
|
||||
$time = round($microtime, $round);
|
||||
|
@ -237,7 +227,7 @@ class SQLQuery
|
|||
$unit = 'ms';
|
||||
$time = round($microtime*1000);
|
||||
|
||||
$format = preg_replace('/(%.[\d]+f)/', '%d', $format);
|
||||
$format = (string) preg_replace('/(%.\d+f)/', '%d', $format);
|
||||
}
|
||||
|
||||
return sprintf($format, $time, $unit);
|
||||
|
|
|
@ -1,43 +1,52 @@
|
|||
<?php
|
||||
|
||||
use D3\OxidSqlLogger\OxidEsalesDatabase;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @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 */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseConnectionException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function D3StopSQLLog()
|
||||
function D3StopSQLLog(): void
|
||||
{
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3DisableLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function D3AddSQLLogItem($message)
|
||||
function D3AddSQLLogItem(string $message): void
|
||||
{
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database->d3GetLogger()->startQuery($message);
|
||||
$database->d3GetLogger()->stopQuery();
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
if ($logger = $database->d3GetLogger()) {
|
||||
$logger->startQuery($message);
|
||||
$logger->stopQuery();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue