Skip to content

Commit 263949a

Browse files
committed
multi client configuration
1 parent bbc342f commit 263949a

5 files changed

Lines changed: 101 additions & 89 deletions

File tree

‎pkg/enqueue-bundle/DependencyInjection/Configuration.php‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,15 @@ public function getConfigTreeBuilder(): TreeBuilder
4848
})
4949
;
5050

51-
$transportFactory = new TransportFactory('default');
52-
53-
$transportConfig = $transportFactory->getConfiguration('transport');
54-
$transportConfig->isRequired();
55-
56-
$consumerConfig = $transportFactory->getQueueConsumerConfiguration('consumption');
57-
58-
$clientConfig = (new ClientFactory('default'))->getConfiguration('client', $this->debug);
59-
60-
$monitoringConfig = (new MonitoringFactory('default'))->getConfiguration('monitoring');
61-
6251
$rootNode
6352
->requiresAtLeastOneElement()
6453
->useAttributeAsKey('key')
6554
->arrayPrototype()
6655
->children()
67-
->append($transportConfig)
68-
->append($consumerConfig)
69-
->append($clientConfig)
70-
->append($monitoringConfig)
56+
->append(TransportFactory::getConfiguration())
57+
->append(TransportFactory::getQueueConsumerConfiguration())
58+
->append(ClientFactory::getConfiguration($this->debug))
59+
->append(MonitoringFactory::getConfiguration())
7160
->arrayNode('extensions')->addDefaultsIfNotSet()->children()
7261
->booleanNode('doctrine_ping_connection_extension')->defaultFalse()->end()
7362
->booleanNode('doctrine_clear_identity_map_extension')->defaultFalse()->end()

‎pkg/enqueue/Symfony/Client/DependencyInjection/ClientFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(string $name)
5050
$this->name = $name;
5151
}
5252

53-
public function getConfiguration(string $name, bool $debug): NodeDefinition
53+
public static function getConfiguration(bool $debug, string $name = 'client'): NodeDefinition
5454
{
5555
$builder = new ArrayNodeDefinition($name);
5656

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(string $name)
4141
$this->name = $name;
4242
}
4343

44-
public function getConfiguration(string $name): NodeDefinition
44+
public static function getConfiguration(string $name = 'transport'): NodeDefinition
4545
{
4646
$knownSchemes = array_keys(Resources::getKnownSchemes());
4747
$availableSchemes = array_keys(Resources::getAvailableSchemes());
@@ -74,6 +74,7 @@ public function getConfiguration(string $name): NodeDefinition
7474
throw new \LogicException(sprintf('The value must be array, null or string. Got "%s"', gettype($v)));
7575
})
7676
->end()
77+
->isRequired()
7778
->ignoreExtraKeys(false)
7879
->children()
7980
->scalarNode('dsn')
@@ -100,7 +101,7 @@ public function getConfiguration(string $name): NodeDefinition
100101
return $builder;
101102
}
102103

