From 4321aac0115ac14dbd51604a4774d3e5141f51a4 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 2 Jul 2016 19:49:23 +0200 Subject: [PATCH 1/7] Added possibility for debug plugins --- src/PluginClient.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index 9aa9e6c..e2d790d 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -110,6 +110,7 @@ private function configure(array $options = []) $resolver = new OptionsResolver(); $resolver->setDefaults([ 'max_restarts' => 10, + 'debug_plugins' => [] ]); return $resolver->resolve($options); @@ -127,7 +128,16 @@ private function createPluginChain($pluginList, callable $clientCallable) { $firstCallable = $lastCallable = $clientCallable; - while ($plugin = array_pop($pluginList)) { + /* + * Inject debug plugins between each plugin. + */ + $pluginListWithDegug = $this->options['debug_plugins']; + foreach ($pluginList as $plugin) { + $pluginListWithDegug[] = $plugin; + $pluginListWithDegug = array_merge($pluginListWithDegug, $this->options['debug_plugins']); + } + + while ($plugin = array_pop($pluginListWithDegug)) { $lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) { return $plugin->handleRequest($request, $lastCallable, $firstCallable); }; From fdb16f438f3860af68cff3d0f14e2830e6e6e925 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 3 Jul 2016 09:59:46 +0200 Subject: [PATCH 2/7] Added changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfdceab..5517c1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Added - Suggest separate plugins in composer.json +- Introduced `debug_plugins` option for `PluginClient` ## 1.1.0 - 2016-05-04 From 4f491efc83af572c563c2a0ae193fd2d7a4c6f82 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 3 Jul 2016 10:00:20 +0200 Subject: [PATCH 3/7] Style fixes --- src/PluginClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index e2d790d..4572af7 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -110,7 +110,7 @@ private function configure(array $options = []) $resolver = new OptionsResolver(); $resolver->setDefaults([ 'max_restarts' => 10, - 'debug_plugins' => [] + 'debug_plugins' => [], ]); return $resolver->resolve($options); @@ -129,7 +129,7 @@ private function createPluginChain($pluginList, callable $clientCallable) $firstCallable = $lastCallable = $clientCallable; /* - * Inject debug plugins between each plugin. + * Inject debug plugins between each plugin. */ $pluginListWithDegug = $this->options['debug_plugins']; foreach ($pluginList as $plugin) { From 825301a804adaca369e9574571711d7ef8f10ee8 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 3 Jul 2016 19:03:13 +0200 Subject: [PATCH 4/7] Added docs and corrected typos --- src/PluginClient.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index 4572af7..52efd22 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -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 all normal plugins * } * * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient @@ -131,13 +132,13 @@ private function createPluginChain($pluginList, callable $clientCallable) /* * Inject debug plugins between each plugin. */ - $pluginListWithDegug = $this->options['debug_plugins']; + $pluginListWithDebug = $this->options['debug_plugins']; foreach ($pluginList as $plugin) { - $pluginListWithDegug[] = $plugin; - $pluginListWithDegug = array_merge($pluginListWithDegug, $this->options['debug_plugins']); + $pluginListWithDebug[] = $plugin; + $pluginListWithDebug = array_merge($pluginListWithDebug, $this->options['debug_plugins']); } - while ($plugin = array_pop($pluginListWithDegug)) { + while ($plugin = array_pop($pluginListWithDebug)) { $lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) { return $plugin->handleRequest($request, $lastCallable, $firstCallable); }; From 0ad83c01d6bce328c277ee9b9629b289b1cc8c28 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 4 Jul 2016 08:37:56 +0200 Subject: [PATCH 5/7] typo --- src/PluginClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index 52efd22..c0105f7 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -45,7 +45,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient * @param array $options { * * @var int $max_restarts - * @var Plugin[] $debug_plugins an array of plugins that are injected between all normal plugins + * @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 From 929eeb06ca7b6a1f99d0bac39320ee32144c7013 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 4 Jul 2016 20:49:00 +0200 Subject: [PATCH 6/7] Validate debug_plugins option --- src/PluginClient.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/PluginClient.php b/src/PluginClient.php index c0105f7..2e69ecf 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -114,6 +114,19 @@ private function configure(array $options = []) '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); } From 0f822bc727a873c386ab5406f97a5a9002a1a513 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 4 Jul 2016 21:08:23 +0200 Subject: [PATCH 7/7] Added tests to make sure the debug plugins runs between each plugin --- spec/PluginClientSpec.php | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 88406ae..27b4068 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -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); + } }