Skip to content

Commit 8096f29

Browse files
authored
Allow you to skip the factory keyword. (#170)
* Allow you to skip the factory keyword. * Updated exception message
1 parent ff80f90 commit 8096f29

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Discovery\HttpClientDiscovery;
6+
7+
/**
8+
* Use auto discovery to find a HTTP client.
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class AutoDiscoveryFactory implements ClientFactory
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function createClient(array $config = [])
18+
{
19+
return HttpClientDiscovery::find();
20+
}
21+
}

DependencyInjection/Configuration.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,15 @@ private function configureClients(ArrayNodeDefinition $root)
173173
})
174174
->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")')
175175
->end()
176+
->validate()
177+
->ifTrue(function ($config) {
178+
return $config['factory'] === 'httplug.factory.auto' && !empty($config['config']);
179+
})
180+
->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".')
181+
->end()
176182
->children()
177183
->scalarNode('factory')
178-
->isRequired()
184+
->defaultValue('httplug.factory.auto')
179185
->cannotBeEmpty()
180186
->info('The service id of a factory to use when creating the adapter.')
181187
->end()

Resources/config/data-collector.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
</service>
2828

2929
<!-- ClientFactories -->
30+
<service id="httplug.collector.factory.auto" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.auto" public="false">
31+
<argument type="service" id="httplug.collector.factory.auto.inner"/>
32+
<argument type="service" id="httplug.collector.collector"/>
33+
<argument type="service" id="httplug.collector.formatter"/>
34+
<argument type="service" id="debug.stopwatch"/>
35+
</service>
3036
<service id="httplug.collector.factory.buzz" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.buzz" public="false">
3137
<argument type="service" id="httplug.collector.factory.buzz.inner"/>
3238
<argument type="service" id="httplug.collector.collector"/>

Resources/config/services.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<service id="Http\Client\HttpClient" alias="httplug.client" public="false" />
1313

1414
<!-- ClientFactories -->
15+
<service id="httplug.factory.auto" class="Http\HttplugBundle\ClientFactory\AutoDiscoveryFactory" public="false">
16+
<argument type="service" id="httplug.message_factory"/>
17+
</service>
1518
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
1619
<argument type="service" id="httplug.message_factory"/>
1720
</service>

0 commit comments

Comments
 (0)