Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

use function bin2hex;
use function in_array;
use function random_bytes;
use function array_key_exists;

final class Container implements ContainerInterface
Expand All @@ -30,6 +28,13 @@ final class Container implements ContainerInterface
/** @var string BaseURL for api requests */
private $baseUrl;

private const SERVICES = [
HttpHistory::class,
PluginClientBuilder::class,
StreamFactoryInterface::class,
RequestFactoryInterface::class,
];

public function __construct(HttpHistory $history, string $baseUrl)
{
$this->baseUrl = $baseUrl;
Expand All @@ -38,14 +43,7 @@ public function __construct(HttpHistory $history, string $baseUrl)

public function has($id)
{
static $services = [
HttpHistory::class,
PluginClientBuilder::class,
StreamFactoryInterface::class,
RequestFactoryInterface::class,
];

return in_array($id, $services, true);
return in_array($id, self::SERVICES, true);
}

public function get($id)
Expand Down Expand Up @@ -77,10 +75,9 @@ private function getPluginClientBuilder(): PluginClientBuilder

assert($this->services[HttpHistory::class] instanceof HttpHistory);

// use randomized strings so that these cannot be removed (safety)
$builder->addPlugin(bin2hex(random_bytes(10)), new ContentLengthPlugin);
$builder->addPlugin(bin2hex(random_bytes(10)), new BaseUriPlugin($baseUri));
$builder->addPlugin(bin2hex(random_bytes(10)), new HistoryPlugin($this->services[HttpHistory::class]));
$builder = $builder->addPlugin(new ContentLengthPlugin);
$builder = $builder->addPlugin(new BaseUriPlugin($baseUri));
$builder = $builder->addPlugin(new HistoryPlugin($this->services[HttpHistory::class]));

return $builder;
}
Expand Down
69 changes: 47 additions & 22 deletions src/Http/PluginClientBuilder.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,75 @@
<?php declare(strict_types=1);
namespace Behapi\Http;

use Psr\Http\Client\ClientInterface;

use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;

use function array_values;

/**
* Build an instance of a PluginClient with a dynamic list of plugins.
*
* @author Baptiste Clavié <clavie.b@gmail.com>
*/
final class PluginClientBuilder
{
/** @var Plugin[] */
private $plugins;
/** @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;
private $client = null;

public function addPlugin(string $name, Plugin $plugin): void
/** @param int $priority Priority of the plugin. The higher comes first. */
public function addPlugin(Plugin $plugin, int $priority = 0): self
{
$this->plugins[$name] = $plugin;
$this->plugins[$priority][] = $plugin;
$this->client = null;

return $this;
}

public function removePlugin(string $name): void
/** @param mixed $value */
public function addOption(string $name, $value): self
{
unset($this->plugins[$name]);
$this->options[$name] = $value;
$this->client = null;

return $this;
}

public function getPlugin(string $name): Plugin
public function removeOption(string $name): self
{
if (!isset($this->plugins[$name])) {
throw new PluginNotFound($name);
}
unset($this->options[$name]);
$this->client = null;

return $this->plugins[$name];
return $this;
}

public function createClient($client, array $options = []): PluginClient
public function createClient(ClientInterface $httpClient): PluginClient
{
if (null === $this->client) {
$this->client = new PluginClient(
$client,
array_values($this->plugins),
$options
);
static $client;

if ($client === $httpClient && $this->client !== null) {
return $this->client;
}

$client = $httpClient;
$plugins = $this->plugins;

if (0 === count($plugins)) {
$plugins[] = [];
}

return $this->client;
krsort($plugins);
$plugins = array_merge(...$plugins);

return $this->client = new PluginClient(
$httpClient,
array_values($plugins),
$this->options
);
}
}
4 changes: 2 additions & 2 deletions src/Http/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;

use function trim;
use function is_array;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct(PluginClientBuilder $builder, StreamFactoryInterface
$this->streamFactory = $streamFactory;
$this->requestFactory = $requestFactory;

$this->client = HttpClientDiscovery::find();
$this->client = Psr18ClientDiscovery::find();
}

/** @When /^I create a "(?P<method>GET|POST|PATCH|PUT|DELETE|OPTIONS|HEAD)" request to "(?P<url>.+?)"$/ */
Expand Down