|
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
|
|
|
* TODO: make class final once removed from plugins. |
|
20
|
|
|
*/ |
|
21
|
|
|
/*final*/ class PluginClient implements HttpClient, HttpAsyncClient |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* An HTTP async client. |
|
25
|
|
|
* |
|
26
|
|
|
* @var HttpAsyncClient |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The plugin chain. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Plugin[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $plugins; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* A list of options. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $options; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param HttpClient|HttpAsyncClient $client |
|
46
|
|
|
* @param Plugin[] $plugins |
|
47
|
|
|
* @param array $options { |
|
48
|
|
|
* |
|
49
|
|
|
* @var int $max_restarts |
|
50
|
|
|
* } |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($client, array $plugins = [], array $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($client instanceof HttpAsyncClient) { |
|
57
|
|
|
$this->client = $client; |
|
58
|
|
|
} elseif ($client instanceof HttpClient) { |
|
59
|
|
|
$this->client = new EmulatedHttpAsyncClient($client); |
|
60
|
|
|
} else { |
|
61
|
|
|
throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->plugins = $plugins; |
|
65
|
|
|
$this->options = $this->configure($options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function sendRequest(RequestInterface $request) |
|
72
|
|
|
{ |
|
73
|
|
|
// If we don't have an http client, use the async call |
|
74
|
|
|
if (!($this->client instanceof HttpClient)) { |
|
75
|
|
|
return $this->sendAsyncRequest($request)->wait(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Else we want to use the synchronous call of the underlying client, and not the async one in the case |
|
79
|
|
|
// we have both an async and sync call |
|
80
|
|
|
$pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
|
81
|
|
|
try { |
|
82
|
|
|
return new FulfilledPromise($this->client->sendRequest($request)); |
|
83
|
|
|
} catch (HttplugException $exception) { |
|
84
|
|
|
return new RejectedPromise($exception); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
}); |
|
87
|
|
|
|
|
88
|
|
|
return $pluginChain($request)->wait(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
|
95
|
|
|
{ |
|
96
|
|
|
$pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
|
97
|
|
|
return $this->client->sendAsyncRequest($request); |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
return $pluginChain($request); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Configure the plugin client. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $options |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
private function configure(array $options = []) |
|
111
|
|
|
{ |
|
112
|
|
|
$resolver = new OptionsResolver(); |
|
113
|
|
|
$resolver->setDefaults([ |
|
114
|
|
|
'max_restarts' => 10, |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
return $resolver->resolve($options); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Create the plugin chain. |
|
122
|
|
|
* |
|
123
|
|
|
* @param Plugin[] $pluginList A list of plugins |
|
124
|
|
|
* @param callable $clientCallable Callable making the HTTP call |
|
125
|
|
|
* |
|
126
|
|
|
* @return callable |
|
127
|
|
|
*/ |
|
128
|
|
|
private function createPluginChain($pluginList, callable $clientCallable) |
|
129
|
|
|
{ |
|
130
|
|
|
$firstCallable = $lastCallable = $clientCallable; |
|
131
|
|
|
|
|
132
|
|
|
while ($plugin = array_pop($pluginList)) { |
|
133
|
|
|
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) { |
|
134
|
|
|
return $plugin->handleRequest($request, $lastCallable, $firstCallable); |
|
135
|
|
|
}; |
|
136
|
|
|
|
|
137
|
|
|
$firstCallable = $lastCallable; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$firstCalls = 0; |
|
141
|
|
|
$firstCallable = function (RequestInterface $request) use ($lastCallable, &$firstCalls) { |
|
142
|
|
|
if ($firstCalls > $this->options['max_restarts']) { |
|
143
|
|
|
throw $this->createLoopException($request); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
++$firstCalls; |
|
147
|
|
|
|
|
148
|
|
|
return $lastCallable($request); |
|
149
|
|
|
}; |
|
150
|
|
|
|
|
151
|
|
|
return $firstCallable; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Creates a new loop exception. |
|
156
|
|
|
* |
|
157
|
|
|
* @param RequestInterface $request |
|
158
|
|
|
* |
|
159
|
|
|
* @return LoopException |
|
160
|
|
|
* |
|
161
|
|
|
* TODO: Remove once plugin client is removed from plugins. |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function createLoopException(RequestInterface $request) |
|
164
|
|
|
{ |
|
165
|
|
|
return new LoopException('Too many restarts in plugin client', $request); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
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: