Skip to content

Commit d6cd4c4

Browse files
committed
[symfony] Add ability to customize connection factory factory.
1 parent a5e7803 commit d6cd4c4

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎pkg/enqueue/Symfony/DependencyInjection/TransportFactory.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function addConfiguration(ArrayNodeDefinition $builder): void
3939
}
4040

4141
if (is_array($v)) {
42+
if (isset($v['factory_class']) && isset($v['factory_service'])) {
43+
throw new \LogicException('Both options factory_class and factory_service are set. Please choose one.');
44+
}
45+
4246
return $v;
4347
}
4448

@@ -52,16 +56,31 @@ public function addConfiguration(ArrayNodeDefinition $builder): void
5256
->ignoreExtraKeys(false)
5357
->children()
5458
->scalarNode('dsn')->cannotBeEmpty()->isRequired()->end()
59+
->scalarNode('factory_service')->end()
60+
->scalarNode('factory_class')->end()
5561
->end()
5662
;
5763
}
5864

5965
public function createConnectionFactory(ContainerBuilder $container, array $config): string
6066
{
67+
$factoryFactoryId = 'enqueue.connection_factory_factory';
6168
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
6269

70+
if (array_key_exists('factory_class', $config)) {
71+
$factoryFactoryId = sprintf('enqueue.transport.%s.connection_factory_factory', $this->getName());
72+
73+
$container->register($factoryFactoryId, $config['factory_class']);
74+
}
75+
76+
$factoryFactoryService = new Reference(
77+
array_key_exists('factory_service', $config) ? $config['factory_service'] : $factoryFactoryId
78+
);
79+
80+
unset($config['factory_service'], $config['factory_class']);
81+
6382
$container->register($factoryId, PsrConnectionFactory::class)
64-
->setFactory([new Reference('enqueue.connection_factory_factory'), 'create'])
83+
->setFactory([$factoryFactoryService, 'create'])
6584
->addArgument($config)
6685
;
6786

‎pkg/enqueue/Tests/Symfony/DependencyInjection/TransportFactoryTest.php‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,60 @@ public function testThrowIfEmptyDsnGiven()
119119
$processor->process($tb->buildTree(), [['dsn' => '']]);
120120
}
121121

122+
public function testThrowIfFactoryClassAndFactoryServiceSetAtTheSameTime()
123+
{
124+
$transport = new TransportFactory('default');
125+
$tb = new TreeBuilder();
126+
$rootNode = $tb->root('foo');
127+
128+
$transport->addConfiguration($rootNode);
129+
$processor = new Processor();
130+
131+
$this->expectException(\LogicException::class);
132+
$this->expectExceptionMessage('Both options factory_class and factory_service are set. Please choose one.');
133+
$processor->process($tb->buildTree(), [[
134+
'dsn' => 'foo:',
135+
'factory_class' => 'aFactoryClass',
136+
'factory_service' => 'aFactoryService',
137+
]]);
138+
}
139+
140+
public function testShouldAllowSetFactoryClass()
141+
{
142+
$transport = new TransportFactory('default');
143+
$tb = new TreeBuilder();
144+
$rootNode = $tb->root('foo');
145+
146+
$transport->addConfiguration($rootNode);
147+
$processor = new Processor();
148+
149+
$config = $processor->process($tb->buildTree(), [[
150+
'dsn' => 'foo:',
151+
'factory_class' => 'theFactoryClass',
152+
]]);
153+
154+
$this->assertArrayHasKey('factory_class', $config);
155+
$this->assertSame('theFactoryClass', $config['factory_class']);
156+
}
157+
158+
public function testShouldAllowSetFactoryService()
159+
{
160+
$transport = new TransportFactory('default');
161+
$tb = new TreeBuilder();
162+
$rootNode = $tb->root('foo');
163+
164+
$transport->addConfiguration($rootNode);
165+
$processor = new Processor();
166+
167+
$config = $processor->process($tb->buildTree(), [[
168+
'dsn' => 'foo:',
169+
'factory_service' => 'theFactoryService',
170+
]]);
171+
172+
$this->assertArrayHasKey('factory_service', $config);
173+
$this->assertSame('theFactoryService', $config['factory_service']);
174+
}
175+
122176
public function testThrowIfExtraOptionGiven()
123177
{
124178
$transport = new TransportFactory('default');
@@ -164,6 +218,70 @@ public function testShouldCreateConnectionFactoryFromDSN()
164218
);
165219
}
166220

221+
public function testShouldCreateConnectionFactoryUsingCustomFactortyClass()
222+
{
223+
$container = new ContainerBuilder();
224+
225+
$transport = new TransportFactory('default');
226+
227+
$serviceId = $transport->createConnectionFactory($container, ['dsn' => 'foo:', 'factory_class' => 'theFactoryClass']);
228+
229+
$this->assertEquals('enqueue.transport.default.connection_factory', $serviceId);
230+
231+
$this->assertTrue($container->hasDefinition('enqueue.transport.default.connection_factory_factory'));
232+
$this->assertSame(
233+
'theFactoryClass',
234+
$container->getDefinition('enqueue.transport.default.connection_factory_factory')->getClass()
235+
);
236+
237+
$this->assertTrue($container->hasDefinition('enqueue.transport.default.connection_factory'));
238+
239+
$this->assertNotEmpty($container->getDefinition('enqueue.transport.default.connection_factory')->getFactory());
240+
$this->assertEquals(
241+
[new Reference('enqueue.transport.default.connection_factory_factory'), 'create'],
242+
$container->getDefinition('enqueue.transport.default.connection_factory')->getFactory())
243+
;
244+
$this->assertSame(
245+
[['dsn' => 'foo:']],
246+
$container->getDefinition('enqueue.transport.default.connection_factory')->getArguments())
247+
;
248+
249+
$this->assertTrue($container->hasAlias('enqueue.transport.connection_factory'));
250+
$this->assertEquals(
251+
'enqueue.transport.default.connection_factory',
252+
(string) $container->getAlias('enqueue.transport.connection_factory')
253+
);
254+
}
255+
256+
public function testShouldCreateConnectionFactoryUsingCustomFactortyService()
257+
{
258+
$container = new ContainerBuilder();
259+
260+
$transport = new TransportFactory('default');
261+
262+
$serviceId = $transport->createConnectionFactory($container, ['dsn' => 'foo:', 'factory_service' => 'theFactoryService']);
263+
264+
$this->assertEquals('enqueue.transport.default.connection_factory', $serviceId);
265+
266+
$this->assertTrue($container->hasDefinition('enqueue.transport.default.connection_factory'));
267+
268+
$this->assertNotEmpty($container->getDefinition('enqueue.transport.default.connection_factory')->getFactory());
269+
$this->assertEquals(
270+
[new Reference('theFactoryService'), 'create'],
271+
$container->getDefinition('enqueue.transport.default.connection_factory')->getFactory())
272+
;
273+
$this->assertSame(
274+
[['dsn' => 'foo:']],
275+
$container->getDefinition('enqueue.transport.default.connection_factory')->getArguments())
276+
;
277+
278+
$this->assertTrue($container->hasAlias('enqueue.transport.connection_factory'));
279+
$this->assertEquals(
280+
'enqueue.transport.default.connection_factory',
281+
(string) $container->getAlias('enqueue.transport.connection_factory')
282+
);
283+
}
284+
167285
public function testShouldCreateContextFromDsn()
168286
{
169287
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)