1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Common; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Exception\LoopException; |
6
|
|
|
use Http\Client\Exception as HttplugException; |
7
|
|
|
use Http\Client\HttpAsyncClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Promise\FulfilledPromise; |
10
|
|
|
use Http\Promise\RejectedPromise; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The client managing plugins and providing a decorator around HTTP Clients. |
16
|
|
|
* |
17
|
|
|
* @author Joel Wurtz <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class PluginClient implements HttpClient, HttpAsyncClient |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* An HTTP async client. |
23
|
|
|
* |
24
|
|
|
* @var HttpAsyncClient |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The plugin chain. |
30
|
|
|
* |
31
|
|
|
* @var Plugin[] |
32
|
|
|
*/ |
33
|
|
|
private $plugins; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A list of options. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $options; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param HttpClient|HttpAsyncClient $client |
44
|
|
|
* @param Plugin[] $plugins |
45
|
|
|
* @param array $options { |
46
|
|
|
* |
47
|
|
|
* @var int $max_restarts |
48
|
|
|
* @var Plugin[] $debug_plugins an array of plugins that are injected between each normal plugin |
49
|
|
|
* } |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
52
|
|
|
*/ |
53
|
8 |
|
public function __construct($client, array $plugins = [], array $options = []) |
54
|
|
|
{ |
55
|
8 |
|
if ($client instanceof HttpAsyncClient) { |
56
|
3 |
|
$this->client = $client; |
57
|
8 |
|
} elseif ($client instanceof HttpClient) { |
58
|
5 |
|
$this->client = new EmulatedHttpAsyncClient($client); |
59
|
5 |
|
} else { |
60
|
|
|
throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
$this->plugins = $plugins; |
64
|
8 |
|
$this->options = $this->configure($options); |
65
|
8 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
4 |
|
public function sendRequest(RequestInterface $request) |
71
|
|
|
{ |
72
|
|
|
// If we don't have an http client, use the async call |
73
|
4 |
|
if (!($this->client instanceof HttpClient)) { |
74
|
1 |
|
return $this->sendAsyncRequest($request)->wait(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Else we want to use the synchronous call of the underlying client, and not the async one in the case |
78
|
|
|
// we have both an async and sync call |
79
|
|
|
$pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
80
|
|
|
try { |
81
|
2 |
|
return new FulfilledPromise($this->client->sendRequest($request)); |
82
|
|
|
} catch (HttplugException $exception) { |
83
|
|
|
return new RejectedPromise($exception); |
|
|
|
|
84
|
|
|
} |
85
|
3 |
|
}); |
86
|
|
|
|
87
|
3 |
|
return $pluginChain($request)->wait(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
2 |
|
public function sendAsyncRequest(RequestInterface $request) |
94
|
|
|
{ |
95
|
|
|
$pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
96
|
2 |
|
return $this->client->sendAsyncRequest($request); |
97
|
2 |
|
}); |
98
|
|
|
|
99
|
2 |
|
return $pluginChain($request); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Configure the plugin client. |
104
|
|
|
* |
105
|
|
|
* @param array $options |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
8 |
|
private function configure(array $options = []) |
110
|
|
|
{ |
111
|
8 |
|
$resolver = new OptionsResolver(); |
112
|
8 |
|
$resolver->setDefaults([ |
113
|
8 |
|
'max_restarts' => 10, |
114
|
8 |
|
'debug_plugins' => [], |
115
|
8 |
|
]); |
116
|
|
|
|
117
|
|
|
$resolver |
118
|
8 |
|
->setAllowedTypes('debug_plugins', 'array') |
119
|
|
|
->setAllowedValues('debug_plugins', function (array $plugins) { |
120
|
8 |
|
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
|
8 |
|
} |
126
|
|
|
|
127
|
8 |
|
return true; |
128
|
8 |
|
}); |
129
|
|
|
|
130
|
8 |
|
return $resolver->resolve($options); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create the plugin chain. |
135
|
|
|
* |
136
|
|
|
* @param Plugin[] $pluginList A list of plugins |
137
|
|
|
* @param callable $clientCallable Callable making the HTTP call |
138
|
|
|
* |
139
|
|
|
* @return callable |
140
|
|
|
*/ |
141
|
5 |
|
private function createPluginChain($pluginList, callable $clientCallable) |
142
|
|
|
{ |
143
|
5 |
|
$firstCallable = $lastCallable = $clientCallable; |
144
|
|
|
|
145
|
|
|
/* |
146
|
|
|
* Inject debug plugins between each plugin. |
147
|
|
|
*/ |
148
|
5 |
|
$pluginListWithDebug = $this->options['debug_plugins']; |
149
|
5 |
|
foreach ($pluginList as $plugin) { |
150
|
1 |
|
$pluginListWithDebug[] = $plugin; |
151
|
1 |
|
$pluginListWithDebug = array_merge($pluginListWithDebug, $this->options['debug_plugins']); |
152
|
5 |
|
} |
153
|
|
|
|
154
|
5 |
|
while ($plugin = array_pop($pluginListWithDebug)) { |
155
|
|
|
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) { |
156
|
1 |
|
return $plugin->handleRequest($request, $lastCallable, $firstCallable); |
157
|
1 |
|
}; |
158
|
|
|
|
159
|
1 |
|
$firstCallable = $lastCallable; |
160
|
1 |
|
} |
161
|
|
|
|
162
|
5 |
|
$firstCalls = 0; |
163
|
5 |
|
$firstCallable = function (RequestInterface $request) use ($lastCallable, &$firstCalls) { |
164
|
5 |
|
if ($firstCalls > $this->options['max_restarts']) { |
165
|
1 |
|
throw new LoopException('Too many restarts in plugin client', $request); |
166
|
|
|
} |
167
|
|
|
|
168
|
5 |
|
++$firstCalls; |
169
|
|
|
|
170
|
5 |
|
return $lastCallable($request); |
171
|
5 |
|
}; |
172
|
|
|
|
173
|
5 |
|
return $firstCallable; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: