Skip to content

Commit d053921

Browse files
authored
Merge pull request #316 from php-http/content-type-plugin
allow to configure content type plugin
2 parents b587fb4 + 998841b commit d053921

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
99
- Autowiring support for FlexibleClient, HttpMethodsClientInterface and
1010
BatchClientInterface if they are enabled on the default/first client.
1111
(Only available with Httplug 2)
12+
- Configuration for the content_type plugin
1213

1314
### Changed
1415

src/DependencyInjection/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ private function createClientPluginNode()
351351
->end()
352352
->end()
353353
->end()
354+
->arrayNode('content_type')
355+
->canBeEnabled()
356+
->info('Detect the content type of a request body and set the Content-Type header if it is not already set.')
357+
->children()
358+
->booleanNode('skip_detection')
359+
->info('Whether to skip detection when request body is larger than size_limit')
360+
->defaultFalse()
361+
->end()
362+
->scalarNode('size_limit')
363+
->info('Skip content type detection if request body is larger than size_limit bytes')
364+
->end()
365+
->end()
366+
->end()
354367
->arrayNode('header_append')
355368
->canBeEnabled()
356369
->info('Append headers to the request. If the header already exists the value will be appended to the current value.')

src/DependencyInjection/HttplugExtension.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,40 +186,47 @@ private function configurePluginByName($name, Definition $definition, array $con
186186
->replaceArgument(2, $options);
187187

188188
break;
189+
189190
case 'cookie':
190191
$definition->replaceArgument(0, new Reference($config['cookie_jar']));
191192

192193
break;
194+
193195
case 'decoder':
194196
$definition->addArgument([
195197
'use_content_encoding' => $config['use_content_encoding'],
196198
]);
197199

198200
break;
201+
199202
case 'history':
200203
$definition->replaceArgument(0, new Reference($config['journal']));
201204

202205
break;
206+
203207
case 'logger':
204208
$definition->replaceArgument(0, new Reference($config['logger']));
205209
if (!empty($config['formatter'])) {
206210
$definition->replaceArgument(1, new Reference($config['formatter']));
207211
}
208212

209213
break;
214+
210215
case 'redirect':
211216
$definition->addArgument([
212217
'preserve_header' => $config['preserve_header'],
213218
'use_default_for_multiple' => $config['use_default_for_multiple'],
214219
]);
215220

216221
break;
222+
217223
case 'retry':
218224
$definition->addArgument([
219225
'retries' => $config['retry'],
220226
]);
221227

222228
break;
229+
223230
case 'stopwatch':
224231
$definition->replaceArgument(0, new Reference($config['stopwatch']));
225232

@@ -236,12 +243,14 @@ private function configurePluginByName($name, Definition $definition, array $con
236243
]);
237244

238245
break;
246+
239247
case 'add_path':
240248
$pathUriService = $serviceId.'.path_uri';
241249
$this->createUri($container, $pathUriService, $config['path']);
242250
$definition->replaceArgument(0, new Reference($pathUriService));
243251

244252
break;
253+
245254
case 'base_uri':
246255
$baseUriService = $serviceId.'.base_uri';
247256
$this->createUri($container, $baseUriService, $config['uri']);
@@ -251,6 +260,11 @@ private function configurePluginByName($name, Definition $definition, array $con
251260
]);
252261

253262
break;
263+
264+
case 'content_type':
265+
$definition->replaceArgument(0, $config);
266+
break;
267+
254268
case 'header_append':
255269
case 'header_defaults':
256270
case 'header_set':

src/Resources/config/plugins.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<argument/>
4242
<argument/>
4343
</service>
44+
<service id="httplug.plugin.content_type" class="Http\Client\Common\Plugin\ContentTypePlugin" public="false" abstract="true">
45+
<argument/>
46+
</service>
4447
<service id="httplug.plugin.header_append" class="Http\Client\Common\Plugin\HeaderAppendPlugin" public="false" abstract="true">
4548
<argument/>
4649
</service>

tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public function testClientPlugins()
8383
'host' => 'http://localhost:8000',
8484
],
8585
],
86+
[
87+
'content_type' => [
88+
'skip_detection' => true,
89+
],
90+
],
8691
[
8792
'header_append' => [
8893
'headers' => ['X-FOO' => 'bar'],
@@ -131,6 +136,7 @@ public function testClientPlugins()
131136
'httplug.client.acme.plugin.decoder',
132137
'httplug.plugin.redirect',
133138
'httplug.client.acme.plugin.add_host',
139+
'httplug.client.acme.plugin.content_type',
134140
'httplug.client.acme.plugin.header_append',
135141
'httplug.client.acme.plugin.header_defaults',
136142
'httplug.client.acme.plugin.header_set',

0 commit comments

Comments
 (0)