Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed96673

Browse files
committed
feature #48797 [FrameworkBundle] Add extra attribute for HttpClient Configuration (voodooism)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [FrameworkBundle] Add `extra` attribute for HttpClient Configuration | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | coming soon...<!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry sh - ould follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> I want to configure HttpClient once in configure file and forget about the configuration when doing a request or injection. In my particular case, I want to pass certificates to the client as raw values using special curlopts. Here #48775 proposed the best way to do it - using `extra` attribute in configuration. It's going to look like so ```php return static function (FrameworkConfig $frameworkConfig): void { $httpClient = $frameworkConfig->httpClient(); $httpClient->defaultOptions([ 'extra' => ['curl' => ['foo' => 'bar']] ]); $httpClient->scopedClient('some_client') ->baseUri('https://some.uri') ->header('Accept', 'application/json') ->extra(['curl' => ['foo' => 'bar']]); } ``` Commits ------- 6c89894 [FrameworkBundle] Add `extra` attribute for HttpClient Configuration
2 parents 5c09985 + 6c89894 commit ed96673

10 files changed

+39
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.3
55
---
66

7+
* Add `extra` option for `http_client.default_options` and `http_client.scoped_client`
78
* Add `DomCrawlerAssertionsTrait::assertSelectorCount(int $count, string $selector)`
89
* Allow to avoid `limit` definition in a RateLimiter configuration when using the `no_limit` policy
910
* Add `--format` option to the `debug:config` command

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,11 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e
17251725
->variableNode('md5')->end()
17261726
->end()
17271727
->end()
1728+
->arrayNode('extra')
1729+
->info('Extra options for specific HTTP client')
1730+
->normalizeKeys(false)
1731+
->variablePrototype()->end()
1732+
->end()
17281733
->append($this->addHttpClientRetrySection())
17291734
->end()
17301735
->end()
@@ -1868,6 +1873,11 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e
18681873
->variableNode('md5')->end()
18691874
->end()
18701875
->end()
1876+
->arrayNode('extra')
1877+
->info('Extra options for specific HTTP client')
1878+
->normalizeKeys(false)
1879+
->variablePrototype()->end()
1880+
->end()
18711881
->append($this->addHttpClientRetrySection())
18721882
->end()
18731883
->end()

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
621621
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
622622
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
623+
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
623624
</xsd:choice>
624625
<xsd:attribute name="max-redirects" type="xsd:integer" />
625626
<xsd:attribute name="http-version" type="xsd:string" />
@@ -645,6 +646,7 @@
645646
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
646647
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
647648
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
649+
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
648650
</xsd:choice>
649651
<xsd:attribute name="name" type="xsd:string" />
650652
<xsd:attribute name="scope" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'pin-sha256' => ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412='],
2525
'md5' => 'sdhtb481248721thbr=',
2626
],
27+
'extra' => ['foo' => ['bar' => 'baz']],
2728
],
2829
],
2930
]);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_override_default_options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
'max_host_connections' => 4,
77
'default_options' => [
88
'headers' => ['foo' => 'bar'],
9+
'extra' => ['foo' => 'bar'],
910
],
1011
'scoped_clients' => [
1112
'foo' => [
1213
'base_uri' => 'http://example.com',
1314
'headers' => ['bar' => 'baz'],
15+
'extra' => ['bar' => 'baz'],
1416
],
1517
],
1618
],

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<framework:pin-sha256>jsda84hjtyd4821bgfesd215bsfg5412=</framework:pin-sha256>
3131
<framework:md5>sdhtb481248721thbr=</framework:md5>
3232
</framework:peer-fingerprint>
33+
<framework:extra>
34+
<framework:foo>
35+
<framework:bar>baz</framework:bar>
36+
</framework:foo>
37+
</framework:extra>
3338
</framework:default-options>
3439
</framework:http-client>
3540
</framework:config>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
<framework:http-client max-host-connections="4">
1010
<framework:default-options>
1111
<framework:header name="foo">bar</framework:header>
12+
<framework:extra>
13+
<framework:foo>bar</framework:foo>
14+
</framework:extra>
1215
</framework:default-options>
1316
<framework:scoped-client name="foo" base-uri="http://example.com">
1417
<framework:header name="bar">baz</framework:header>
18+
<framework:extra>
19+
<framework:bar>baz</framework:bar>
20+
</framework:extra>
1521
</framework:scoped-client>
1622
</framework:http-client>
1723
</framework:config>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_full_default_options.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ framework:
2222
peer_fingerprint:
2323
pin-sha256: ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412=']
2424
md5: 'sdhtb481248721thbr='
25+
extra:
26+
foo:
27+
bar: 'baz'

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_override_default_options.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ framework:
44
max_host_connections: 4
55
default_options:
66
headers: {'foo': 'bar'}
7+
extra: {'foo': 'bar'}
78
scoped_clients:
89
foo:
910
base_uri: http://example.com
1011
headers: {'bar': 'baz'}
12+
extra: {'bar': 'baz'}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ public function testHttpClientDefaultOptions()
18521852
$defaultOptions = [
18531853
'headers' => [],
18541854
'resolve' => [],
1855+
'extra' => [],
18551856
];
18561857
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());
18571858

@@ -1872,17 +1873,21 @@ public function testHttpClientOverrideDefaultOptions()
18721873
$container = $this->createContainerFromFile('http_client_override_default_options');
18731874

18741875
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
1876+
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['extra']);
18751877
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
18761878
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
18771879

18781880
$expected = [
18791881
'headers' => [
18801882
'bar' => 'baz',
18811883
],
1884+
'extra' => [
1885+
'bar' => 'baz',
1886+
],
18821887
'query' => [],
18831888
'resolve' => [],
18841889
];
1885-
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(2));
1890+
$this->assertEquals($expected, $container->getDefinition('foo')->getArgument(2));
18861891
}
18871892

18881893
public function testHttpClientRetry()
@@ -1944,6 +1949,7 @@ public function testHttpClientFullDefaultOptions()
19441949
'pin-sha256' => ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412='],
19451950
'md5' => 'sdhtb481248721thbr=',
19461951
], $defaultOptions['peer_fingerprint']);
1952+
$this->assertSame(['foo' => ['bar' => 'baz']], $defaultOptions['extra']);
19471953
}
19481954

19491955
public function provideMailer(): array

0 commit comments

Comments
 (0)