-
Notifications
You must be signed in to change notification settings - Fork 53
Add plugin client builder #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Http\Client\Common; | ||
|
||
use Http\Client\HttpAsyncClient; | ||
use Psr\Http\Client\ClientInterface; | ||
|
||
/** | ||
* Build an instance of a PluginClient with a dynamic list of plugins. | ||
* | ||
* @author Baptiste Clavié <clavie.b@gmail.com> | ||
*/ | ||
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 = []; | ||
|
||
/** | ||
* @param int $priority Priority of the plugin. The higher comes first. | ||
*/ | ||
public function addPlugin(Plugin $plugin, int $priority = 0): self | ||
{ | ||
$this->plugins[$priority][] = $plugin; | ||
|
||
return $this; | ||
} | ||
|
||
public function setOption(string $name, $value): self | ||
{ | ||
$this->options[$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function removeOption(string $name): self | ||
{ | ||
unset($this->options[$name]); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param ClientInterface | HttpAsyncClient $client | ||
*/ | ||
public function createClient($client): PluginClient | ||
{ | ||
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { | ||
throw new \RuntimeException('You must provide a valid http client'); | ||
} | ||
|
||
$plugins = $this->plugins; | ||
|
||
if (0 === count($plugins)) { | ||
$plugins[] = []; | ||
} | ||
Taluu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
krsort($plugins); | ||
$plugins = array_merge(...$plugins); | ||
|
||
return new PluginClient( | ||
$client, | ||
array_values($plugins), | ||
$this->options | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\Http\Client\Common; | ||
|
||
use Closure; | ||
use Http\Client\Common\Plugin; | ||
use Http\Client\Common\PluginClient; | ||
use Http\Client\Common\PluginClientBuilder; | ||
use Http\Client\HttpAsyncClient; | ||
use Http\Client\HttpClient; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PluginClientBuilderTest extends TestCase | ||
{ | ||
/** @dataProvider clientProvider */ | ||
public function testPriority(string $client): void | ||
{ | ||
$builder = new PluginClientBuilder(); | ||
|
||
$plugins = [ | ||
10 => $this->prophesize(Plugin::class)->reveal(), | ||
-10 => $this->prophesize(Plugin::class)->reveal(), | ||
0 => $this->prophesize(Plugin::class)->reveal(), | ||
]; | ||
|
||
foreach ($plugins as $priority => $plugin) { | ||
$builder->addPlugin($plugin, $priority); | ||
} | ||
|
||
$client = $this->prophesize($client)->reveal(); | ||
$client = $builder->createClient($client); | ||
|
||
$closure = Closure::bind( | ||
function (): array { | ||
return $this->plugins; | ||
}, | ||
$client, | ||
PluginClient::class | ||
); | ||
|
||
$plugged = $closure(); | ||
|
||
$expected = $plugins; | ||
krsort($expected); | ||
$expected = array_values($expected); | ||
|
||
$this->assertSame($expected, $plugged); | ||
} | ||
|
||
/** @dataProvider clientProvider */ | ||
public function testOptions(string $client): void | ||
{ | ||
$builder = new PluginClientBuilder(); | ||
$builder->setOption('max_restarts', 5); | ||
|
||
$client = $this->prophesize($client)->reveal(); | ||
$client = $builder->createClient($client); | ||
|
||
$closure = Closure::bind( | ||
function (): array { | ||
return $this->options; | ||
}, | ||
$client, | ||
PluginClient::class | ||
); | ||
|
||
$options = $closure(); | ||
|
||
$this->assertArrayHasKey('max_restarts', $options); | ||
$this->assertSame(5, $options['max_restarts']); | ||
} | ||
|
||
public function clientProvider(): iterable | ||
{ | ||
yield 'sync\'d http client' => [HttpClient::class]; | ||
yield 'async\'d http client' => [HttpAsyncClient::class]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.