Skip to content

Commit

Permalink
Added possibility for debug plugins (#22)
Browse files Browse the repository at this point in the history
* Added possibility for debug plugins

* Added changelog

* Style fixes

* Added docs and corrected typos

* typo

* Validate debug_plugins option

* Added tests to make sure the debug plugins runs between each plugin
  • Loading branch information
Nyholm authored and sagikazarmark committed Jul 5, 2016
1 parent 09009b2 commit a3c93b2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Added

- Suggest separate plugins in composer.json
- Introduced `debug_plugins` option for `PluginClient`


## 1.1.0 - 2016-05-04
Expand Down
42 changes: 42 additions & 0 deletions spec/PluginClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,46 @@ function it_throws_loop_exception(HttpClient $httpClient, RequestInterface $requ

$this->shouldThrow('Http\Client\Common\Exception\LoopException')->duringSendRequest($request);
}

function it_injects_debug_plugins(HttpClient $httpClient, RequestInterface $request, Plugin $plugin0, Plugin $plugin1, Plugin $debugPlugin)
{
$plugin0
->handleRequest(
$request,
Argument::type('callable'),
Argument::type('callable')
)
->shouldBeCalledTimes(1)
->will(function ($args) {
return $args[1]($args[0]);
})
;
$plugin1
->handleRequest(
$request,
Argument::type('callable'),
Argument::type('callable')
)
->shouldBeCalledTimes(1)
->will(function ($args) {
return $args[1]($args[0]);
})
;

$debugPlugin
->handleRequest(
$request,
Argument::type('callable'),
Argument::type('callable')
)
->shouldBeCalledTimes(3)
->will(function ($args) {
return $args[1]($args[0]);
})
;


$this->beConstructedWith($httpClient, [$plugin0, $plugin1], ['debug_plugins'=>[$debugPlugin]]);
$this->sendRequest($request);
}
}
28 changes: 26 additions & 2 deletions src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ final class PluginClient implements HttpClient, HttpAsyncClient
* @param Plugin[] $plugins
* @param array $options {
*
* @var int $max_restarts
* @var int $max_restarts
* @var Plugin[] $debug_plugins an array of plugins that are injected between each normal plugin
* }
*
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient
Expand Down Expand Up @@ -110,8 +111,22 @@ private function configure(array $options = [])
$resolver = new OptionsResolver();
$resolver->setDefaults([
'max_restarts' => 10,
'debug_plugins' => [],
]);

$resolver
->setAllowedTypes('debug_plugins', 'array')
->setAllowedValues('debug_plugins', function (array $plugins) {
foreach ($plugins as $plugin) {
// Make sure each object passed with the `debug_plugins` is an instance of Plugin.
if (!$plugin instanceof Plugin) {
return false;
}
}

return true;
});

return $resolver->resolve($options);
}

Expand All @@ -127,7 +142,16 @@ private function createPluginChain($pluginList, callable $clientCallable)
{
$firstCallable = $lastCallable = $clientCallable;

while ($plugin = array_pop($pluginList)) {
/*
* Inject debug plugins between each plugin.
*/
$pluginListWithDebug = $this->options['debug_plugins'];
foreach ($pluginList as $plugin) {
$pluginListWithDebug[] = $plugin;
$pluginListWithDebug = array_merge($pluginListWithDebug, $this->options['debug_plugins']);
}

while ($plugin = array_pop($pluginListWithDebug)) {
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) {
return $plugin->handleRequest($request, $lastCallable, $firstCallable);
};
Expand Down

0 comments on commit a3c93b2

Please sign in to comment.