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

Skip to content

Commit 2580204

Browse files
[FrameworkBundle] change the way http clients are configured by leveraging ScopingHttpClient
1 parent 041f60f commit 2580204

17 files changed

+321
-216
lines changed

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

Lines changed: 215 additions & 126 deletions
Large diffs are not rendered by default.

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

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
use Symfony\Component\Form\FormTypeGuesserInterface;
6161
use Symfony\Component\Form\FormTypeInterface;
6262
use Symfony\Component\HttpClient\HttpClient;
63-
use Symfony\Component\HttpClient\HttpClientTrait;
6463
use Symfony\Component\HttpClient\Psr18Client;
64+
use Symfony\Component\HttpClient\ScopingHttpClient;
6565
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
6666
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
6767
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
@@ -115,7 +115,6 @@
115115
use Symfony\Component\Yaml\Command\LintCommand as BaseYamlLintCommand;
116116
use Symfony\Component\Yaml\Yaml;
117117
use Symfony\Contracts\Cache\CacheInterface;
118-
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
119118
use Symfony\Contracts\HttpClient\HttpClientInterface;
120119
use Symfony\Contracts\Service\ResetInterface;
121120
use Symfony\Contracts\Service\ServiceSubscriberInterface;
@@ -1802,42 +1801,29 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
18021801

18031802
$loader->load('http_client.xml');
18041803

1805-
$merger = new class() {
1806-
use HttpClientTrait;
1807-
1808-
public function merge(array $options, array $defaultOptions)
1809-
{
1810-
try {
1811-
[, $mergedOptions] = $this->prepareRequest(null, null, $options, $defaultOptions);
1812-
1813-
foreach ($mergedOptions as $k => $v) {
1814-
if (!isset($options[$k]) && !isset($defaultOptions[$k])) {
1815-
// Remove options added by prepareRequest()
1816-
unset($mergedOptions[$k]);
1817-
}
1818-
}
1819-
1820-
return $mergedOptions;
1821-
} catch (TransportExceptionInterface $e) {
1822-
throw new InvalidArgumentException($e->getMessage(), 0, $e);
1823-
}
1824-
}
1825-
};
1826-
1827-
$defaultOptions = $merger->merge($config['default_options'] ?? [], []);
1828-
$container->getDefinition('http_client')->setArguments([$defaultOptions, $config['max_host_connections'] ?? 6]);
1804+
$container->getDefinition('http_client')->setArguments([$config['default_options'] ?? [], $config['max_host_connections'] ?? 6]);
1805+
$httpClient = $container->get('http_client');
18291806

18301807
if (!$hasPsr18 = interface_exists(ClientInterface::class)) {
18311808
$container->removeDefinition('psr18.http_client');
1809+
$container->removeDefinition('psr18.authenticated_http_client');
18321810
$container->removeAlias(ClientInterface::class);
1811+
$container->removeAlias(ClientInterface::class.' $authenticatedClient');
18331812
}
18341813

1835-
foreach ($config['clients'] as $name => $clientConfig) {
1836-
$options = $merger->merge($clientConfig['default_options'] ?? [], $defaultOptions);
1814+
$scopes = [];
1815+
1816+
foreach ($config['scopes'] as $name => $scopeConfig) {
1817+
if (\in_array($name, ['http_client', 'authenticated_http_client'], true)) {
1818+
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
1819+
}
1820+
1821+
$scope = $scopeConfig['scope'];
1822+
unset($scopeConfig['scope']);
1823+
$scopes[$scope] = $scopeConfig;
18371824

1838-
$container->register($name, HttpClientInterface::class)
1839-
->setFactory([HttpClient::class, 'create'])
1840-
->setArguments([$options, $clientConfig['max_host_connections'] ?? $config['max_host_connections'] ?? 6]);
1825+
$container->register($name, ScopingHttpClient::class)
1826+
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);
18411827

18421828
$container->registerAliasForArgument($name, HttpClientInterface::class);
18431829

@@ -1849,6 +1835,8 @@ public function merge(array $options, array $defaultOptions)
18491835
$container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name);
18501836
}
18511837
}
1838+
1839+
$container->getDefinition('authenticated_http_client')->replaceArgument(1, $scopes);
18521840
}
18531841

