8
0
Fork 0
oxid-sql-logger/src/OxidEsalesDatabase.php

77 Zeilen
2.3 KiB
PHP

2019-08-20 23:34:33 +02:00
<?php
2019-09-19 19:52:54 +02:00
2019-08-20 23:34:33 +02:00
/**
2019-09-19 19:52:54 +02:00
* @author Tobias Matthaiou <developer@tobimat.eu>
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
2019-08-20 23:34:33 +02:00
*/
2024-02-02 10:59:40 +01:00
declare(strict_types=1);
2019-09-19 19:52:54 +02:00
namespace D3\OxidSqlLogger;
2019-08-20 23:34:33 +02:00
use Doctrine\DBAL\Configuration;
2021-03-02 15:31:09 +01:00
use Doctrine\DBAL\Logging\SQLLogger;
2022-08-18 14:18:43 +02:00
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
2024-02-02 10:59:40 +01:00
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
2024-02-02 11:33:11 +01:00
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider;
2024-02-02 10:59:40 +01:00
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2019-08-20 23:34:33 +02:00
2022-08-18 14:18:43 +02:00
class OxidEsalesDatabase extends Database
2019-08-20 23:34:33 +02:00
{
2021-03-02 15:31:09 +01:00
/**
2024-02-02 10:59:40 +01:00
* @param string|null $message
2021-03-02 15:31:09 +01:00
*
2024-02-02 10:59:40 +01:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2024-02-02 10:59:40 +01:00
public function d3EnableLogger(string $message = null): void
2019-08-20 23:34:33 +02:00
{
2024-02-02 10:59:40 +01:00
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
2024-02-02 10:59:40 +01:00
$this->d3GetConfiguration()->setSQLLogger(
new OxidSQLLogger(
2024-02-02 11:33:11 +01:00
$trace[1]['file'] ?? '',
array_key_exists('line', $trace[1]) ? (int) $trace[1]['line'] : 0,
$trace[2]['class'] ?? '',
$trace[2]['function'] ?? '',
$message
)
);
}
/**
2021-03-02 15:31:09 +01:00
* @return SQLLogger|null
2024-02-02 10:59:40 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2024-02-02 10:59:40 +01:00
public function d3GetLogger(): ?SQLLogger
{
2024-02-02 10:59:40 +01:00
return $this->d3GetConfiguration()->getSQLLogger();
2019-08-20 23:34:33 +02:00
}
2021-03-02 15:31:09 +01:00
/**
2024-02-02 10:59:40 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2021-03-02 15:31:09 +01:00
*/
2024-02-02 10:59:40 +01:00
public function d3DisableLogger(): void
2021-03-02 15:31:09 +01:00
{
2024-02-02 10:59:40 +01:00
$this->d3GetConfiguration()->setSQLLogger();
2021-03-02 15:31:09 +01:00
}
/**
2024-02-02 11:33:11 +01:00
* @return Configuration
2024-02-02 10:59:40 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2024-02-02 11:33:11 +01:00
protected function d3GetConfiguration(): Configuration
2019-08-20 23:34:33 +02:00
{
2024-02-02 11:33:11 +01:00
/** @var ConnectionProvider $connectionProvider */
$connectionProvider = ContainerFactory::getInstance()->getContainer()
->get(ConnectionProviderInterface::class);
return $connectionProvider->get()->getConfiguration();
2019-08-20 23:34:33 +02:00
}
}