Skip to content

Commit a3c93b2

Browse files
Nyholmsagikazarmark
authored andcommitted
Added possibility for debug plugins (#22)
* 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
1 parent 09009b2 commit a3c93b2

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Added
77

88
- Suggest separate plugins in composer.json
9+
- Introduced `debug_plugins` option for `PluginClient`
910

1011

1112
## 1.1.0 - 2016-05-04

spec/PluginClientSpec.php

+42
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,46 @@ function it_throws_loop_exception(HttpClient $httpClient, RequestInterface $requ
8787

8888
$this->shouldThrow('Http\Client\Common\Exception\LoopException')->duringSendRequest($request);
8989
}
90+
91+
function it_injects_debug_plugins(HttpClient $httpClient, RequestInterface $request, Plugin $plugin0, Plugin $plugin1, Plugin $debugPlugin)
92+
{
93+
$plugin0
94+
->handleRequest(
95+
$request,
96+
Argument::type('callable'),
97+
Argument::type('callable')
98+
)
99+
->shouldBeCalledTimes(1)
100+
->will(function ($args) {
101+
return $args[1]($args[0]);
102+
})
103+
;
104+
$plugin1
105+
->handleRequest(
106+
$request,
107+
Argument::type('callable'),
108+
Argument::type('callable')
109+
)
110+
->shouldBeCalledTimes(1)
111+
->will(function ($args) {
112+
return $args[1]($args[0]);
113+
})
114+
;
115+
116+
$debugPlugin
117+
->handleRequest(
118+
$request,
119+
Argument::type('callable'),
120+
Argument::type('callable')
121+
)
122+
->shouldBeCalledTimes(3)
123+
->will(function ($args) {
124+
return $args[1]($args[0]);
125+
})
126+
;
127+
128+
129+
$this->beConstructedWith($httpClient, [$plugin0, $plugin1], ['debug_plugins'=>[$debugPlugin]]);
130+
$this->sendRequest($request);
131+
}
90132
}

src/PluginClient.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ final class PluginClient implements HttpClient, HttpAsyncClient
4444
* @param Plugin[] $plugins
4545
* @param array $options {
4646
*
47-
* @var int $max_restarts
47+
* @var int $max_restarts
48+
* @var Plugin[] $debug_plugins an array of plugins that are injected between each normal plugin
4849
* }
4950
*
5051
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient
@@ -110,8 +111,22 @@ private function configure(array $options = [])
110111
$resolver = new OptionsResolver();
111112
$resolver->setDefaults([
112113
'max_restarts' => 10,
114+
'debug_plugins' => [],
113115
]);
114116

117+
$resolver
118+
->setAllowedTypes('debug_plugins', 'array')
119+
->setAllowedValues('debug_plugins', function (array $plugins) {
120+
foreach ($plugins as $plugin) {
121+
// Make sure each object passed with the `debug_plugins` is an instance of Plugin.
122+
if (!$plugin instanceof Plugin) {
123+
return false;
124+
}
125+
}
126+
127+
return true;
128+
});
129+
115130
return $resolver->resolve($options);
116131
}
117132

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

130-
while ($plugin = array_pop($pluginList)) {
145+
/*
146+
* Inject debug plugins between each plugin.
147+
*/
148+
$pluginListWithDebug = $this->options['debug_plugins'];
149+
foreach ($pluginList as $plugin) {
150+
$pluginListWithDebug[] = $plugin;
151+
$pluginListWithDebug = array_merge($pluginListWithDebug, $this->options['debug_plugins']);
152+
}
153+
154+
while ($plugin = array_pop($pluginListWithDebug)) {
131155
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) {
132156
return $plugin->handleRequest($request, $lastCallable, $firstCallable);
133157
};

0 commit comments

Comments
 (0)