18541842
/**

src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,16 @@
1616
<argument type="service" id="http_client" />
1717
</service>
1818
<service id="Psr\Http\Client\ClientInterface" alias="psr18.http_client" />
19+
20+
<service id="authenticated_http_client" class="Symfony\Component\HttpClient\ScopingHttpClient">
21+
<argument type="service" id="http_client" />
22+
<argument type="collection" />
23+
</service>
24+
<service id="Symfony\Contracts\HttpClient\HttpClientInterface $authenticatedClient" alias="authenticated_http_client" />
25+
26+
<service id="psr18.authenticated_http_client" class="Symfony\Component\HttpClient\Psr18Client" autowire="true">
27+
<argument type="service" id="authenticated_http_client" />
28+
</service>
29+
<service id="Psr\Http\Client\ClientInterface $authenticatedClient" alias="psr18.authenticated_http_client" />
1930
</services>
2031
</container>

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -449,30 +449,55 @@
449449

450450
<xsd:complexType name="http_client">
451451
<xsd:sequence>
452-
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
453-
<xsd:element name="client" type="http_client_client" minOccurs="0" maxOccurs="unbounded" />
452+
<xsd:element name="default-options" type="http_client_default_options" minOccurs="0" />
453+
<xsd:element name="scope" type="http_client_scope_options" minOccurs="0" maxOccurs="unbounded" />
454454
</xsd:sequence>
455455
<xsd:attribute name="enabled" type="xsd:boolean" />
456456
<xsd:attribute name="max-host-connections" type="xsd:integer" />
457457
</xsd:complexType>
458458

459-
<xsd:complexType name="http_client_options" mixed="true">
459+
<xsd:complexType name="http_client_default_options" mixed="true">
460460
<xsd:choice maxOccurs="unbounded">
461-
<xsd:element name="query" type="http_query" minOccurs="0" maxOccurs="unbounded" />
462461
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
463462
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
464463
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
465464
</xsd:choice>
465+
<xsd:attribute name="max-redirects" type="xsd:integer" />
466+
<xsd:attribute name="http-version" type="xsd:string" />
466467
<xsd:attribute name="proxy" type="xsd:string" />
468+
<xsd:attribute name="no-proxy" type="xsd:string" />
467469
<xsd:attribute name="timeout" type="xsd:float" />
468470
<xsd:attribute name="bindto" type="xsd:string" />
469471
<xsd:attribute name="verify-peer" type="xsd:boolean" />
472+
<xsd:attribute name="verify-host" type="xsd:boolean" />
473+
<xsd:attribute name="cafile" type="xsd:string" />
474+
<xsd:attribute name="capath" type="xsd:string" />
475+
<xsd:attribute name="local-cert" type="xsd:string" />
476+
<xsd:attribute name="local-pk" type="xsd:string" />
477+
<xsd:attribute name="passphrase" type="xsd:string" />
478+
<xsd:attribute name="ciphers" type="xsd:string" />
479+
480+
</xsd:complexType>
481+
482+
<xsd:complexType name="http_client_scope_options" mixed="true">
483+
<xsd:choice maxOccurs="unbounded">
484+
<xsd:element name="query" type="http_query" minOccurs="0" maxOccurs="unbounded" />
485+
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
486+
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
487+
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
488+
</xsd:choice>
489+
<xsd:attribute name="name" type="xsd:string" />
490+
<xsd:attribute name="scope" type="xsd:string" />
491+
<xsd:attribute name="base-uri" type="xsd:string" />
470492
<xsd:attribute name="auth-basic" type="xsd:string" />
471493
<xsd:attribute name="auth-bearer" type="xsd:string" />
472494
<xsd:attribute name="max-redirects" type="xsd:integer" />
473495
<xsd:attribute name="http-version" type="xsd:string" />
474-
<xsd:attribute name="base-uri" type="xsd:string" />
496+
<xsd:attribute name="proxy" type="xsd:string" />
475497
<xsd:attribute name="no-proxy" type="xsd:string" />
498+
<xsd:attribute name="timeout" type="xsd:float" />
499+
<xsd:attribute name="bindto" type="xsd:string" />
500+
<xsd:attribute name="verify-peer" type="xsd:boolean" />
476501
<xsd:attribute name="verify-host" type="xsd:boolean" />
477502
<xsd:attribute name="cafile" type="xsd:string" />
478503
<xsd:attribute name="capath" type="xsd:string" />
@@ -482,14 +507,6 @@
482507
<xsd:attribute name="ciphers" type="xsd:string" />
483508
</xsd:complexType>
484509

485-
<xsd:complexType name="http_client_client">
486-
<xsd:sequence>
487-
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
488-
</xsd:sequence>
489-
<xsd:attribute name="name" type="xsd:string" />
490-
<xsd:attribute name="max-host-connections" type="xsd:integer" />
491-
</xsd:complexType>
492-
493510
<xsd:complexType name="fingerprint">
494511
<xsd:choice maxOccurs="unbounded">
495512
<xsd:element name="pin-sha256" type="xsd:string" minOccurs="0" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
334334
'disallow_search_engine_index' => true,
335335
'http_client' => [
336336
'enabled' => !class_exists(FullStack::class) && class_exists(HttpClient::class),
337-
'clients' => [],
337+
'scopes' => [],
338338
],
339339
];
340340
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
'http_client' => [
55
'max_host_connections' => 4,
66
'default_options' => null,
7-
'clients' => [
7+
'scopes' => [
88
'foo' => [
9-
'default_options' => null,
9+
'base_uri' => 'http://example.com',
1010
],
1111
],
1212
],

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
$container->loadFromExtension('framework', [
44
'http_client' => [
55
'default_options' => [
6-
'auth_basic' => 'foo:bar',
7-
'query' => ['foo' => 'bar', 'bar' => 'baz'],
86
'headers' => ['X-powered' => 'PHP'],
97
'max_redirects' => 2,
108
'http_version' => '2.0',
11-
'base_uri' => 'http://example.com',
129
'resolve' => ['localhost' => '127.0.0.1'],
1310
'proxy' => 'proxy.org',
1411
'timeout' => 3.5,

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
'default_options' => [
77
'headers' => ['foo' => 'bar'],
88
],
9-
'clients' => [
9+
'scopes' => [
1010
'foo' => [
11-
'max_host_connections' => 5,
12-
'default_options' => [
13-
'headers' => ['bar' => 'baz'],
14-
],
11+
'base_uri' => 'http://example.com',
12+
'headers' => ['bar' => 'baz'],
1513
],
1614
],
1715
],

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
<framework:config>
99
<framework:http-client max-host-connections="4">
1010
<framework:default-options />
11-
<framework:client name="foo">
12-
<framework:default-options />
13-
</framework:client>
11+
<framework:scope
12+
name="foo"
13+
base-uri="http://example.com"
14+
/>
1415
</framework:http-client>
1516
</framework:config>
1617
</container>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
bindto="127.0.0.1"
1313
timeout="3.5"
1414
verify-peer="true"
15-
auth-basic="foo:bar"
1615
max-redirects="2"
1716
http-version="2.0"
18-
base-uri="http://example.com"
1917
verify-host="true"
2018
cafile="/etc/ssl/cafile"
2119
capath="/etc/ssl"
@@ -24,8 +22,6 @@
2422
passphrase="password123456"
2523
ciphers="RC4-SHA:TLS13-AES-128-GCM-SHA256"
2624
>
27-
<framework:query key="foo">bar</framework:query>
28-
<framework:query key="bar">baz</framework:query>
2925
<framework:header name="X-powered">PHP</framework:header>
3026
<framework:resolve host="localhost">127.0.0.1</framework:resolve>
3127
<framework:peer-fingerprint>

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
<framework:default-options>
1111
<framework:header name="foo">bar</framework:header>
1212
</framework:default-options>
13-
<framework:client name="foo" max-host-connections="5">
14-
<framework:default-options>
15-
<framework:header name="bar">baz</framework:header>
16-
</framework:default-options>
17-
</framework:client>
13+
<framework:scope name="foo" base-uri="http://example.com">
14+
<framework:header name="bar">baz</framework:header>
15+
</framework:scope>
1816
</framework:http-client>
1917
</framework:config>
2018
</container>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ framework:
22
http_client:
33
max_host_connections: 4
44
default_options: ~
5-
clients:
5+
scopes:
66
foo:
7-
default_options: ~
7+
base_uri: http://example.com

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
framework:
22
http_client:
33
default_options:
4-
auth_basic: foo:bar
5-
query: {'foo': 'bar', 'bar': 'baz'}
64
headers:
75
X-powered: PHP
86
max_redirects: 2
97
http_version: 2.0
10-
base_uri: 'http://example.com'
118
resolve: {'localhost': '127.0.0.1'}
129
proxy: proxy.org
1310
timeout: 3.5

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ framework:
33
max_host_connections: 4
44
default_options:
55
headers: {'foo': 'bar'}
6-
clients:
6+
scopes:
77
foo:
8-
max_host_connections: 5
9-
default_options:
10-
headers: {'bar': 'baz'}
8+
base_uri: http://example.com
9+
headers: {'bar': 'baz'}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
3636
use Symfony\Component\DependencyInjection\Reference;
3737
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
38+
use Symfony\Component\HttpClient\ScopingHttpClient;
3839
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
3940
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
4041
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
@@ -51,7 +52,6 @@
5152
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
5253
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
5354
use Symfony\Component\Workflow;
54-
use Symfony\Contracts\HttpClient\HttpClientInterface;
5555

5656
abstract class FrameworkExtensionTest extends TestCase
5757
{
@@ -1398,25 +1398,34 @@ public function testHttpClientDefaultOptions()
13981398
$this->assertTrue($container->hasDefinition('http_client'), '->registerHttpClientConfiguration() loads http_client.xml');
13991399

14001400
$defaultOptions = [
1401-
'query' => [],
14021401
'headers' => [],
14031402
'resolve' => [],
14041403
];
14051404
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());
14061405

14071406
$this->assertTrue($container->hasDefinition('foo'), 'should have the "foo" service.');
1408-
$this->assertSame(HttpClientInterface::class, $container->getDefinition('foo')->getClass());
1409-
$this->assertSame([$defaultOptions, 4], $container->getDefinition('foo')->getArguments());
1407+
$this->assertSame(ScopingHttpClient::class, $container->getDefinition('foo')->getClass());
14101408
}
14111409

14121410
public function testHttpClientOverrideDefaultOptions()
14131411
{
14141412
$container = $this->createContainerFromFile('http_client_override_default_options');
14151413

1416-
$this->assertSame(['foo' => ['bar']], $container->getDefinition('http_client')->getArgument(0)['headers']);
1414+
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
14171415
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
1418-
$this->assertSame(['bar' => ['baz'], 'foo' => ['bar']], $container->getDefinition('foo')->getArgument(0)['headers']);
1419-
$this->assertSame(5, $container->getDefinition('foo')->getArgument(1));
1416+
1417+
$expected = [
1418+
'http\://example\.com/' => [
1419+
'base_uri' => 'http://example.com',
1420+
'headers' => [
1421+
'bar' => 'baz',
1422+
],
1423+
'query' => [],
1424+
'resolve' => [],
1425+
],
1426+
];
1427+
1428+
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(1));
14201429
}
14211430

14221431
public function testHttpClientFullDefaultOptions()
@@ -1425,12 +1434,9 @@ public function testHttpClientFullDefaultOptions()
14251434

14261435
$defaultOptions = $container->getDefinition('http_client')->getArgument(0);
14271436

1428-
$this->assertSame('foo:bar', $defaultOptions['auth_basic']);
1429-
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $defaultOptions['query']);
1430-
$this->assertSame(['x-powered' => ['PHP']], $defaultOptions['headers']);
1437+
$this->assertSame(['X-powered' => 'PHP'], $defaultOptions['headers']);
14311438
$this->assertSame(2, $defaultOptions['max_redirects']);
14321439
$this->assertSame(2.0, (float) $defaultOptions['http_version']);
1433-
$this->assertSame('http://example.com', $defaultOptions['base_uri']);
14341440
$this->assertSame(['localhost' => '127.0.0.1'], $defaultOptions['resolve']);
14351441
$this->assertSame('proxy.org', $defaultOptions['proxy']);
14361442
$this->assertSame(3.5, $defaultOptions['timeout']);

0 commit comments

Comments
 (0)