Skip to content

Commit b869755

Browse files
committed
Replace deprecated doctrine SQLLogger interface
1 parent 9fad259 commit b869755

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

Neos.Flow/Classes/Persistence/Doctrine/EntityManagerConfiguration.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Neos\Flow\Configuration\Exception\InvalidConfigurationException;
2727
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
2828
use Neos\Flow\Package;
29-
use Neos\Flow\Persistence\Doctrine\Logging\SqlLogger;
3029
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
3130
use Neos\Flow\Annotations as Flow;
3231
use Psr\Cache\CacheItemPoolInterface;
@@ -80,10 +79,6 @@ public function injectSettings(array $settings): void
8079
*/
8180
public function configureEntityManager(Connection $connection, Configuration $config, EventManager $eventManager): void
8281
{
83-
if (isset($this->settings['doctrine']['sqlLogger']) && is_string($this->settings['doctrine']['sqlLogger']) && class_exists($this->settings['doctrine']['sqlLogger'])) {
84-
$this->enableSqlLogger($this->settings['doctrine']['sqlLogger'], $config);
85-
}
86-
8782
if (isset($this->settings['doctrine']['eventSubscribers']) && is_array($this->settings['doctrine']['eventSubscribers'])) {
8883
$this->registerEventSubscribers($this->settings['doctrine']['eventSubscribers'], $eventManager);
8984
}
@@ -103,21 +98,6 @@ public function configureEntityManager(Connection $connection, Configuration $co
10398
}
10499
}
105100

106-
/**
107-
* @param string $configuredSqlLogger
108-
* @param Configuration $doctrineConfiguration
109-
* @throws InvalidConfigurationException
110-
*/
111-
protected function enableSqlLogger(string $configuredSqlLogger, Configuration $doctrineConfiguration): void
112-
{
113-
$sqlLoggerInstance = new $configuredSqlLogger();
114-
if ($sqlLoggerInstance instanceof SQLLogger) {
115-
$doctrineConfiguration->setSQLLogger($sqlLoggerInstance);
116-
} else {
117-
throw new InvalidConfigurationException(sprintf('Neos.Flow.persistence.doctrine.sqlLogger must point to a \Doctrine\DBAL\Logging\SQLLogger implementation, %s given.', get_class($sqlLoggerInstance)), 1426150388);
118-
}
119-
}
120-
121101
/**
122102
* @param array $configuredSubscribers
123103
* @param EventManager $eventManager

Neos.Flow/Classes/Persistence/Doctrine/EntityManagerFactory.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
use Doctrine\DBAL\DriverManager;
1616
use Doctrine\DBAL\Connection;
1717
use Doctrine\DBAL\Exception\ConnectionException;
18+
use Doctrine\DBAL\Logging\Middleware;
1819
use Doctrine\ORM\Configuration;
1920
use Doctrine\ORM\EntityManager;
2021
use Neos\Flow\Annotations as Flow;
2122
use Neos\Flow\Configuration\Exception\InvalidConfigurationException;
23+
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
2224
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
25+
use Neos\Flow\Persistence\Doctrine\Logging\SqlLogger;
2326
use Neos\Flow\Persistence\Doctrine\Mapping\Driver\FlowAnnotationDriver;
2427
use Neos\Flow\Reflection\ReflectionService;
2528
use Neos\Flow\Utility\Environment;
2629
use Neos\Utility\Files;
30+
use Psr\Log\LoggerInterface;
2731

2832
/**
2933
* EntityManager factory for Doctrine integration
@@ -85,6 +89,11 @@ public function create()
8589
$config = new Configuration();
8690
$config->setClassMetadataFactoryName(Mapping\ClassMetadataFactory::class);
8791

92+
$configuredSqlLogger = $this->settings['doctrine']['sqlLogger'] ?? null;
93+
if ($configuredSqlLogger && class_exists($configuredSqlLogger)) {
94+
$config = $this->enableSqlLogger($configuredSqlLogger, $config);
95+
}
96+
8897
$eventManager = new EventManager();
8998

9099
$flowAnnotationDriver = $this->objectManager->get(FlowAnnotationDriver::class);
@@ -141,4 +150,30 @@ public function emitBeforeDoctrineEntityManagerCreation(Connection $connection,
141150
public function emitAfterDoctrineEntityManagerCreation(Configuration $config, EntityManager $entityManager)
142151
{
143152
}
153+
154+
/**
155+
* The logger middleware needs to be set on the configuration applied to the Driver and Connection
156+
*
157+
* @param class-string $configuredSqlLogger
158+
* @param Configuration $doctrineConfiguration
159+
* @return Configuration
160+
* @throws InvalidConfigurationException
161+
*/
162+
protected function enableSqlLogger(string $configuredSqlLogger, Configuration $doctrineConfiguration): Configuration
163+
{
164+
$sqlLoggerInstance = $this->objectManager->get($configuredSqlLogger);
165+
if (!$sqlLoggerInstance instanceof SQLLogger) {
166+
throw new InvalidConfigurationException(sprintf('Neos.Flow.persistence.doctrine.sqlLogger must be \Neos\Flow\Persistence\Doctrine\Logging\SqlLogger, %s given.', get_class($sqlLoggerInstance)), 1426150388);
167+
}
168+
169+
$logger = $sqlLoggerInstance->logger;
170+
if ($logger instanceof DependencyProxy) {
171+
$logger = $logger->_activateDependency();
172+
}
173+
if (!$logger instanceof LoggerInterface) {
174+
throw new InvalidConfigurationException(sprintf('The SqlLogger needs to get a Psr LoggerInterface injected, got "%s"', get_class($logger)), 1720548075);
175+
}
176+
177+
return $doctrineConfiguration->setMiddlewares(array_merge($doctrineConfiguration->getMiddlewares(), [new Middleware($logger)]));
178+
}
144179
}

