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

Skip to content

Commit 1d16850

Browse files
Anthony MARTINnicolas-grekas
Anthony MARTIN
authored andcommitted
[FrameworkBundle] configure HttpClient services using scopes and ScopingClient
1 parent f206538 commit 1d16850

11 files changed

+254
-183
lines changed

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

Lines changed: 222 additions & 125 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 30 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;
@@ -1802,42 +1802,20 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
18021802

18031803
$loader->load('http_client.xml');
18041804

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]);
1805+
$container->getDefinition('http_client')->setArguments([$config['default_options'] ?? [], $config['max_host_connections'] ?? 6]);
1806+
$httpClient = $container->get('http_client');
18291807

18301808
if (!$hasPsr18 = interface_exists(ClientInterface::class)) {
18311809
$container->removeDefinition('psr18.http_client');
18321810
$container->removeAlias(ClientInterface::class);
18331811
}
18341812

1835-
foreach ($config['clients'] as $name => $clientConfig) {
1836-
$options = $merger->merge($clientConfig['default_options'] ?? [], $defaultOptions);
1813+
foreach ($config['scopes'] as $name => $scopeConfig) {
1814+
$scope = $scopeConfig['scope'];
1815+
unset($scopeConfig['scope']);
18371816

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

18421820
$container->registerAliasForArgument($name, HttpClientInterface::class);
18431821

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_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_override_default_options.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
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">
14+
<framework:base-uri>http://example.com</framework:base-uri>
15+
<framework:header name="bar">baz</framework:header>
16+
</framework:scope>
1817
</framework:http-client>
1918
</framework:config>
2019
</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_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: 4 additions & 5 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,14 +1398,13 @@ 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());
1407+
$this->assertSame(ScopingHttpClient::class, $container->getDefinition('foo')->getClass());
14091408
$this->assertSame([$defaultOptions, 4], $container->getDefinition('foo')->getArguments());
14101409
}
14111410

@@ -1415,8 +1414,8 @@ public function testHttpClientOverrideDefaultOptions()
14151414

14161415
$this->assertSame(['foo' => ['bar']], $container->getDefinition('http_client')->getArgument(0)['headers']);
14171416
$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));
1417+
$this->assertSame(['bar' => 'baz'], $container->getDefinition($container->getDefinition('foo')->getArgument(0))->getArgument(1)['headers']);
1418+
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
14201419
}
14211420

14221421
public function testHttpClientFullDefaultOptions()

src/Symfony/Component/HttpClient/ScopingHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function request(string $method, string $url, array $options = []): Respo
6161
}
6262

6363
foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
64-
if (preg_match("{{$regexp}}A", $url)) {
64+
if (preg_match("{{$regexp}([:/?#]|$)}A", $url)) {
6565
$options = self::mergeDefaultOptions($options, $defaultOptions, true);
6666
break;
6767
}

0 commit comments

Comments
 (0)