Skip to content

Commit ba20113

Browse files
committed
Add support for default_host and force_host options on each client
1 parent c2493b5 commit ba20113

File tree

7 files changed

+107
-5
lines changed

7 files changed

+107
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
### Added
77

8+
- Option to configure default host for each client. default_host to add host if missing, force_host to change all requests to a specific host.
89
- Support for BatchClient
9-
- The stopwatch plugin in included by default when using profiling.
10+
- The stopwatch plugin in included by default when using profiling.
1011

1112
### Changed
1213

DependencyInjection/Configuration.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function getConfigTreeBuilder()
9292
return $v;
9393
})
9494
->end()
95+
->fixXmlConfig('client')
9596
->children()
9697
->arrayNode('main_alias')
9798
->addDefaultsIfNotSet()
@@ -184,6 +185,22 @@ protected function configureClients(ArrayNodeDefinition $root)
184185
->defaultFalse()
185186
->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.')
186187
->end()
188+
->arrayNode('options')
189+
->validate()
190+
->ifTrue(function ($options) {
191+
return array_key_exists('default_host', $options) && array_key_exists('force_host', $options);
192+
})
193+
->thenInvalid('You can only set one of default_host and force_host for each client')
194+
->end()
195+
->children()
196+
->scalarNode('default_host')
197+
->info('Configure the AddHostPlugin for this client. Add host if request is only for a path.')
198+
->end()
199+
->scalarNode('force_host')
200+
->info('Configure the AddHostPlugin for this client. Send all requests to this host regardless of host in request.')
201+
->end()
202+
->end()
203+
->end()
187204
->arrayNode('plugins')
188205
->info('A list of service ids of plugins. The order is important.')
189206
->prototype('scalar')->end()
@@ -202,7 +219,7 @@ protected function configurePlugins(ArrayNodeDefinition $root)
202219
->arrayNode('plugins')
203220
->addDefaultsIfNotSet()
204221
->children()
205-
->append($this->addAuthenticationPluiginNode())
222+
->append($this->addAuthenticationPluginNode())
206223

207224
->arrayNode('cache')
208225
->canBeEnabled()
@@ -313,7 +330,7 @@ protected function configurePlugins(ArrayNodeDefinition $root)
313330
*
314331
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
315332
*/
316-
private function addAuthenticationPluiginNode()
333+
private function addAuthenticationPluginNode()
317334
{
318335
$builder = new TreeBuilder();
319336
$node = $builder->root('authentication');

DependencyInjection/HttplugExtension.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Common\BatchClient;
66
use Http\Client\Common\FlexibleHttpClient;
77
use Http\Client\Common\HttpMethodsClient;
8+
use Http\Client\Common\Plugin\AddHostPlugin;
89
use Http\Client\Common\Plugin\AuthenticationPlugin;
910
use Http\Discovery\HttpAsyncClientDiscovery;
1011
use Http\Discovery\HttpClientDiscovery;
@@ -14,6 +15,7 @@
1415
use Http\Message\Authentication\BasicAuth;
1516
use Http\Message\Authentication\Bearer;
1617
use Http\Message\Authentication\Wsse;
18+
use Psr\Http\Message\UriInterface;
1719
use Symfony\Component\Config\FileLocator;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921
use Symfony\Component\DependencyInjection\Definition;
@@ -215,6 +217,7 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
215217
{
216218
$serviceId = 'httplug.client.'.$name;
217219

220+
$plugins = $arguments['plugins'];
218221
$pluginClientOptions = [];
219222

220223
if ($profiling) {
@@ -234,6 +237,40 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
234237
$pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)];
235238
}
236239

