From 80beadc971c2a04a9dc7e79d296ae91e1fdece2e Mon Sep 17 00:00:00 2001 From: Alexander Kozienko Date: Mon, 21 Jan 2019 11:08:30 +0200 Subject: [PATCH 1/3] doctrine schema --- .../DoctrineConnectionFactoryFactory.php | 54 +++++++++++++++++++ .../DoctrineSchema/DoctrineDriverFactory.php | 41 ++++++++++++++ .../DoctrineSchemaCompilerPass.php | 39 ++++++++++++++ pkg/enqueue-bundle/EnqueueBundle.php | 3 ++ .../Functional/App/config/custom-config.yml | 8 +++ .../Tests/Functional/UseCasesTest.php | 7 +++ 6 files changed, 152 insertions(+) create mode 100644 pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php create mode 100644 pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php create mode 100644 pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php b/pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php new file mode 100644 index 000000000..43b2aeab4 --- /dev/null +++ b/pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php @@ -0,0 +1,54 @@ +doctrine = $doctrine; + $this->parentFactory = $parentFactory; + } + + 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 ($dsn->getScheme() === 'doctrine') { + $config = $dsn->getQuery(); + $config['connection_name'] = $dsn->getHost(); + + return new ManagerRegistryConnectionFactory($this->doctrine, $config); + } + + return $this->parentFactory->create($config); + } +} diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php b/pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php new file mode 100644 index 000000000..0fce50038 --- /dev/null +++ b/pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php @@ -0,0 +1,41 @@ +parentFactory = $parentFactory; + } + + 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 ($dsn->getScheme() === 'doctrine') { + return new DbalDriver($factory->createContext(), $config, $collection); + } + + return $this->parentFactory->create($factory, $config, $collection); + } +} diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php b/pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php new file mode 100644 index 000000000..221d6392a --- /dev/null +++ b/pkg/enqueue-bundle/DoctrineSchema/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'))) + ; + } + } +} diff --git a/pkg/enqueue-bundle/EnqueueBundle.php b/pkg/enqueue-bundle/EnqueueBundle.php index 6db17e15d..042a76c0d 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\Bundle\DoctrineSchema\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' => [ From d09cf84f01b0d3c0eca9bb78ced5e4d688acd62a Mon Sep 17 00:00:00 2001 From: Alexander Kozienko Date: Mon, 21 Jan 2019 12:03:01 +0200 Subject: [PATCH 2/3] doctrine schema --- pkg/enqueue-bundle/EnqueueBundle.php | 2 +- .../Doctrine}/DoctrineConnectionFactoryFactory.php | 10 +++++----- .../Doctrine}/DoctrineDriverFactory.php | 10 +++++----- .../Doctrine}/DoctrineSchemaCompilerPass.php | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) rename pkg/{enqueue-bundle/DoctrineSchema => enqueue/Doctrine}/DoctrineConnectionFactoryFactory.php (85%) rename pkg/{enqueue-bundle/DoctrineSchema => enqueue/Doctrine}/DoctrineDriverFactory.php (77%) rename pkg/{enqueue-bundle/DoctrineSchema => enqueue/Doctrine}/DoctrineSchemaCompilerPass.php (97%) diff --git a/pkg/enqueue-bundle/EnqueueBundle.php b/pkg/enqueue-bundle/EnqueueBundle.php index 042a76c0d..195013578 100644 --- a/pkg/enqueue-bundle/EnqueueBundle.php +++ b/pkg/enqueue-bundle/EnqueueBundle.php @@ -5,7 +5,7 @@ use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventsPass; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncTransformersPass; -use Enqueue\Bundle\DoctrineSchema\DoctrineSchemaCompilerPass; +use Enqueue\Doctrine\DoctrineSchemaCompilerPass; use Enqueue\Symfony\Client\DependencyInjection\AnalyzeRouteCollectionPass; use Enqueue\Symfony\Client\DependencyInjection\BuildClientExtensionsPass; use Enqueue\Symfony\Client\DependencyInjection\BuildCommandSubscriberRoutesPass as BuildClientCommandSubscriberRoutesPass; diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php similarity index 85% rename from pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php rename to pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php index 43b2aeab4..14f39f9da 100644 --- a/pkg/enqueue-bundle/DoctrineSchema/DoctrineConnectionFactoryFactory.php +++ b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php @@ -1,6 +1,6 @@ doctrine = $doctrine; - $this->parentFactory = $parentFactory; + $this->fallbackFactory = $fallbackFactory; } public function create($config): ConnectionFactory @@ -49,6 +49,6 @@ public function create($config): ConnectionFactory return new ManagerRegistryConnectionFactory($this->doctrine, $config); } - return $this->parentFactory->create($config); + return $this->fallbackFactory->create($config); } } diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php similarity index 77% rename from pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php rename to pkg/enqueue/Doctrine/DoctrineDriverFactory.php index 0fce50038..a1c977cdc 100644 --- a/pkg/enqueue-bundle/DoctrineSchema/DoctrineDriverFactory.php +++ b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php @@ -1,6 +1,6 @@ parentFactory = $parentFactory; + $this->fallbackFactory = $fallbackFactory; } public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface @@ -36,6 +36,6 @@ public function create(ConnectionFactory $factory, Config $config, RouteCollecti return new DbalDriver($factory->createContext(), $config, $collection); } - return $this->parentFactory->create($factory, $config, $collection); + return $this->fallbackFactory->create($factory, $config, $collection); } } diff --git a/pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php similarity index 97% rename from pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php rename to pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php index 221d6392a..25016a761 100644 --- a/pkg/enqueue-bundle/DoctrineSchema/DoctrineSchemaCompilerPass.php +++ b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php @@ -1,6 +1,6 @@ Date: Mon, 21 Jan 2019 12:15:40 +0200 Subject: [PATCH 3/3] doctrine schema --- pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php | 2 +- pkg/enqueue/Doctrine/DoctrineDriverFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php index 14f39f9da..db8f0dd76 100644 --- a/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php +++ b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php @@ -42,7 +42,7 @@ public function create($config): ConnectionFactory $dsn = Dsn::parseFirst($config['dsn']); - if ($dsn->getScheme() === 'doctrine') { + if ('doctrine' === $dsn->getScheme()) { $config = $dsn->getQuery(); $config['connection_name'] = $dsn->getHost(); diff --git a/pkg/enqueue/Doctrine/DoctrineDriverFactory.php b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php index a1c977cdc..aab6489aa 100644 --- a/pkg/enqueue/Doctrine/DoctrineDriverFactory.php +++ b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php @@ -32,7 +32,7 @@ public function create(ConnectionFactory $factory, Config $config, RouteCollecti $dsn = Dsn::parseFirst($dsn); - if ($dsn->getScheme() === 'doctrine') { + if ('doctrine' === $dsn->getScheme()) { return new DbalDriver($factory->createContext(), $config, $collection); }