103-
public function getQueueConsumerConfiguration(string $name): ArrayNodeDefinition
104+
public static function getQueueConsumerConfiguration(string $name = 'consumption'): ArrayNodeDefinition
104105
{
105106
$builder = new ArrayNodeDefinition($name);
106107

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

Lines changed: 92 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ public function testThrowIfEmptyNameGivenOnConstruction()
4444

4545
public function testShouldAllowAddConfigurationAsStringDsn()
4646
{
47-
$transport = new TransportFactory('default');
4847
$tb = new TreeBuilder();
4948
$rootNode = $tb->root('foo');
5049

51-
$transport->addTransportConfiguration($rootNode);
50+
$rootNode->append(TransportFactory::getConfiguration());
51+
5252
$processor = new Processor();
53-
$config = $processor->process($tb->buildTree(), ['dsn://']);
53+
$config = $processor->process($tb->buildTree(), [['transport' => 'dsn://']]);
5454

55-
$this->assertEquals(['dsn' => 'dsn://'], $config);
55+
$this->assertEquals([
56+
'transport' => [
57+
'dsn' => 'dsn://',
58+
],
59+
], $config);
5660
}
5761

5862
/**
@@ -62,191 +66,209 @@ public function testShouldAllowAddConfigurationAsStringDsn()
6266
*/
6367
public function testShouldAllowAddConfigurationAsDsnWithoutSlashes()
6468
{
65-
$transport = new TransportFactory('default');
6669
$tb = new TreeBuilder();
6770
$rootNode = $tb->root('foo');
6871

69-
$transport->addTransportConfiguration($rootNode);
72+
$rootNode->append(TransportFactory::getConfiguration());
73+
7074
$processor = new Processor();
71-
$config = $processor->process($tb->buildTree(), ['dsn:']);
75+
$config = $processor->process($tb->buildTree(), [['transport' => 'dsn:']]);
7276

73-
$this->assertEquals(['dsn' => 'dsn:'], $config);
77+
$this->assertEquals([
78+
'transport' => [
79+
'dsn' => 'dsn:',
80+
],
81+
], $config);
7482
}
7583

7684
public function testShouldSetNullTransportIfNullGiven()
7785
{
78-
$transport = new TransportFactory('default');
7986
$tb = new TreeBuilder();
8087
$rootNode = $tb->root('foo');
8188

82-
$transport->addTransportConfiguration($rootNode);
89+
$rootNode->append(TransportFactory::getConfiguration());
90+
8391
$processor = new Processor();
92+
$config = $processor->process($tb->buildTree(), [['transport' => null]]);
8493

85-
$config = $processor->process($tb->buildTree(), [null]);
86-
$this->assertEquals(['dsn' => 'null:'], $config);
94+
$this->assertEquals([
95+
'transport' => [
96+
'dsn' => 'null:',
97+
],
98+
], $config);
8799
}
88100

89101
public function testShouldSetNullTransportIfEmptyStringGiven()
90102
{
91-
$transport = new TransportFactory('default');
92103
$tb = new TreeBuilder();
93104
$rootNode = $tb->root('foo');
94105

95-
$transport->addTransportConfiguration($rootNode);
106+
$rootNode->append(TransportFactory::getConfiguration());
107+
96108
$processor = new Processor();
109+
$config = $processor->process($tb->buildTree(), [['transport' => '']]);
97110

98-
$config = $processor->process($tb->buildTree(), ['']);
99-
$this->assertEquals(['dsn' => 'null:'], $config);
111+
$this->assertEquals([
112+
'transport' => [
113+
'dsn' => 'null:',
114+
],
115+
], $config);
100116
}
101117

102118
public function testShouldSetNullTransportIfEmptyArrayGiven()
103119
{
104-
$transport = new TransportFactory('default');
105120
$tb = new TreeBuilder();
106121
$rootNode = $tb->root('foo');
107122

108-
$transport->addTransportConfiguration($rootNode);
123+
$rootNode->append(TransportFactory::getConfiguration());
124+
109125
$processor = new Processor();
126+
$config = $processor->process($tb->buildTree(), [['transport' => []]]);
110127

111-
$config = $processor->process($tb->buildTree(), [[]]);
112-
$this->assertEquals(['dsn' => 'null:'], $config);
128+
$this->assertEquals([
129+
'transport' => [
130+
'dsn' => 'null:',
131+
],
132+
], $config);
113133
}
114134

115135
public function testThrowIfEmptyDsnGiven()
116136
{
117-
$transport = new TransportFactory('default');
118137
$tb = new TreeBuilder();
119138
$rootNode = $tb->root('foo');
120139

121-
$transport->addTransportConfiguration($rootNode);
122-
$processor = new Processor();
140+
$rootNode->append(TransportFactory::getConfiguration());
123141

142+
$processor = new Processor();
124143
$this->expectException(InvalidConfigurationException::class);
125-
$this->expectExceptionMessage('The path "foo.dsn" cannot contain an empty value, but got "".');
126-
$processor->process($tb->buildTree(), [['dsn' => '']]);
144+
$this->expectExceptionMessage('The path "foo.transport.dsn" cannot contain an empty value, but got "".');
145+
$processor->process($tb->buildTree(), [['transport' => ['dsn' => '']]]);
127146
}
128147

129148
public function testThrowIfFactoryClassAndFactoryServiceSetAtTheSameTime()
130149
{
131-
$transport = new TransportFactory('default');
132150
$tb = new TreeBuilder();
133151
$rootNode = $tb->root('foo');
134152

135-
$transport->addTransportConfiguration($rootNode);
153+
$rootNode->append(TransportFactory::getConfiguration());
154+
136155
$processor = new Processor();
137156

138157
$this->expectException(\LogicException::class);
139158
$this->expectExceptionMessage('Both options factory_class and factory_service are set. Please choose one.');
140159
$processor->process($tb->buildTree(), [[
141-
'dsn' => 'foo:',
142-
'factory_class' => 'aFactoryClass',
143-
'factory_service' => 'aFactoryService',
144-
]]);
160+
'transport' => [
161+
'dsn' => 'foo:',
162+
'factory_class' => 'aFactoryClass',
163+
'factory_service' => 'aFactoryService',
164+
]]]);
145165
}
146166

