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