80
80
use Symfony \Component \HttpClient \Retry \GenericRetryStrategy ;
81
81
use Symfony \Component \HttpClient \RetryableHttpClient ;
82
82
use Symfony \Component \HttpClient \ScopingHttpClient ;
83
+ use Symfony \Component \HttpClient \UriTemplateHttpClient ;
83
84
use Symfony \Component \HttpFoundation \Request ;
84
85
use Symfony \Component \HttpKernel \Attribute \AsController ;
85
86
use Symfony \Component \HttpKernel \CacheClearer \CacheClearerInterface ;
@@ -2338,6 +2339,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
2338
2339
$ options = $ config ['default_options ' ] ?? [];
2339
2340
$ retryOptions = $ options ['retry_failed ' ] ?? ['enabled ' => false ];
2340
2341
unset($ options ['retry_failed ' ]);
2342
+ $ defaultUriTemplateVars = $ options ['vars ' ] ?? [];
2343
+ unset($ options ['vars ' ]);
2341
2344
$ container ->getDefinition ('http_client ' )->setArguments ([$ options , $ config ['max_host_connections ' ] ?? 6 ]);
2342
2345
2343
2346
if (!$ hasPsr18 = ContainerBuilder::willBeAvailable ('psr/http-client ' , ClientInterface::class, ['symfony/framework-bundle ' , 'symfony/http-client ' ])) {
@@ -2349,11 +2352,31 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
2349
2352
$ container ->removeDefinition (HttpClient::class);
2350
2353
}
2351
2354
2352
- if ($ this ->readConfigEnabled ('http_client.retry_failed ' , $ container , $ retryOptions )) {
2355
+ if ($ hasRetryFailed = $ this ->readConfigEnabled ('http_client.retry_failed ' , $ container , $ retryOptions )) {
2353
2356
$ this ->registerRetryableHttpClient ($ retryOptions , 'http_client ' , $ container );
2354
2357
}
2355
2358
2356
- $ httpClientId = ($ retryOptions ['enabled ' ] ?? false ) ? 'http_client.retryable.inner ' : ($ this ->isInitializedConfigEnabled ('profiler ' ) ? '.debug.http_client.inner ' : 'http_client ' );
2359
+ if ($ hasUriTemplate = class_exists (UriTemplateHttpClient::class)) {
2360
+ if (ContainerBuilder::willBeAvailable ('guzzlehttp/uri-template ' , \GuzzleHttp \UriTemplate \UriTemplate::class, [])) {
2361
+ $ container ->setAlias ('http_client.uri_template_expander ' , 'http_client.uri_template_expander.guzzle ' );
2362
+ } elseif (ContainerBuilder::willBeAvailable ('rize/uri-template ' , \Rize \UriTemplate::class, [])) {
2363
+ $ container ->setAlias ('http_client.uri_template_expander ' , 'http_client.uri_template_expander.rize ' );
2364
+ }
2365
+
2366
+ $ container
2367
+ ->getDefinition ('http_client.uri_template ' )
2368
+ ->setArgument (2 , $ defaultUriTemplateVars );
2369
+ } elseif ($ defaultUriTemplateVars ) {
2370
+ throw new LogicException ('Support for URI template requires symfony/http-client 6.3 or higher, try upgrading. ' );
2371
+ }
2372
+
2373
+ $ httpClientId = match (true ) {
2374
+ $ hasUriTemplate => 'http_client.uri_template.inner ' ,
2375
+ $ hasRetryFailed => 'http_client.retryable.inner ' ,
2376
+ $ this ->isInitializedConfigEnabled ('profiler ' ) => '.debug.http_client.inner ' ,
2377
+ default => 'http_client ' ,
2378
+ };
2379
+
2357
2380
foreach ($ config ['scoped_clients ' ] as $ name => $ scopeConfig ) {
2358
2381
if ('http_client ' === $ name ) {
2359
2382
throw new InvalidArgumentException (sprintf ('Invalid scope name: "%s" is reserved. ' , $ name ));
@@ -2384,6 +2407,17 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
2384
2407
$ this ->registerRetryableHttpClient ($ retryOptions , $ name , $ container );
2385
2408
}
2386
2409
2410
+ if ($ hasUriTemplate ) {
2411
+ $ container
2412
+ ->register ($ name .'.uri_template ' , UriTemplateHttpClient::class)
2413
+ ->setDecoratedService ($ name , null , 7 ) // Between TraceableHttpClient (5) and RetryableHttpClient (10)
2414
+ ->setArguments ([
2415
+ new Reference ('.inner ' ),
2416
+ new Reference ('http_client.uri_template_expander ' , ContainerInterface::NULL_ON_INVALID_REFERENCE ),
2417
+ $ defaultUriTemplateVars ,
2418
+ ]);
2419
+ }
2420
+
2387
2421
$ container ->registerAliasForArgument ($ name , HttpClientInterface::class);
2388
2422
2389
2423
if ($ hasPsr18 ) {
0 commit comments