From b423382db74ee17fbfff0e43739d7fccc854471e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Wed, 27 Jan 2021 17:18:36 +0100 Subject: [PATCH 1/3] Bump to php-http client-common 2.2 Needed to be able to use "brand-new" plugin-client builder --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c8bb059..0828a33 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "psr/http-factory-implementation": "^1.0", "php-http/discovery": "^1.11", - "php-http/client-common": "^2.0" + "php-http/client-common": "^2.2" }, "suggest": { From ab5902da7ad8048431ce47f398b525a35ab68ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Wed, 27 Jan 2021 17:17:10 +0100 Subject: [PATCH 2/3] Use php-http's client builder instead of home-made one The home-made one was supposed to be there until the PR on php-http was merged, which it was and also released --- README.md | 2 +- src/Container.php | 2 +- src/Http/RequestContext.php | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4387f46..120663e 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ container capable of using behapi's container. Some services are provided to be injected in contexts, which are the following: -- `@Behapi\Http\PluginClientBuilder`, which will build a +- `@Http\Client\Common\PluginClientBuilder`, which will build a `Http\Client\Common\PluginClient` when needed - `@Behapi\HttpHistory\History`, which is a sort of a container with the last requests done and last responses received diff --git a/src/Container.php b/src/Container.php index dae8900..38f82dd 100644 --- a/src/Container.php +++ b/src/Container.php @@ -4,6 +4,7 @@ use Behat\Behat\HelperContainer\ContainerInterface; use Behat\Behat\HelperContainer\Exception\ServiceNotFoundException; +use Http\Client\Common\PluginClientBuilder; use Http\Client\Common\Plugin\BaseUriPlugin; use Http\Client\Common\Plugin\HistoryPlugin; use Http\Client\Common\Plugin\ContentLengthPlugin; @@ -13,7 +14,6 @@ use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; -use Behapi\Http\PluginClientBuilder; use Behapi\HttpHistory\History as HttpHistory; use function in_array; diff --git a/src/Http/RequestContext.php b/src/Http/RequestContext.php index c6c927f..275e030 100644 --- a/src/Http/RequestContext.php +++ b/src/Http/RequestContext.php @@ -12,6 +12,7 @@ use Behat\Gherkin\Node\TableNode; use Http\Discovery\Psr18ClientDiscovery; +use Http\Client\Common\PluginClientBuilder; use function trim; use function is_array; From df116d52e536785bdddfb509eb9e95b987b5a224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Wed, 27 Jan 2021 17:19:38 +0100 Subject: [PATCH 3/3] Remove home-made PluginClientBuilder --- src/Http/PluginClientBuilder.php | 75 -------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 src/Http/PluginClientBuilder.php diff --git a/src/Http/PluginClientBuilder.php b/src/Http/PluginClientBuilder.php deleted file mode 100644 index 5bed3fa..0000000 --- a/src/Http/PluginClientBuilder.php +++ /dev/null @@ -1,75 +0,0 @@ - - */ -final class PluginClientBuilder -{ - /** @var Plugin[][] List of plugins ordered by priority [priority => Plugin[]]). */ - private $plugins = []; - - /** @var array Array of options to give to the plugin client */ - private $options = []; - - /** @var ?PluginClient */ - private $client = null; - - /** @param int $priority Priority of the plugin. The higher comes first. */ - public function addPlugin(Plugin $plugin, int $priority = 0): self - { - $this->plugins[$priority][] = $plugin; - $this->client = null; - - return $this; - } - - /** @param mixed $value */ - public function addOption(string $name, $value): self - { - $this->options[$name] = $value; - $this->client = null; - - return $this; - } - - public function removeOption(string $name): self - { - unset($this->options[$name]); - $this->client = null; - - return $this; - } - - public function createClient(ClientInterface $httpClient): PluginClient - { - static $client; - - if ($client === $httpClient && $this->client !== null) { - return $this->client; - } - - $client = $httpClient; - $plugins = $this->plugins; - - if (0 === count($plugins)) { - $plugins[] = []; - } - - krsort($plugins); - $plugins = array_merge(...$plugins); - - return $this->client = new PluginClient( - $httpClient, - array_values($plugins), - $this->options - ); - } -}