Neos.Flow/Classes/Persistence/Doctrine/Logging/SqlLogger.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,23 @@
1313
* source code.
1414
*/
1515

16-
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
17-
use Neos\Flow\Log\Utility\LogEnvironment;
16+
use Doctrine\DBAL\Logging\Middleware;
1817
use Psr\Log\LoggerInterface;
1918

2019
/**
21-
* A SQL logger that logs to a Flow logger.
20+
* Just a stub placeholder so nothing has to be changed in user configuration,
21+
* This class has no meaning apart from being referenced in settings,
22+
* we extract the LoggerInterface from here and apply it to a {@see Middleware}
23+
*
24+
* This class itself is mentioned in Settings to enable the logger,
25+
* the property is referenced in the Objects.yaml and if someone wanted to overwrite
26+
* the logger they would have done it there, so we cannot switch to constructor injection
27+
* without backwards incompatibility.
2228
*/
23-
class SqlLogger implements \Doctrine\DBAL\Logging\SQLLogger
29+
class SqlLogger
2430
{
2531
/**
2632
* @var LoggerInterface
2733
*/
28-
protected $logger;
29-
30-
/**
31-
* Logs a SQL statement to the system logger (DEBUG priority).
32-
*
33-
* @param string $sql The SQL to be executed
34-
* @param array $params The SQL parameters
35-
* @param array $types The SQL parameter types.
36-
* @return void
37-
*/
38-
public function startQuery($sql, array $params = null, array $types = null)
39-
{
40-
if ($this->logger instanceof DependencyProxy) {
41-
$this->logger->_activateDependency();
42-
}
43-
// this is a safeguard for when no logger might be available...
44-
if ($this->logger instanceof LoggerInterface) {
45-
$this->logger->debug($sql, array_merge(LogEnvironment::fromMethodName(__METHOD__), ['params' => $params, 'types' => $types]));
46-
}
47-
}
48-
49-
/**
50-
* @return void
51-
*/
52-
public function stopQuery()
53-
{
54-
}
34+
public $logger;
5535
}

0 commit comments

Comments
 (0)