147167
public function testThrowIfConnectionFactoryClassUsedWithFactoryClassAtTheSameTime()
148168
{
149-
$transport = new TransportFactory('default');
150169
$tb = new TreeBuilder();
151170
$rootNode = $tb->root('foo');
152171

153-
$transport->addTransportConfiguration($rootNode);
172+
$rootNode->append(TransportFactory::getConfiguration());
173+
154174
$processor = new Processor();
155175

156176
$this->expectException(\LogicException::class);
157177
$this->expectExceptionMessage('The option connection_factory_class must not be used with factory_class or factory_service at the same time. Please choose one.');
158178
$processor->process($tb->buildTree(), [[
159-
'dsn' => 'foo:',
160-
'connection_factory_class' => 'aFactoryClass',
161-
'factory_service' => 'aFactoryService',
162-
]]);
179+
'transport' => [
180+
'dsn' => 'foo:',
181+
'connection_factory_class' => 'aFactoryClass',
182+
'factory_service' => 'aFactoryService',
183+
]]]);
163184
}
164185

165186
public function testThrowIfConnectionFactoryClassUsedWithFactoryServiceAtTheSameTime()
166187
{
167-
$transport = new TransportFactory('default');
168188
$tb = new TreeBuilder();
169189
$rootNode = $tb->root('foo');
170190

171-
$transport->addTransportConfiguration($rootNode);
191+
$rootNode->append(TransportFactory::getConfiguration());
172192
$processor = new Processor();
173193

174194
$this->expectException(\LogicException::class);
175195
$this->expectExceptionMessage('The option connection_factory_class must not be used with factory_class or factory_service at the same time. Please choose one.');
176196
$processor->process($tb->buildTree(), [[
177-
'dsn' => 'foo:',
178-
'connection_factory_class' => 'aFactoryClass',
179-
'factory_service' => 'aFactoryService',
180-
]]);
197+
'transport' => [
198+
'dsn' => 'foo:',
199+
'connection_factory_class' => 'aFactoryClass',
200+
'factory_service' => 'aFactoryService',
201+
]]]);
181202
}
182203

183204
public function testShouldAllowSetFactoryClass()
184205
{
185-
$transport = new TransportFactory('default');
186206
$tb = new TreeBuilder();
187207
$rootNode = $tb->root('foo');
188208

189-
$transport->addTransportConfiguration($rootNode);
209+
$rootNode->append(TransportFactory::getConfiguration());
190210
$processor = new Processor();
191211

192212
$config = $processor->process($tb->buildTree(), [[
193-
'dsn' => 'foo:',
194-
'factory_class' => 'theFactoryClass',
195-
]]);
213+
'transport' => [
214+
'dsn' => 'foo:',
215+
'factory_class' => 'theFactoryClass',
216+
]]]);
196217