240+
if (array_key_exists('options', $arguments)) {
241+
foreach ($arguments['options'] as $option => $value) {
242+
switch ($option) {
243+
case 'default_host':
244+
$uriService = $serviceId.'.default_host_uri';
245+
$addHostPlugin = $serviceId.'.default_host_plugin';
246+
$this->createUri($container, $uriService, $value);
247+
$container
248+
->register($addHostPlugin, AddHostPlugin::class)
249+
->setPublic(false)
250+
->addArgument(new Reference($uriService))
251+
;
252+
if (!in_array($addHostPlugin, $plugins)) {
253+
$plugins[] = $addHostPlugin;
254+
}
255+
break;
256+
case 'force_host':
257+
$uriService = $serviceId.'.force_host_uri';
258+
$addHostPlugin = $serviceId.'.force_host_plugin';
259+
$this->createUri($container, $uriService, $value);
260+
$container
261+
->register($addHostPlugin, AddHostPlugin::class)
262+
->setPublic(false)
263+
->addArgument(new Reference($uriService))
264+
->addArgument(['replace' => true])
265+
;
266+
if (!in_array($addHostPlugin, $plugins)) {
267+
$plugins[] = $addHostPlugin;
268+
}
269+
break;
270+
}
271+
}
272+
}
273+
237274
$container
238275
->register($serviceId, DummyClient::class)
239276
->setFactory([PluginClientFactory::class, 'createPluginClient'])
@@ -242,7 +279,7 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
242279
function ($id) {
243280
return new Reference($id);
244281
},
245-
$arguments['plugins']
282+
$plugins
246283
)
247284
)
248285
->addArgument(new Reference($arguments['factory']))
@@ -282,6 +319,23 @@ function ($id) {
282319
}
283320
}
284321

322+
/**
323+
* Create a URI object with the default URI factory.
324+
*
325+
* @param ContainerBuilder $container
326+
* @param string $serviceId Name of the private service to create
327+
* @param string $uri String representation of the URI
328+
*/
329+
private function createUri(ContainerBuilder $container, $serviceId, $uri)
330+
{
331+
$container
332+
->register($serviceId, UriInterface::class)
333+
->setPublic(false)
334+
->setFactory([new Reference('httplug.uri_factory'), 'createUri'])
335+
->addArgument($uri)
336+
;
337+
}
338+
285339
/**
286340
* Make the user can select what client is used for auto discovery. If none is provided, a service will be created
287341
* by finding a client using auto discovery.

Tests/Resources/Fixtures/config/full.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory',
1414
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory',
1515
],
16+
'clients' => [
17+
'test' => [
18+
'factory' => 'httplug.factory.guzzle6',
19+
'http_methods_client' => true,
20+
'options' => [
21+
'default_host' => 'http://localhost',
22+
],
23+
],
24+
],
1625
'profiling' => [
1726
'enabled' => true,
1827
'formatter' => 'my_toolbar_formatter',

Tests/Resources/Fixtures/config/full.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<uri-factory>Http\Message\UriFactory\GuzzleUriFactory</uri-factory>
1515
<stream-factory>Http\Message\StreamFactory\GuzzleStreamFactory</stream-factory>
1616
</classes>
17+
<client name="test" factory="httplug.factory.guzzle6" http-methods-client="true">
18+
<options default-host="http://localhost"/>
19+
</client>
1720
<profiling enabled="true" formatter="my_toolbar_formatter" captured_body_length="0"/>
1821
<plugins>
1922
<authentication>

Tests/Resources/Fixtures/config/full.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ httplug:
99
message_factory: Http\Message\MessageFactory\GuzzleMessageFactory
1010
uri_factory: Http\Message\UriFactory\GuzzleUriFactory
1111
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
12+
clients:
13+
test:
14+
factory: httplug.factory.guzzle6
15+
http_methods_client: true
16+
options:
17+
default_host: http://localhost
1218
profiling:
1319
enabled: true
1420
formatter: my_toolbar_formatter

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,19 @@ public function testSupportsAllConfigFormats()
115115
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory',
116116
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory',
117117
],
118-
'clients' => [],
118+
'clients' => [
119+
'test' => [
120+
'factory' => 'httplug.factory.guzzle6',
121+
'http_methods_client' => true,
122+
'flexible_client' => false,
123+
'batch_client' => false,
124+
'options' => [
125+
'default_host' => 'http://localhost',
126+
],
127+
'plugins' => [],
128+
'config' => [],
129+
],
130+
],
119131
'profiling' => [
120132
'enabled' => true,
121133
'formatter' => 'my_toolbar_formatter',

0 commit comments

Comments
 (0)