Skip to content

Commit 2baccf2

Browse files
authored
Merge pull request #732 from php-enqueue/doctrine-chema
[bundle][dbal] Use doctrine bundle configured connections
2 parents 516acfa + 62da38d commit 2baccf2

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

pkg/enqueue-bundle/EnqueueBundle.php

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension;
66
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventsPass;
77
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncTransformersPass;
8+
use Enqueue\Doctrine\DoctrineSchemaCompilerPass;
89
use Enqueue\Symfony\Client\DependencyInjection\AnalyzeRouteCollectionPass;
910
use Enqueue\Symfony\Client\DependencyInjection\BuildClientExtensionsPass;
1011
use Enqueue\Symfony\Client\DependencyInjection\BuildCommandSubscriberRoutesPass as BuildClientCommandSubscriberRoutesPass;
@@ -39,5 +40,7 @@ public function build(ContainerBuilder $container): void
3940
$container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
4041
$container->addCompilerPass(new AsyncTransformersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
4142
}
43+
44+
$container->addCompilerPass(new DoctrineSchemaCompilerPass());
4245
}
4346
}

pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ framework:
1414
router: { resource: '%kernel.root_dir%/config/routing.yml' }
1515
default_locale: '%locale%'
1616

17+
doctrine:
18+
dbal:
19+
connections:
20+
custom:
21+
url: "%env(DOCTRINE_DSN)%"
22+
driver: pdo_mysql
23+
charset: UTF8
24+
1725
services:
1826
test_enqueue.client.default.driver:
1927
alias: 'enqueue.client.default.driver'

pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ public function provideEnqueueConfigs()
136136
'transport' => getenv('MONGO_DSN'),
137137
],
138138
]];
139+
140+
yield 'doctrine' => [[
141+
'default' => [
142+
'transport' => 'doctrine://custom',
143+
],
144+
]];
145+
139146
//
140147
// yield 'gps' => [[
141148
// 'transport' => [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Enqueue\Doctrine;
4+
5+
use Enqueue\ConnectionFactoryFactoryInterface;
6+
use Enqueue\Dbal\ManagerRegistryConnectionFactory;
7+
use Enqueue\Dsn\Dsn;
8+
use Interop\Queue\ConnectionFactory;
9+
use Symfony\Bridge\Doctrine\RegistryInterface;
10+
11+
class DoctrineConnectionFactoryFactory implements ConnectionFactoryFactoryInterface
12+
{
13+
/**
14+
* @var RegistryInterface
15+
*/
16+
private $doctrine;
17+
18+
/**
19+
* @var ConnectionFactoryFactoryInterface
20+
*/
21+
private $fallbackFactory;
22+
23+
public function __construct(RegistryInterface $doctrine, ConnectionFactoryFactoryInterface $fallbackFactory)
24+
{
25+
$this->doctrine = $doctrine;
26+
$this->fallbackFactory = $fallbackFactory;
27+
}
28+
29+
public function create($config): ConnectionFactory
30+
{
31+
if (is_string($config)) {
32+
$config = ['dsn' => $config];
33+
}
34+
35+
if (false == is_array($config)) {
36+
throw new \InvalidArgumentException('The config must be either array or DSN string.');
37+
}
38+
39+
if (false == array_key_exists('dsn', $config)) {
40+
throw new \InvalidArgumentException('The config must have dsn key set.');
41+
}
42+
43+
$dsn = Dsn::parseFirst($config['dsn']);
44+
45+
if ('doctrine' === $dsn->getScheme()) {
46+
$config = $dsn->getQuery();
47+
$config['connection_name'] = $dsn->getHost();
48+
49+
return new ManagerRegistryConnectionFactory($this->doctrine, $config);
50+
}
51+
52+
return $this->fallbackFactory->create($config);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Enqueue\Doctrine;
4+
5+
use Enqueue\Client\Config;
6+
use Enqueue\Client\Driver\DbalDriver;
7+
use Enqueue\Client\DriverFactoryInterface;
8+
use Enqueue\Client\DriverInterface;
9+
use Enqueue\Client\RouteCollection;
10+
use Enqueue\Dsn\Dsn;
11+
use Interop\Queue\ConnectionFactory;
12+
13+
class DoctrineDriverFactory implements DriverFactoryInterface
14+
{
15+
/**
16+
* @var DriverFactoryInterface
17+
*/
18+
private $fallbackFactory;
19+
20+
public function __construct(DriverFactoryInterface $fallbackFactory)
21+
{
22+
$this->fallbackFactory = $fallbackFactory;
23+
}
24+
25+
public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface
26+
{
27+
$dsn = $config->getTransportOption('dsn');
28+
29+
if (empty($dsn)) {
30+
throw new \LogicException('This driver factory relies on dsn option from transport config. The option is empty or not set.');
31+
}
32+
33+
$dsn = Dsn::parseFirst($dsn);
34+
35+
if ('doctrine' === $dsn->getScheme()) {
36+
return new DbalDriver($factory->createContext(), $config, $collection);
37+
}
38+
39+
return $this->fallbackFactory->create($factory, $config, $collection);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Enqueue\Doctrine;
4+
5+
use Enqueue\Symfony\Client\DependencyInjection\ClientFactory;
6+
use Enqueue\Symfony\DependencyInjection\TransportFactory;
7+
use Enqueue\Symfony\DiUtils;
8+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Reference;
11+
12+
class DoctrineSchemaCompilerPass implements CompilerPassInterface
13+
{
14+
public function process(ContainerBuilder $container)
15+
{
16+
if (false === $container->hasDefinition('doctrine')) {
17+
return;
18+
}
19+
20+
foreach ($container->getParameter('enqueue.transports') as $name) {
21+
$diUtils = DiUtils::create(TransportFactory::MODULE, $name);
22+
23+
$container->register($diUtils->format('connection_factory_factory.outer'), DoctrineConnectionFactoryFactory::class)
24+
->setDecoratedService($diUtils->format('connection_factory_factory'), $diUtils->format('connection_factory_factory.inner'))
25+
->addArgument(new Reference('doctrine'))
26+
->addArgument(new Reference($diUtils->format('connection_factory_factory.inner')))
27+
;
28+
}
29+
30+
foreach ($container->getParameter('enqueue.clients') as $name) {
31+
$diUtils = DiUtils::create(ClientFactory::MODULE, $name);
32+
33+
$container->register($diUtils->format('driver_factory.outer'), DoctrineDriverFactory::class)
34+
->setDecoratedService($diUtils->format('driver_factory'), $diUtils->format('driver_factory.inner'))
35+
->addArgument(new Reference($diUtils->format('driver_factory.inner')))
36+
;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)