http_client.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
  11. use Psr\Http\Client\ClientInterface;
  12. use Psr\Http\Message\ResponseFactoryInterface;
  13. use Psr\Http\Message\StreamFactoryInterface;
  14. use Symfony\Component\HttpClient\HttpClient;
  15. use Symfony\Component\HttpClient\HttplugClient;
  16. use Symfony\Component\HttpClient\Psr18Client;
  17. use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
  18. use Symfony\Contracts\HttpClient\HttpClientInterface;
  19. return static function (ContainerConfigurator $container) {
  20. $container->services()
  21. ->set('http_client', HttpClientInterface::class)
  22. ->factory([HttpClient::class, 'create'])
  23. ->args([
  24. [], // default options
  25. abstract_arg('max host connections'),
  26. ])
  27. ->call('setLogger', [service('logger')->ignoreOnInvalid()])
  28. ->tag('monolog.logger', ['channel' => 'http_client'])
  29. ->tag('http_client.client')
  30. ->alias(HttpClientInterface::class, 'http_client')
  31. ->set('psr18.http_client', Psr18Client::class)
  32. ->args([
  33. service('http_client'),
  34. service(ResponseFactoryInterface::class)->ignoreOnInvalid(),
  35. service(StreamFactoryInterface::class)->ignoreOnInvalid(),
  36. ])
  37. ->alias(ClientInterface::class, 'psr18.http_client')
  38. ->set(\Http\Client\HttpClient::class, HttplugClient::class)
  39. ->args([
  40. service('http_client'),
  41. service(ResponseFactoryInterface::class)->ignoreOnInvalid(),
  42. service(StreamFactoryInterface::class)->ignoreOnInvalid(),
  43. ])
  44. ->set('http_client.abstract_retry_strategy', GenericRetryStrategy::class)
  45. ->abstract()
  46. ->args([
  47. abstract_arg('http codes'),
  48. abstract_arg('delay ms'),
  49. abstract_arg('multiplier'),
  50. abstract_arg('max delay ms'),
  51. abstract_arg('jitter'),
  52. ])
  53. ;
  54. };