3
3
namespace Doctrine \Bundle \DoctrineBundle \DependencyInjection ;
4
4
5
5
use Doctrine \Bundle \DoctrineBundle \Attribute \AsEntityListener ;
6
+ use Doctrine \Bundle \DoctrineBundle \Attribute \AsMiddleware ;
6
7
use Doctrine \Bundle \DoctrineBundle \CacheWarmer \DoctrineMetadataCacheWarmer ;
7
8
use Doctrine \Bundle \DoctrineBundle \Command \Proxy \ImportDoctrineCommand ;
8
9
use Doctrine \Bundle \DoctrineBundle \Dbal \ManagerRegistryAwareConnectionProvider ;
13
14
use Doctrine \Bundle \DoctrineBundle \Repository \ServiceEntityRepositoryInterface ;
14
15
use Doctrine \DBAL \Connection ;
15
16
use Doctrine \DBAL \Connections \PrimaryReadReplicaConnection ;
16
- use Doctrine \DBAL \Driver \Middleware ;
17
+ use Doctrine \DBAL \Driver \Middleware as MiddlewareInterface ;
17
18
use Doctrine \DBAL \Logging \LoggerChain ;
19
+ use Doctrine \DBAL \Logging \Middleware ;
18
20
use Doctrine \DBAL \Sharding \PoolingShardConnection ;
19
21
use Doctrine \DBAL \Sharding \PoolingShardManager ;
20
22
use Doctrine \DBAL \Tools \Console \Command \ImportCommand ;
44
46
use Symfony \Component \DependencyInjection \Alias ;
45
47
use Symfony \Component \DependencyInjection \ChildDefinition ;
46
48
use Symfony \Component \DependencyInjection \ContainerBuilder ;
47
- use Symfony \Component \DependencyInjection \ContainerInterface ;
48
49
use Symfony \Component \DependencyInjection \Definition ;
49
50
use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
50
51
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
65
66
use function sprintf ;
66
67
use function str_replace ;
67
68
69
+ use const PHP_VERSION_ID ;
70
+
68
71
/**
69
72
* DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
70
73
*/
@@ -143,9 +146,33 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
143
146
$ container ->setParameter ('doctrine.connections ' , $ connections );
144
147
$ container ->setParameter ('doctrine.default_connection ' , $ this ->defaultConnection );
145
148
149
+ $ connWithLogging = [];
146
150
foreach ($ config ['connections ' ] as $ name => $ connection ) {
151
+ if ($ connection ['logging ' ]) {
152
+ $ connWithLogging [] = $ name ;
153
+ }
154
+
147
155
$ this ->loadDbalConnection ($ name , $ connection , $ container );
148
156
}
157
+
158
+ /** @psalm-suppress UndefinedClass */
159
+ $ container ->registerForAutoconfiguration (MiddlewareInterface::class)->addTag ('doctrine.middleware ' );
160
+
161
+ if (PHP_VERSION_ID >= 80000 && method_exists (ContainerBuilder::class, 'registerAttributeForAutoconfiguration ' )) {
162
+ $ container ->registerAttributeForAutoconfiguration (AsMiddleware::class, static function (ChildDefinition $ definition , AsMiddleware $ attribute ) {
163
+ if ($ attribute ->connections === []) {
164
+ $ definition ->addTag ('doctrine.middleware ' );
165
+
166
+ return ;
167
+ }
168
+
169
+ foreach ($ attribute ->connections as $ connName ) {
170
+ $ definition ->addTag ('doctrine.middleware ' , ['connection ' => $ connName ]);
171
+ }
172
+ });
173
+ }
174
+
175
+ $ this ->useMiddlewaresIfAvailable ($ container , $ connWithLogging );
149
176
}
150
177
151
178
/**
@@ -160,7 +187,6 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
160
187
$ configuration = $ container ->setDefinition (sprintf ('doctrine.dbal.%s_connection.configuration ' , $ name ), new ChildDefinition ('doctrine.dbal.connection.configuration ' ));
161
188
$ logger = null ;
162
189
if ($ connection ['logging ' ]) {
163
- $ this ->useMiddlewaresIfAvailable ($ connection , $ container , $ name , $ configuration );
164
190
$ logger = new Reference ('doctrine.dbal.logger ' );
165
191
}
166
192
@@ -1073,25 +1099,24 @@ private function createArrayAdapterCachePool(ContainerBuilder $container, string
1073
1099
return $ id ;
1074
1100
}
1075
1101
1076
- /** @param array< string, mixed> $connection */
1077
- protected function useMiddlewaresIfAvailable (array $ connection , ContainerBuilder $ container , string $ name , Definition $ configuration ): void
1102
+ /** @param string[] $connWithLogging */
1103
+ private function useMiddlewaresIfAvailable (ContainerBuilder $ container , array $ connWithLogging ): void
1078
1104
{
1079
1105
/** @psalm-suppress UndefinedClass */
1080
- if (! interface_exists (Middleware::class)) {
1106
+ if (! class_exists (Middleware::class)) {
1081
1107
return ;
1082
1108
}
1083
1109
1084
1110
$ container
1085
1111
->getDefinition ('doctrine.dbal.logger ' )
1086
1112
->replaceArgument (0 , null );
1087
1113
1088
- $ loggingMiddlewareDef = $ container ->setDefinition (
1089
- sprintf ('doctrine.dbal.%s_connection.logging_middleware ' , $ name ),
1090
- new ChildDefinition ('doctrine.dbal.logging_middleware ' )
1091
- );
1092
- $ loggingMiddlewareDef ->addArgument (new Reference ('logger ' , ContainerInterface::NULL_ON_INVALID_REFERENCE ));
1093
- $ loggingMiddlewareDef ->addTag ('monolog.logger ' , ['channel ' => 'doctrine ' ]);
1114
+ $ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Resources/config ' ));
1115
+ $ loader ->load ('middlewares.xml ' );
1094
1116
1095
- $ configuration ->addMethodCall ('setMiddlewares ' , [[$ loggingMiddlewareDef ]]);
1117
+ $ loggingMiddlewareAbstractDef = $ container ->getDefinition ('doctrine.dbal.logging_middleware ' );
1118
+ foreach ($ connWithLogging as $ connName ) {
1119
+ $ loggingMiddlewareAbstractDef ->addTag ('doctrine.middleware ' , ['connection ' => $ connName ]);
1120
+ }
1096
1121
}
1097
1122
}
0 commit comments