diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a89388..3203b98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- `PluginClientFactory` to create `PluginClient` instances. + ### Deprecated - The `debug_plugins` option for `PluginClient` is deprecated and will be removed in 2.0. Use the decorator design pattern instead like in [ProfilePlugin](https://github.com/php-http/HttplugBundle/blob/de33f9c14252f22093a5ec7d84f17535ab31a384/Collector/ProfilePlugin.php). diff --git a/spec/PluginClientFactorySpec.php b/spec/PluginClientFactorySpec.php new file mode 100644 index 0000000..da9febe --- /dev/null +++ b/spec/PluginClientFactorySpec.php @@ -0,0 +1,21 @@ +shouldHaveType('Http\Client\Common\PluginClientFactory'); + } + + function it_returns_a_plugin_client(HttpClient $httpClient) + { + $client = $this->createClient($httpClient); + + $client->shouldHaveType('Http\Client\Common\PluginClient'); + } +} diff --git a/src/PluginClientFactory.php b/src/PluginClientFactory.php new file mode 100644 index 0000000..14c4712 --- /dev/null +++ b/src/PluginClientFactory.php @@ -0,0 +1,55 @@ + + */ +final class PluginClientFactory +{ + /** + * @var callable + */ + private static $factory; + + /** + * Set the factory to use. + * The callable to provide must have the same arguments and return type as PluginClientFactory::createClient. + * This is used by the HTTPlugBundle to provide a better Symfony integration. + * + * @internal + * + * @param callable $factory + */ + public static function setFactory(callable $factory) + { + static::$factory = $factory; + } + + /** + * @param HttpClient|HttpAsyncClient $client + * @param Plugin[] $plugins + * @param array $options { + * + * @var string $client_name to give client a name which may be used when displaying client information like in + * the HTTPlugBundle profiler. + * } + * + * @see PluginClient constructor for PluginClient specific $options. + * + * @return PluginClient + */ + public function createClient($client, array $plugins = [], array $options = []) + { + if (static::$factory) { + $factory = static::$factory; + + return $factory($client, $plugins, $options); + } + + return new PluginClient($client, $plugins, $options); + } +}