|
15 | 15 | use Doctrine\DBAL\DriverManager; |
16 | 16 | use Doctrine\DBAL\Connection; |
17 | 17 | use Doctrine\DBAL\Exception\ConnectionException; |
| 18 | +use Doctrine\DBAL\Logging\Middleware; |
18 | 19 | use Doctrine\ORM\Configuration; |
19 | 20 | use Doctrine\ORM\EntityManager; |
20 | 21 | use Neos\Flow\Annotations as Flow; |
21 | 22 | use Neos\Flow\Configuration\Exception\InvalidConfigurationException; |
| 23 | +use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy; |
22 | 24 | use Neos\Flow\ObjectManagement\ObjectManagerInterface; |
| 25 | +use Neos\Flow\Persistence\Doctrine\Logging\SqlLogger; |
23 | 26 | use Neos\Flow\Persistence\Doctrine\Mapping\Driver\FlowAnnotationDriver; |
24 | 27 | use Neos\Flow\Reflection\ReflectionService; |
25 | 28 | use Neos\Flow\Utility\Environment; |
26 | 29 | use Neos\Utility\Files; |
| 30 | +use Psr\Log\LoggerInterface; |
27 | 31 |
|
28 | 32 | /** |
29 | 33 | * EntityManager factory for Doctrine integration |
@@ -85,6 +89,11 @@ public function create() |
85 | 89 | $config = new Configuration(); |
86 | 90 | $config->setClassMetadataFactoryName(Mapping\ClassMetadataFactory::class); |
87 | 91 |
|
| 92 | + $configuredSqlLogger = $this->settings['doctrine']['sqlLogger'] ?? null; |
| 93 | + if ($configuredSqlLogger && class_exists($configuredSqlLogger)) { |
| 94 | + $config = $this->enableSqlLogger($configuredSqlLogger, $config); |
| 95 | + } |
| 96 | + |
88 | 97 | $eventManager = new EventManager(); |
89 | 98 |
|
90 | 99 | $flowAnnotationDriver = $this->objectManager->get(FlowAnnotationDriver::class); |
@@ -141,4 +150,30 @@ public function emitBeforeDoctrineEntityManagerCreation(Connection $connection, |
141 | 150 | public function emitAfterDoctrineEntityManagerCreation(Configuration $config, EntityManager $entityManager) |
142 | 151 | { |
143 | 152 | } |
| 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 | + } |
144 | 179 | } |
0 commit comments