197-
$this->assertArrayHasKey('factory_class', $config);
198-
$this->assertSame('theFactoryClass', $config['factory_class']);
218+
$this->assertArrayHasKey('factory_class', $config['transport']);
219+
$this->assertSame('theFactoryClass', $config['transport']['factory_class']);
199220
}
200221

201222
public function testShouldAllowSetFactoryService()
202223
{
203-
$transport = new TransportFactory('default');
204224
$tb = new TreeBuilder();
205225
$rootNode = $tb->root('foo');
206226

207-
$transport->addTransportConfiguration($rootNode);
227+
$rootNode->append(TransportFactory::getConfiguration());
208228
$processor = new Processor();
209229

210230
$config = $processor->process($tb->buildTree(), [[
211-
'dsn' => 'foo:',
212-
'factory_service' => 'theFactoryService',
213-
]]);
231+
'transport' => [
232+
'dsn' => 'foo:',
233+
'factory_service' => 'theFactoryService',
234+
]]]);
214235

215-
$this->assertArrayHasKey('factory_service', $config);
216-
$this->assertSame('theFactoryService', $config['factory_service']);
236+
$this->assertArrayHasKey('factory_service', $config['transport']);
237+
$this->assertSame('theFactoryService', $config['transport']['factory_service']);
217238
}
218239

219240
public function testShouldAllowSetConnectionFactoryClass()
220241
{
221-
$transport = new TransportFactory('default');
222242
$tb = new TreeBuilder();
223243
$rootNode = $tb->root('foo');
224244

225-
$transport->addTransportConfiguration($rootNode);
245+
$rootNode->append(TransportFactory::getConfiguration());
226246
$processor = new Processor();
227247

228248
$config = $processor->process($tb->buildTree(), [[
229-
'dsn' => 'foo:',
230-
'connection_factory_class' => 'theFactoryClass',
231-
]]);
249+
'transport' => [
250+
'dsn' => 'foo:',
251+
'connection_factory_class' => 'theFactoryClass',
252+
]]]);
232253

233-
$this->assertArrayHasKey('connection_factory_class', $config);
234-
$this->assertSame('theFactoryClass', $config['connection_factory_class']);
254+
$this->assertArrayHasKey('connection_factory_class', $config['transport']);
255+
$this->assertSame('theFactoryClass', $config['transport']['connection_factory_class']);
235256
}
236257

237258
public function testThrowIfExtraOptionGiven()
238259
{
239-
$transport = new TransportFactory('default');
240260
$tb = new TreeBuilder();
241261
$rootNode = $tb->root('foo');
242262

243-
$transport->addTransportConfiguration($rootNode);
263+
$rootNode->append(TransportFactory::getConfiguration());
244264
$processor = new Processor();
245265

246-
$config = $processor->process($tb->buildTree(), [['dsn' => 'foo:', 'extraOption' => 'aVal']]);
247-
$this->assertEquals(
248-
['dsn' => 'foo:', 'extraOption' => 'aVal'],
249-
$config
266+
$config = $processor->process($tb->buildTree(), [['transport' => ['dsn' => 'foo:', 'extraOption' => 'aVal']]]);
267+
$this->assertEquals([
268+
'transport' => [
269+
'dsn' => 'foo:',
270+
'extraOption' => 'aVal',
271+
]], $config
250272
);
251273
}
252274

‎pkg/monitoring/Symfony/DependencyInjection/MonitoringFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(string $name)
3232
$this->diUtils = DiUtils::create('monitoring', $name);
3333
}
3434

35-
public function getConfiguration(string $name): ArrayNodeDefinition
35+
public static function getConfiguration(string $name = 'monitoring'): ArrayNodeDefinition
3636
{
3737
$builder = new ArrayNodeDefinition($name);
3838

0 commit comments

Comments
 (0)