diff --git a/src/DAMA/DoctrineTestBundle/DAMADoctrineTestBundle.php b/src/DAMA/DoctrineTestBundle/DAMADoctrineTestBundle.php index 0e618b8..a2c6353 100644 --- a/src/DAMA/DoctrineTestBundle/DAMADoctrineTestBundle.php +++ b/src/DAMA/DoctrineTestBundle/DAMADoctrineTestBundle.php @@ -2,7 +2,8 @@ namespace DAMA\DoctrineTestBundle; -use DAMA\DoctrineTestBundle\DependencyInjection\DoctrineTestCompilerPass; +use DAMA\DoctrineTestBundle\DependencyInjection\AddMiddlewaresCompilerPass; +use DAMA\DoctrineTestBundle\DependencyInjection\ModifyDoctrineConfigCompilerPass; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -13,6 +14,9 @@ public function build(ContainerBuilder $container): void { parent::build($container); // lower priority than CacheCompatibilityPass from DoctrineBundle - $container->addCompilerPass(new DoctrineTestCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1); + $container->addCompilerPass(new ModifyDoctrineConfigCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1); + + // higher priority than MiddlewaresPass from DoctrineBundle + $container->addCompilerPass(new AddMiddlewaresCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1); } } diff --git a/src/DAMA/DoctrineTestBundle/DependencyInjection/AddMiddlewaresCompilerPass.php b/src/DAMA/DoctrineTestBundle/DependencyInjection/AddMiddlewaresCompilerPass.php new file mode 100644 index 0000000..8972915 --- /dev/null +++ b/src/DAMA/DoctrineTestBundle/DependencyInjection/AddMiddlewaresCompilerPass.php @@ -0,0 +1,70 @@ + $connections */ + $connections = $container->getParameter('doctrine.connections'); + $connectionNames = array_keys($connections); + $transactionalBehaviorEnabledConnections = $this->getTransactionEnabledConnectionNames($container, $connectionNames); + $container->getParameterBag()->set(self::TRANSACTIONAL_BEHAVIOR_ENABLED_CONNECTIONS, $transactionalBehaviorEnabledConnections); + + foreach ($transactionalBehaviorEnabledConnections as $name) { + $middlewareDefinition = $container->register(sprintf('dama.doctrine.dbal.middleware.%s', $name), Middleware::class); + $middlewareDefinition->addTag('doctrine.middleware', ['connection' => $name, 'priority' => 100]); + } + + $container->getParameterBag()->remove('dama.'.Configuration::ENABLE_STATIC_CONNECTION); + } + + /** + * @param string[] $connectionNames + * + * @return string[] + */ + private function getTransactionEnabledConnectionNames(ContainerBuilder $container, array $connectionNames): array + { + /** @var bool|array $enableStaticConnectionsConfig */ + $enableStaticConnectionsConfig = $container->getParameter('dama.'.Configuration::ENABLE_STATIC_CONNECTION); + + if (is_array($enableStaticConnectionsConfig)) { + $this->validateConnectionNames(array_keys($enableStaticConnectionsConfig), $connectionNames); + } + + $enabledConnections = []; + + foreach ($connectionNames as $name) { + if ($enableStaticConnectionsConfig === true + || isset($enableStaticConnectionsConfig[$name]) && $enableStaticConnectionsConfig[$name] === true + ) { + $enabledConnections[] = $name; + } + } + + return $enabledConnections; + } + + /** + * @param string[] $configNames + * @param string[] $existingNames + */ + private function validateConnectionNames(array $configNames, array $existingNames): void + { + $unknown = array_diff($configNames, $existingNames); + + if (count($unknown)) { + throw new \InvalidArgumentException(sprintf('Unknown doctrine dbal connection name(s): %s.', implode(', ', $unknown))); + } + } +} diff --git a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php b/src/DAMA/DoctrineTestBundle/DependencyInjection/ModifyDoctrineConfigCompilerPass.php similarity index 78% rename from src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php rename to src/DAMA/DoctrineTestBundle/DependencyInjection/ModifyDoctrineConfigCompilerPass.php index 7b553e5..034df9b 100644 --- a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php +++ b/src/DAMA/DoctrineTestBundle/DependencyInjection/ModifyDoctrineConfigCompilerPass.php @@ -3,7 +3,6 @@ namespace DAMA\DoctrineTestBundle\DependencyInjection; use DAMA\DoctrineTestBundle\Doctrine\Cache\Psr6StaticArrayCache; -use DAMA\DoctrineTestBundle\Doctrine\DBAL\Middleware; use Doctrine\Common\Cache\Cache; use Doctrine\DBAL\Connection; use Psr\Cache\CacheItemPoolInterface; @@ -11,13 +10,11 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Reference; -class DoctrineTestCompilerPass implements CompilerPassInterface +final class ModifyDoctrineConfigCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { - $container->register('dama.doctrine.dbal.middleware', Middleware::class); $cacheNames = []; if ($container->getParameter('dama.'.Configuration::STATIC_META_CACHE)) { @@ -31,7 +28,9 @@ public function process(ContainerBuilder $container): void /** @var array $connections */ $connections = $container->getParameter('doctrine.connections'); $connectionNames = array_keys($connections); - $transactionalBehaviorEnabledConnections = $this->getTransactionEnabledConnectionNames($container, $connectionNames); + $transactionalBehaviorEnabledConnections = $container->getParameter( + AddMiddlewaresCompilerPass::TRANSACTIONAL_BEHAVIOR_ENABLED_CONNECTIONS, + ); $connectionKeys = $this->getConnectionKeys($container, $connectionNames); foreach ($connectionNames as $name) { @@ -60,6 +59,7 @@ public function process(ContainerBuilder $container): void $container->getParameterBag()->remove('dama.'.Configuration::STATIC_META_CACHE); $container->getParameterBag()->remove('dama.'.Configuration::STATIC_QUERY_CACHE); $container->getParameterBag()->remove('dama.'.Configuration::CONNECTION_KEYS); + $container->getParameterBag()->remove(AddMiddlewaresCompilerPass::TRANSACTIONAL_BEHAVIOR_ENABLED_CONNECTIONS); } /** @@ -79,24 +79,6 @@ private function modifyConnectionService(ContainerBuilder $container, $connectio 0, $this->getModifiedConnectionOptions($connectionOptions, $connectionKey, $name), ); - - $connectionConfig = $container->getDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name)); - $methodCalls = $connectionConfig->getMethodCalls(); - $middlewareRef = new Reference('dama.doctrine.dbal.middleware'); - $hasMiddlewaresMethodCall = false; - foreach ($methodCalls as &$methodCall) { - if ($methodCall[0] === 'setMiddlewares') { - $hasMiddlewaresMethodCall = true; - // our middleware needs to be the first one here so we wrap the "native" driver - $methodCall[1][0] = array_merge([$middlewareRef], $methodCall[1][0]); - } - } - - if (!$hasMiddlewaresMethodCall) { - $methodCalls[] = ['setMiddlewares', [[$middlewareRef]]]; - } - - $connectionConfig->setMethodCalls($methodCalls); } /** @@ -166,33 +148,6 @@ private function registerStaticCache( $container->setDefinition($cacheServiceId, $cache); } - /** - * @param string[] $connectionNames - * - * @return string[] - */ - private function getTransactionEnabledConnectionNames(ContainerBuilder $container, array $connectionNames): array - { - /** @var bool|array $enableStaticConnectionsConfig */ - $enableStaticConnectionsConfig = $container->getParameter('dama.'.Configuration::ENABLE_STATIC_CONNECTION); - - if (is_array($enableStaticConnectionsConfig)) { - $this->validateConnectionNames(array_keys($enableStaticConnectionsConfig), $connectionNames); - } - - $enabledConnections = []; - - foreach ($connectionNames as $name) { - if ($enableStaticConnectionsConfig === true - || isset($enableStaticConnectionsConfig[$name]) && $enableStaticConnectionsConfig[$name] === true - ) { - $enabledConnections[] = $name; - } - } - - return $enabledConnections; - } - /** * @param string[] $configNames * @param string[] $existingNames diff --git a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php b/tests/DAMA/DoctrineTestBundle/DependencyInjection/CompilerPassesTest.php similarity index 84% rename from tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php rename to tests/DAMA/DoctrineTestBundle/DependencyInjection/CompilerPassesTest.php index 502692a..ff42b8b 100644 --- a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php +++ b/tests/DAMA/DoctrineTestBundle/DependencyInjection/CompilerPassesTest.php @@ -1,9 +1,12 @@ setDefinition( - 'doctrine.dbal.a_connection.configuration', - (new Definition(Configuration::class)) - ->setMethodCalls([['setMiddlewares', [[new Reference('foo')]]]]) - ); $containerBuilder->setDefinition('doctrine.dbal.b_connection.configuration', new Definition(Configuration::class)); $containerBuilder->setDefinition('doctrine.dbal.c_connection.configuration', new Definition(Configuration::class)); @@ -69,7 +66,8 @@ public function testProcess(array $config, callable $assertCallback, ?callable $ $expectationCallback($this, $containerBuilder); } - (new DoctrineTestCompilerPass())->process($containerBuilder); + (new AddMiddlewaresCompilerPass())->process($containerBuilder); + (new ModifyDoctrineConfigCompilerPass())->process($containerBuilder); foreach (array_keys($containerBuilder->getParameterBag()->all()) as $parameterName) { $this->assertStringStartsNotWith('dama.', $parameterName); @@ -97,35 +95,6 @@ function (ContainerBuilder $containerBuilder): void { self::assertSame([ 'dama.connection_key' => 'a', ], $containerBuilder->getDefinition('doctrine.dbal.a_connection')->getArgument(0)); - - self::assertEquals( - [ - [ - 'setMiddlewares', - [ - [ - new Reference('dama.doctrine.dbal.middleware'), - new Reference('foo'), - ], - ], - ], - ], - $containerBuilder->getDefinition('doctrine.dbal.a_connection.configuration')->getMethodCalls() - ); - - self::assertEquals( - [ - [ - 'setMiddlewares', - [ - [ - new Reference('dama.doctrine.dbal.middleware'), - ], - ], - ], - ], - $containerBuilder->getDefinition('doctrine.dbal.b_connection.configuration')->getMethodCalls() - ); }, ]; @@ -137,20 +106,6 @@ function (ContainerBuilder $containerBuilder): void { ], function (ContainerBuilder $containerBuilder): void { self::assertFalse($containerBuilder->hasDefinition('doctrine.orm.a_metadata_cache')); - - self::assertEquals( - [ - [ - 'setMiddlewares', - [ - [ - new Reference('foo'), - ], - ], - ], - ], - $containerBuilder->getDefinition('doctrine.dbal.a_connection.configuration')->getMethodCalls() - ); }, ]; @@ -167,8 +122,14 @@ function (ContainerBuilder $containerBuilder): void { self::assertSame([ 'dama.connection_key' => 'a', ], $containerBuilder->getDefinition('doctrine.dbal.a_connection')->getArgument(0)); + self::assertTrue($containerBuilder->hasDefinition('dama.doctrine.dbal.middleware.a')); + self::assertSame([ + 'connection' => 'a', + 'priority' => 100, + ], $containerBuilder->getDefinition('dama.doctrine.dbal.middleware.a')->getTag('doctrine.middleware')[0]); self::assertSame([], $containerBuilder->getDefinition('doctrine.dbal.b_connection')->getArgument(0)); + self::assertFalse($containerBuilder->hasDefinition('dama.doctrine.dbal.middleware.b')); self::assertSame( [ @@ -187,6 +148,7 @@ function (ContainerBuilder $containerBuilder): void { ], $containerBuilder->getDefinition('doctrine.dbal.c_connection')->getArgument(0) ); + self::assertTrue($containerBuilder->hasDefinition('dama.doctrine.dbal.middleware.c')); }, ];