Skip to content

Commit 46353a0

Browse files
committed
Do not override HttpClient DI
1 parent ef34917 commit 46353a0

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/AsyncAwsBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace AsyncAws\Symfony\Bundle;
66

7+
use AsyncAws\Symfony\Bundle\DependencyInjection\Compiler\CreateHttpClientPass;
78
use AsyncAws\Symfony\Bundle\DependencyInjection\Compiler\InjectCasterPass;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
910
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -15,5 +16,6 @@ public function build(ContainerBuilder $container)
1516
parent::build($container);
1617

1718
$container->addCompilerPass(new InjectCasterPass());
19+
$container->addCompilerPass(new CreateHttpClientPass());
1820
}
1921
}

src/DependencyInjection/AsyncAwsExtension.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
use AsyncAws\Core\Credentials\ChainProvider;
99
use AsyncAws\Core\Credentials\CredentialProvider;
1010
use AsyncAws\Core\Credentials\SymfonyCacheProvider;
11+
use AsyncAws\Core\HttpClient\AwsRetryStrategy;
1112
use AsyncAws\Symfony\Bundle\Secrets\CachedEnvVarLoader;
1213
use AsyncAws\Symfony\Bundle\Secrets\SsmVault;
1314
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15+
use Symfony\Component\DependencyInjection\ChildDefinition;
1416
use Symfony\Component\DependencyInjection\ContainerBuilder;
1517
use Symfony\Component\DependencyInjection\ContainerInterface;
1618
use Symfony\Component\DependencyInjection\Definition;
1719
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\HttpClient\Retry\RetryStrategyInterface;
21+
use Symfony\Component\HttpClient\RetryableHttpClient;
1822
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1923
use Symfony\Contracts\Cache\CacheInterface;
2024

@@ -94,7 +98,17 @@ private function addServiceDefinition(ContainerBuilder $container, string $name,
9498
$httpClient = $config['http_client'] ? new Reference($config['http_client']) : null;
9599
} else {
96100
// Use default Symfony http_client unless explicitly set to null.
97-
$httpClient = new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE);
101+
if (class_exists(RetryableHttpClient::class)) {
102+
$httpClient = new Definition(RetryableHttpClient::class);
103+
$httpClient->setArguments([
104+
new Reference('async_aws.http_client'),
105+
new Definition(AwsRetryStrategy::class),
106+
3,
107+
$logger,
108+
]);
109+
} else {
110+
$httpClient = new Reference('async_aws.http_client');
111+
}
98112
}
99113

100114
// If no credential provider is specified, lets configured a credentials provider with cache.
@@ -126,10 +140,12 @@ private function addServiceDefinition(ContainerBuilder $container, string $name,
126140
}
127141

128142
$definition = new Definition($clientClass);
129-
$definition->addArgument($config['config']);
130-
$definition->addArgument($credentialServiceId ? new Reference($credentialServiceId) : null);
131-
$definition->addArgument($httpClient);
132-
$definition->addArgument($logger);
143+
$definition->setArguments([
144+
$config['config'],
145+
$credentialServiceId ? new Reference($credentialServiceId) : null,
146+
$httpClient,
147+
$logger,
148+
]);
133149
$definition->addTag('monolog.logger', ['channel' => 'async_aws']);
134150
$container->setDefinition(sprintf('async_aws.client.%s', $name), $definition);
135151
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace AsyncAws\Symfony\Bundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\HttpClient\HttpClient;
8+
use Symfony\Contracts\HttpClient\HttpClientInterface;
9+
10+
class CreateHttpClientPass implements CompilerPassInterface
11+
{
12+
public function process(ContainerBuilder $container)
13+
{
14+
if (!$container->hasDefinition('http_client')) {
15+
$container->register('async_aws.http_client', HttpClientInterface::class)
16+
->setFactory([HttpClient::class, 'create']);
17+
return;
18+
}
19+
20+
$container->setAlias('async_aws.http_client', 'http_client');
21+
}
22+
}

0 commit comments

Comments
 (0)