Skip to content

Commit

Permalink
feature #32104 Add autowiring for HTTPlug (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

Add autowiring for HTTPlug

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

f76e77d Add autowiring for HTTPlug
  • Loading branch information
fabpot committed Jun 22, 2019
2 parents b9ad880 + f76e77d commit 115e67b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;
use Http\Client\HttpClient;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Http\Client\ClientInterface;
Expand Down Expand Up @@ -60,7 +61,6 @@
use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
Expand Down Expand Up @@ -1881,6 +1881,10 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
$container->removeAlias(ClientInterface::class);
}

if (!interface_exists(HttpClient::class)) {
$container->removeDefinition(HttpClient::class);
}

foreach ($config['scoped_clients'] as $name => $scopeConfig) {
if ('http_client' === $name) {
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
Expand All @@ -1901,9 +1905,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
$container->registerAliasForArgument($name, HttpClientInterface::class);

if ($hasPsr18) {
$container->register('psr18.'.$name, Psr18Client::class)
->setAutowired(true)
->setArguments([new Reference($name)]);
$container->setDefinition('psr18.'.$name, new ChildDefinition('psr18.http_client'))
->replaceArgument(0, new Reference($name));

$container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name);
}
Expand Down
Expand Up @@ -22,5 +22,11 @@
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" on-invalid="ignore" />
</service>
<service id="Psr\Http\Client\ClientInterface" alias="psr18.http_client" />

<service id="Http\Client\HttpClient" class="Symfony\Component\HttpClient\HttplugClient">
<argument type="service" id="http_client" />
<argument type="service" id="Http\Message\ResponseFactory" on-invalid="ignore" />
<argument type="service" id="Http\Message\StreamFactory" on-invalid="ignore" />
</service>
</services>
</container>

0 comments on commit 115e67b

Please sign in to comment.