8000 [HttpClient] Add a ConditionalHttpClient by XuruDragon · Pull Request #30592 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[HttpClient] Add a ConditionalHttpClient #30592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 222 additions & 125 deletions src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
Expand Down Expand Up @@ -1802,42 +1802,20 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder

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

$merger = new class() {
use HttpClientTrait;

public function merge(array $options, array $defaultOptions)
{
try {
[, $mergedOptions] = $this->prepareRequest(null, null, $options, $defaultOptions);

foreach ($mergedOptions as $k => $v) {
if (!isset($options[$k]) && !isset($defaultOptions[$k])) {
// Remove options added by prepareRequest()
unset($mergedOptions[$k]);
}
}

return $mergedOptions;
} catch (TransportExceptionInterface $e) {
throw new InvalidArgumentException($e->getMessage(), 0, $e);
}
}
};

$defaultOptions = $merger->merge($config['default_options'] ?? [], []);
$container->getDefinition('http_client')->setArguments([$defaultOptions, $config['max_host_connections'] ?? 6]);
$container->getDefinition('http_client')->setArguments([$config['default_options'] ?? [], $config['max_host_connections'] ?? 6]);
$httpClient = $container->get('http_client');

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
'disallow_search_engine_index' => true,
'http_client' => [
'enabled' => !class_exists(FullStack::class) && class_exists(HttpClient::class),
'clients' => [],
'scopes' => [],
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
'http_client' => [
'max_host_connections' => 4,
'default_options' => null,
'clients' => [
'scopes' => [
'foo' => [
'default_options' => null,
'base_uri' => 'http://example.com'
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
'default_options' => [
'headers' => ['foo' => 'bar'],
],
'clients' => [
'scopes' => [
'foo' => [
'max_host_connections' => 5,
'default_options' => [
'headers' => ['bar' => 'baz'],
],
'base_uri' => 'http://example.com',
'headers' => ['bar' => 'baz'],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<framework:config>
<framework:http-client max-host-connections="4">
<framework:default-options />
<framework:client name="foo">
<framework:default-options />
</framework:client>
<framework:scope
name="foo"
base-uri="http://example.com"
/>
628C </framework:http-client>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
<framework:default-options>
<framework:header name="foo">bar</framework:header>
</framework:default-options>
<framework:client name="foo" max-host-connections="5">
<framework:default-options>
<framework:header name="bar">baz</framework:header>
</framework:default-options>
</framework:client>
<framework:scope name="foo">
<framework:base-uri>http://example.com</framework:base-uri>
<framework:header name="bar">baz</framework:header>
</framework:scope>
</framework:http-client>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ framework:
http_client:
max_host_connections: 4
default_options: ~
clients:
scopes:
foo:
default_options: ~
base_uri: http://example.com
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ framework:
max_host_connections: 4
default_options:
headers: {'foo': 'bar'}
clients:
scopes:
foo:
max_host_connections: 5
default_options:
headers: {'bar': 'baz'}
base_uri: http://example.com
headers: {'bar': 'baz'}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
Expand All @@ -51,7 +52,6 @@
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Workflow;
use Symfony\Contracts\HttpClient\HttpClientInterface;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -1398,14 +1398,13 @@ public function testHttpClientDefaultOptions()
$this->assertTrue($container->hasDefinition('http_client'), '->registerHttpClientConfiguration() loads http_client.xml');

$defaultOptions = [
'query' => [],
'headers' => [],
'resolve' => [],
];
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());

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

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

$this->assertSame(['foo' => ['bar']], $container->getDefinition('http_client')->getArgument(0)['headers']);
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
$this->assertSame(['bar' => ['baz'], 'foo' => ['bar']], $container->getDefinition('foo')->getArgument(0)['headers']);
$this->assertSame(5, $container->getDefinition('foo')->getArgument(1));
$this->assertSame(['bar' => 'baz'], $container->getDefinition($container->getDefinition('foo')->getArgument(0))->getArgument(1)['headers']);
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
}

public function testHttpClientFullDefaultOptions()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/ScopingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function request(string $method, string $url, array $options = []): Respo
}

foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
if (preg_match("{{$regexp}}A", $url)) {
if (preg_match("{{$regexp}([:/?#]|$)}A", $url)) {
$options = self::mergeDefaultOptions($options, $defaultOptions, true);
break;
}
Expand Down
0