ApiClientRegistry
in package
Central registry for the HTTP clients used by API resource classes.
All resources share one lazily-created, middleware-configured default client. Individual classes can be given their own client without affecting the rest (test isolation, custom transports).
Table of Contents
Properties
- $default : HttpClientInterface|null
- $overrides : array<class-string, HttpClientInterface>
Methods
- createConfiguredHttpClient() : HttpClient
- Create an HttpClient with middleware built from Config settings.
- removeFor() : void
- Remove the override for one specific class.
- reset() : void
- Clear the default client and all per-class overrides.
- resolve() : HttpClientInterface
- Resolve the client for a class: its override if set, otherwise the shared default (created from Config middleware settings on first use).
- setDefault() : void
- Set the shared default client used by every API class without an override.
- setFor() : void
- Set a client for one specific class only.
- createCacheMiddleware() : CacheMiddleware
- Build a CacheMiddleware from Config::setMiddleware()'s 'cache' entry.
Properties
$default
private
static HttpClientInterface|null
$default
= null
Shared default client
$overrides
private
static array<class-string, HttpClientInterface>
$overrides
= []
Per-class client overrides
Methods
createConfiguredHttpClient()
Create an HttpClient with middleware built from Config settings.
public
static createConfiguredHttpClient() : HttpClient
Return values
HttpClientremoveFor()
Remove the override for one specific class.
public
static removeFor(class-string $class) : void
Parameters
- $class : class-string
-
The API class
reset()
Clear the default client and all per-class overrides.
public
static reset() : void
Intended for test teardown so a mock set in one test never leaks into the next.
resolve()
Resolve the client for a class: its override if set, otherwise the shared default (created from Config middleware settings on first use).
public
static resolve(class-string $class) : HttpClientInterface
Parameters
- $class : class-string
-
The API class requesting a client
Return values
HttpClientInterfacesetDefault()
Set the shared default client used by every API class without an override.
public
static setDefault(HttpClientInterface $client) : void
Parameters
- $client : HttpClientInterface
-
The HTTP client
setFor()
Set a client for one specific class only.
public
static setFor(class-string $class, HttpClientInterface $client) : void
Parameters
- $class : class-string
-
The API class to scope the client to
- $client : HttpClientInterface
-
The HTTP client
createCacheMiddleware()
Build a CacheMiddleware from Config::setMiddleware()'s 'cache' entry.
private
static createCacheMiddleware(array<string, mixed> $config) : CacheMiddleware
Accepts an 'adapter' as either an instance or a shorthand string ('memory', 'file', 'apcu'); 'cache_dir' applies to the file adapter. Configuring the cache implies enabling it unless 'enabled' is set explicitly.
Parameters
- $config : array<string, mixed>
-
Cache middleware configuration