diff --git a/pkg/enqueue-bundle/EnqueueBundle.php b/pkg/enqueue-bundle/EnqueueBundle.php index 6db17e15d..195013578 100644 --- a/pkg/enqueue-bundle/EnqueueBundle.php +++ b/pkg/enqueue-bundle/EnqueueBundle.php @@ -5,6 +5,7 @@ use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventsPass; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncTransformersPass; +use Enqueue\Doctrine\DoctrineSchemaCompilerPass; use Enqueue\Symfony\Client\DependencyInjection\AnalyzeRouteCollectionPass; use Enqueue\Symfony\Client\DependencyInjection\BuildClientExtensionsPass; use Enqueue\Symfony\Client\DependencyInjection\BuildCommandSubscriberRoutesPass as BuildClientCommandSubscriberRoutesPass; @@ -39,5 +40,7 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); $container->addCompilerPass(new AsyncTransformersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); } + + $container->addCompilerPass(new DoctrineSchemaCompilerPass()); } } diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index 978da5d18..eab956cc4 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -14,6 +14,14 @@ framework: router: { resource: '%kernel.root_dir%/config/routing.yml' } default_locale: '%locale%' +doctrine: + dbal: + connections: + custom: + url: "%env(DOCTRINE_DSN)%" + driver: pdo_mysql + charset: UTF8 + services: test_enqueue.client.default.driver: alias: 'enqueue.client.default.driver' diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 163527175..ee464080f 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -136,6 +136,13 @@ public function provideEnqueueConfigs() 'transport' => getenv('MONGO_DSN'), ], ]]; + + yield 'doctrine' => [[ + 'default' => [ + 'transport' => 'doctrine://custom', + ], + ]]; + // // yield 'gps' => [[ // 'transport' => [ diff --git a/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php new file mode 100644 index 000000000..db8f0dd76 --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php @@ -0,0 +1,54 @@ +doctrine = $doctrine; + $this->fallbackFactory = $fallbackFactory; + } + + public function create($config): ConnectionFactory + { + if (is_string($config)) { + $config = ['dsn' => $config]; + } + + if (false == is_array($config)) { + throw new \InvalidArgumentException('The config must be either array or DSN string.'); + } + + if (false == array_key_exists('dsn', $config)) { + throw new \InvalidArgumentException('The config must have dsn key set.'); + } + + $dsn = Dsn::parseFirst($config['dsn']); + + if ('doctrine' === $dsn->getScheme()) { + $config = $dsn->getQuery(); + $config['connection_name'] = $dsn->getHost(); + + return new ManagerRegistryConnectionFactory($this->doctrine, $config); + } + + return $this->fallbackFactory->create($config); + } +} diff --git a/pkg/enqueue/Doctrine/DoctrineDriverFactory.php b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php new file mode 100644 index 000000000..aab6489aa --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php @@ -0,0 +1,41 @@ +fallbackFactory = $fallbackFactory; + } + + public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface + { + $dsn = $config->getTransportOption('dsn'); + + if (empty($dsn)) { + throw new \LogicException('This driver factory relies on dsn option from transport config. The option is empty or not set.'); + } + + $dsn = Dsn::parseFirst($dsn); + + if ('doctrine' === $dsn->getScheme()) { + return new DbalDriver($factory->createContext(), $config, $collection); + } + + return $this->fallbackFactory->create($factory, $config, $collection); + } +} diff --git a/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php new file mode 100644 index 000000000..25016a761 --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php @@ -0,0 +1,39 @@ +hasDefinition('doctrine')) { + return; + } + + foreach ($container->getParameter('enqueue.transports') as $name) { + $diUtils = DiUtils::create(TransportFactory::MODULE, $name); + + $container->register($diUtils->format('connection_factory_factory.outer'), DoctrineConnectionFactoryFactory::class) + ->setDecoratedService($diUtils->format('connection_factory_factory'), $diUtils->format('connection_factory_factory.inner')) + ->addArgument(new Reference('doctrine')) + ->addArgument(new Reference($diUtils->format('connection_factory_factory.inner'))) + ; + } + + foreach ($container->getParameter('enqueue.clients') as $name) { + $diUtils = DiUtils::create(ClientFactory::MODULE, $name); + + $container->register($diUtils->format('driver_factory.outer'), DoctrineDriverFactory::class) + ->setDecoratedService($diUtils->format('driver_factory'), $diUtils->format('driver_factory.inner')) + ->addArgument(new Reference($diUtils->format('driver_factory.inner'))) + ; + } + } +}