Skip to content

Commit

Permalink
DbalExtension: fix deprecations in logger setup
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk authored and f3l1x committed Apr 12, 2022
1 parent 7de7107 commit a05a1e5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
12 changes: 12 additions & 0 deletions src/ConnectionAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Nettrine\DBAL;

use Doctrine\DBAL\Connection;

interface ConnectionAccessor
{

public function get(): Connection;

}
56 changes: 30 additions & 26 deletions src/DI/DbalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nettrine\DBAL\ConnectionAccessor;
use Nettrine\DBAL\ConnectionFactory;
use Nettrine\DBAL\Events\ContainerAwareEventManager;
use Nettrine\DBAL\Events\DebugEventManager;
Expand Down Expand Up @@ -83,14 +83,7 @@ public function loadDoctrineConfiguration(): void
$config = $this->config->configuration;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);

$loggerDefinition = $builder->addDefinition($this->prefix('logger'))
->setType(LoggerChain::class)
->setAutowired('self');

$configuration = $builder->addDefinition($this->prefix('configuration'));
$configuration->setFactory(Configuration::class)
->setAutowired(false)
->addSetup('setSQLLogger', [$loggerDefinition]);
$loggers = [];

// SqlLogger (append to chain)
if ($config->sqlLogger !== null) {
Expand All @@ -102,9 +95,30 @@ public function loadDoctrineConfiguration(): void
$configLoggerDefinition->setAutowired(false);
}

$loggerDefinition->addSetup('addLogger', [$configLoggerDefinition]);
$loggers[] = $configLoggerDefinition;
}

$debugConfig = $this->config->debug;
if ($debugConfig->panel) {
$profiler = $builder->addDefinition($this->prefix('profiler'))
->setType(ProfilerLogger::class);
foreach ($debugConfig->sourcePaths as $path) {
$profiler->addSetup('addPath', [$path]);
}

$loggers[] = $profiler;
}

$loggerDefinition = $builder->addDefinition($this->prefix('logger'))
->setType(LoggerChain::class)
->setArguments(['loggers' => $loggers])
->setAutowired('self');

$configuration = $builder->addDefinition($this->prefix('configuration'));
$configuration->setFactory(Configuration::class)
->setAutowired(false)
->addSetup('setSQLLogger', [$loggerDefinition]);

// ResultCache
if ($config->resultCache !== null) {
$resultCacheName = $this->prefix('resultCache');
Expand Down Expand Up @@ -150,37 +164,27 @@ public function loadConnectionConfiguration(): void
->setFactory(ConnectionFactory::class, [$config['types'], $config['typesMapping']]);

$connectionDef = $builder->addDefinition($this->prefix('connection'))
->setFactory(Connection::class)
->setType(Connection::class)
->setFactory('@' . $this->prefix('connectionFactory') . '::createConnection', [
$config,
'@' . $this->prefix('configuration'),
$builder->getDefinitionByType(EventManager::class),
]);

$debugConfig = $this->config->debug;
if ($debugConfig->panel) {
if ($this->config->debug->panel) {
$connectionDef
->addSetup('$profiler = ?', [
new Statement(ProfilerLogger::class, [$connectionDef]),
]);

foreach ($debugConfig->sourcePaths as $path) {
$connectionDef->addSetup('$profiler->addPath(?)', [$path]);
}

$connectionDef->addSetup('?->getConfiguration()->getSqlLogger()->addLogger(?)', [
'@self',
new PhpLiteral('$profiler'),
])
->addSetup('@Tracy\Bar::addPanel', [
new Statement(QueryPanel::class, [
new PhpLiteral('$profiler'),
$this->prefix('@profiler'),
]),
])
->addSetup('@Tracy\BlueScreen::addPanel', [
[DbalBlueScreen::class, 'renderException'],
]);
}

$builder->addAccessorDefinition($this->prefix('connectionAccessor'))
->setImplement(ConnectionAccessor::class);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Logger/ProfilerLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\SQLParserUtils;
use Doctrine\DBAL\SQLParserUtilsException;
use Nettrine\DBAL\ConnectionAccessor;

class ProfilerLogger extends AbstractLogger
{

/** @var Connection */
protected $connection;
/** @var ConnectionAccessor */
protected $connectionAccessor;

public function __construct(Connection $connection)
public function __construct(ConnectionAccessor $connectionAccessor)
{
$this->connection = $connection;
$this->connectionAccessor = $connectionAccessor;
}

public function getConnection(): Connection
{
return $this->connection;
return $this->connectionAccessor->get();
}

/**
Expand Down Expand Up @@ -59,7 +60,7 @@ public function startQuery($sql, ?array $params = null, ?array $types = null): v
$quotedParams = [];
foreach ($params as $typeIndex => $value) {
$type = $types[$typeIndex] ?? null;
$quotedParams[] = $this->connection->quote($value, $type);
$quotedParams[] = $this->getConnection()->quote($value, $type);
}

return $quotedParams;
Expand Down

0 comments on commit a05a1e5

Please sign in to comment.