PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
← All changes | includes/sdk/s3/GuzzleHttp/Client.php +81 -103 1.2.91.4.1 View file →
@@ -3,30 +3,23 @@
3 3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp;
4 4
5 5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Cookie\CookieJar;
6 6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\GuzzleException;
7 -use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
8 -use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
7 +use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\InvalidArgumentException;
8 +use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise as P;
9 +use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
9 10 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
10 11 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
11 12 use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface;
12 13 /**
13 - * @method ResponseInterface get(string|UriInterface $uri, array $options = [])
14 - * @method ResponseInterface head(string|UriInterface $uri, array $options = [])
15 - * @method ResponseInterface put(string|UriInterface $uri, array $options = [])
16 - * @method ResponseInterface post(string|UriInterface $uri, array $options = [])
17 - * @method ResponseInterface patch(string|UriInterface $uri, array $options = [])
18 - * @method ResponseInterface delete(string|UriInterface $uri, array $options = [])
19 - * @method Promise\PromiseInterface getAsync(string|UriInterface $uri, array $options = [])
20 - * @method Promise\PromiseInterface headAsync(string|UriInterface $uri, array $options = [])
21 - * @method Promise\PromiseInterface putAsync(string|UriInterface $uri, array $options = [])
22 - * @method Promise\PromiseInterface postAsync(string|UriInterface $uri, array $options = [])
23 - * @method Promise\PromiseInterface patchAsync(string|UriInterface $uri, array $options = [])
24 - * @method Promise\PromiseInterface deleteAsync(string|UriInterface $uri, array $options = [])
14 + * @final
25 15 */
26 -class Client implements ClientInterface
16 +class Client implements ClientInterface, \Dudlewebs\WPMCS\s3\Psr\Http\Client\ClientInterface
27 17 {
28 - /** @var array Default request options */
18 + use ClientTrait;
19 + /**
20 + * @var array Default request options
21 + */
29 22 private $config;
30 23 /**
31 24 * Clients accept an array of constructor parameters.
32 25 *
@@ -55,9 +48,9 @@
55 48 * - **: any request option
56 49 *
57 50 * @param array $config Client configuration settings.
58 51 *
59 - * @see \GuzzleHttp\RequestOptions for a list of available request options.
52 + * @see RequestOptions for a list of available request options.
60 53 */
61 54 public function __construct(array $config = [])
62 55 {
63 56 if (!isset($config['handler'])) {
@@ -62,13 +55,13 @@
62 55 {
63 56 if (!isset($config['handler'])) {
64 57 $config['handler'] = HandlerStack::create();
65 58 } elseif (!\is_callable($config['handler'])) {
66 - throw new \InvalidArgumentException('handler must be a callable');
59 + throw new InvalidArgumentException('handler must be a callable');
67 60 }
68 61 // Convert the base_uri to a UriInterface
69 62 if (isset($config['base_uri'])) {
70 - $config['base_uri'] = Psr7\uri_for($config['base_uri']);
63 + $config['base_uri'] = Psr7\Utils::uriFor($config['base_uri']);
71 64 }
72 65 $this->configureDefaults($config);
73 66 }
74 67 /**
@@ -74,17 +67,19 @@
74 67 /**
75 68 * @param string $method
76 69 * @param array $args
77 70 *
78 - * @return Promise\PromiseInterface
71 + * @return PromiseInterface|ResponseInterface
72 + *
73 + * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0.
79 74 */
80 75 public function __call($method, $args)
81 76 {
82 77 if (\count($args) < 1) {
83 - throw new \InvalidArgumentException('Magic request methods require a URI and optional options array');
78 + throw new InvalidArgumentException('Magic request methods require a URI and optional options array');
84 79 }
85 80 $uri = $args[0];
86 - $opts = isset($args[1]) ? $args[1] : [];
81 + $opts = $args[1] ?? [];
87 82 return \substr($method, -5) === 'Async' ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) : $this->request($method, $uri, $opts);
88 83 }
89 84 /**
90 85 * Asynchronously send an HTTP request.
@@ -90,12 +85,10 @@
90 85 * Asynchronously send an HTTP request.
91 86 *
92 87 * @param array $options Request options to apply to the given
93 88 * request and to the transfer. See \GuzzleHttp\RequestOptions.
94 - *
95 - * @return Promise\PromiseInterface
96 89 */
97 - public function sendAsync(RequestInterface $request, array $options = [])
90 + public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface
98 91 {
99 92 // Merge the base URI into the request URI if needed.
100 93 $options = $this->prepareDefaults($options);
101 94 return $this->transfer($request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), $options);
@@ -105,17 +98,28 @@
105 98 *
106 99 * @param array $options Request options to apply to the given
107 100 * request and to the transfer. See \GuzzleHttp\RequestOptions.
108 101 *
109 - * @return ResponseInterface
110 102 * @throws GuzzleException
111 103 */
112 - public function send(RequestInterface $request, array $options = [])
104 + public function send(RequestInterface $request, array $options = []) : ResponseInterface
113 105 {
114 106 $options[RequestOptions::SYNCHRONOUS] = \true;
115 107 return $this->sendAsync($request, $options)->wait();
116 108 }
117 109 /**
110 + * The HttpClient PSR (PSR-18) specify this method.
111 + *
112 + * {@inheritDoc}
113 + */
114 + public function sendRequest(RequestInterface $request) : ResponseInterface
115 + {
116 + $options[RequestOptions::SYNCHRONOUS] = \true;
117 + $options[RequestOptions::ALLOW_REDIRECTS] = \false;
118 + $options[RequestOptions::HTTP_ERRORS] = \false;
119 + return $this->sendAsync($request, $options)->wait();
120 + }
121 + /**
118 122 * Create and send an asynchronous HTTP request.
119 123 *
120 124 * Use an absolute path to override the base path of the client, or a
121 125 * relative path to append to the base path of the client. The URL can
@@ -124,22 +128,20 @@
124 128 *
125 129 * @param string $method HTTP method
126 130 * @param string|UriInterface $uri URI object or string.
127 131 * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions.
128 - *
129 - * @return Promise\PromiseInterface
130 132 */
131 - public function requestAsync($method, $uri = '', array $options = [])
133 + public function requestAsync(string $method, $uri = '', array $options = []) : PromiseInterface
132 134 {
133 135 $options = $this->prepareDefaults($options);
134 136 // Remove request modifying parameter because it can be done up-front.
135 - $headers = isset($options['headers']) ? $options['headers'] : [];
136 - $body = isset($options['body']) ? $options['body'] : null;
137 - $version = isset($options['version']) ? $options['version'] : '1.1';
137 + $headers = $options['headers'] ?? [];
138 + $body = $options['body'] ?? null;
139 + $version = $options['version'] ?? '1.1';
138 140 // Merge the URI into the base URI.
139 - $uri = $this->buildUri($uri, $options);
141 + $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options);
140 142 if (\is_array($body)) {
141 - $this->invalidBody();
143 + throw $this->invalidBody();
142 144 }
143 145 $request = new Psr7\Request($method, $uri, $headers, $body, $version);
144 146 // Remove the option so that they are not doubly-applied.
145 147 unset($options['headers'], $options['body'], $options['version']);
@@ -155,12 +157,11 @@
155 157 * @param string $method HTTP method.
156 158 * @param string|UriInterface $uri URI object or string.
157 159 * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions.
158 160 *
159 - * @return ResponseInterface
160 161 * @throws GuzzleException
161 162 */
162 - public function request($method, $uri = '', array $options = [])
163 + public function request(string $method, $uri = '', array $options = []) : ResponseInterface
163 164 {
164 165 $options[RequestOptions::SYNCHRONOUS] = \true;
165 166 return $this->requestAsync($method, $uri, $options)->wait();
166 167 }
@@ -173,24 +174,19 @@
173 174 *
174 175 * @param string|null $option The config option to retrieve.
175 176 *
176 177 * @return mixed
178 + *
179 + * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0.
177 180 */
178 - public function getConfig($option = null)
181 + public function getConfig(?string $option = null)
179 182 {
180 - return $option === null ? $this->config : (isset($this->config[$option]) ? $this->config[$option] : null);
183 + return $option === null ? $this->config : $this->config[$option] ?? null;
181 184 }
182 - /**
183 - * @param string|null $uri
184 - *
185 - * @return UriInterface
186 - */
187 - private function buildUri($uri, array $config)
185 + private function buildUri(UriInterface $uri, array $config) : UriInterface
188 186 {
189 - // for BC we accept null which would otherwise fail in uri_for
190 - $uri = Psr7\uri_for($uri === null ? '' : $uri);
191 187 if (isset($config['base_uri'])) {
192 - $uri = Psr7\UriResolver::resolve(Psr7\uri_for($config['base_uri']), $uri);
188 + $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri);
193 189 }
194 190 if (isset($config['idn_conversion']) && $config['idn_conversion'] !== \false) {
195 191 $idnOptions = $config['idn_conversion'] === \true ? \IDNA_DEFAULT : $config['idn_conversion'];
196 192 $uri = Utils::idnUriConvert($uri, $idnOptions);
@@ -198,26 +194,23 @@
198 194 return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri;
199 195 }
200 196 /**
201 197 * Configures the default options for a client.
202 - *
203 - * @param array $config
204 - * @return void
205 198 */
206 - private function configureDefaults(array $config)
199 + private function configureDefaults(array $config) : void
207 200 {
208 - $defaults = ['allow_redirects' => RedirectMiddleware::$defaultSettings, 'http_errors' => \true, 'decode_content' => \true, 'verify' => \true, 'cookies' => \false, 'idn_conversion' => \true];
201 + $defaults = ['allow_redirects' => RedirectMiddleware::$defaultSettings, 'http_errors' => \true, 'decode_content' => \true, 'verify' => \true, 'cookies' => \false, 'idn_conversion' => \false];
209 202 // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set.
210 203 // We can only trust the HTTP_PROXY environment variable in a CLI
211 204 // process due to the fact that PHP has no reliable mechanism to
212 205 // get environment variables that start with "HTTP_".
213 - if (\php_sapi_name() === 'cli' && \getenv('HTTP_PROXY')) {
214 - $defaults['proxy']['http'] = \getenv('HTTP_PROXY');
206 + if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) {
207 + $defaults['proxy']['http'] = $proxy;
215 208 }
216 - if ($proxy = \getenv('HTTPS_PROXY')) {
209 + if ($proxy = Utils::getenv('HTTPS_PROXY')) {
217 210 $defaults['proxy']['https'] = $proxy;
218 211 }
219 - if ($noProxy = \getenv('NO_PROXY')) {
212 + if ($noProxy = Utils::getenv('NO_PROXY')) {
220 213 $cleanedNoProxy = \str_replace(' ', '', $noProxy);
221 214 $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy);
222 215 }
223 216 $this->config = $config + $defaults;
@@ -225,9 +218,9 @@
225 218 $this->config['cookies'] = new CookieJar();
226 219 }
227 220 // Add the default user-agent header.
228 221 if (!isset($this->config['headers'])) {
229 - $this->config['headers'] = ['User-Agent' => default_user_agent()];
222 + $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()];
230 223 } else {
231 224 // Add the User-Agent header if one was not already set.
232 225 foreach (\array_keys($this->config['headers']) as $name) {
233 226 if (\strtolower($name) === 'user-agent') {
@@ -233,9 +226,9 @@
233 226 if (\strtolower($name) === 'user-agent') {
234 227 return;
235 228 }
236 229 }
237 - $this->config['headers']['User-Agent'] = default_user_agent();
230 + $this->config['headers']['User-Agent'] = Utils::defaultUserAgent();
238 231 }
239 232 }
240 233 /**
241 234 * Merges default options into the array.
@@ -240,12 +233,10 @@
240 233 /**
241 234 * Merges default options into the array.
242 235 *
243 236 * @param array $options Options to modify by reference
244 - *
245 - * @return array
246 237 */
247 - private function prepareDefaults(array $options)
238 + private function prepareDefaults(array $options) : array
248 239 {
249 240 $defaults = $this->config;
250 241 if (!empty($defaults['headers'])) {
251 242 // Default headers are only added if they are not present.
@@ -259,9 +250,9 @@
259 250 if ($options['headers'] === null) {
260 251 $defaults['_conditional'] = [];
261 252 unset($options['headers']);
262 253 } elseif (!\is_array($options['headers'])) {
263 - throw new \InvalidArgumentException('headers must be an array');
254 + throw new InvalidArgumentException('headers must be an array');
264 255 }
265 256 }
266 257 // Shallow merge defaults underneath options.
267 258 $result = $options + $defaults;
@@ -279,55 +270,41 @@
279 270 * The URI of the request is not modified and the request options are used
280 271 * as-is without merging in default options.
281 272 *
282 273 * @param array $options See \GuzzleHttp\RequestOptions.
283 - *
284 - * @return Promise\PromiseInterface
285 274 */
286 - private function transfer(RequestInterface $request, array $options)
275 + private function transfer(RequestInterface $request, array $options) : PromiseInterface
287 276 {
288 - // save_to -> sink
289 - if (isset($options['save_to'])) {
290 - $options['sink'] = $options['save_to'];
291 - unset($options['save_to']);
292 - }
293 - // exceptions -> http_errors
294 - if (isset($options['exceptions'])) {
295 - $options['http_errors'] = $options['exceptions'];
296 - unset($options['exceptions']);
297 - }
298 277 $request = $this->applyOptions($request, $options);
299 278 /** @var HandlerStack $handler */
300 279 $handler = $options['handler'];
301 280 try {
302 - return Promise\promise_for($handler($request, $options));
281 + return P\Create::promiseFor($handler($request, $options));
303 282 } catch (\Exception $e) {
304 - return Promise\rejection_for($e);
283 + return P\Create::rejectionFor($e);
305 284 }
306 285 }
307 286 /**
308 287 * Applies the array of request options to a request.
309 - *
310 - * @param RequestInterface $request
311 - * @param array $options
312 - *
313 - * @return RequestInterface
314 288 */
315 - private function applyOptions(RequestInterface $request, array &$options)
289 + private function applyOptions(RequestInterface $request, array &$options) : RequestInterface
316 290 {
317 291 $modify = ['set_headers' => []];
318 292 if (isset($options['headers'])) {
293 + if (\array_keys($options['headers']) === \range(0, \count($options['headers']) - 1)) {
294 + throw new InvalidArgumentException('The headers array must have header name as keys.');
295 + }
319 296 $modify['set_headers'] = $options['headers'];
320 297 unset($options['headers']);
321 298 }
322 299 if (isset($options['form_params'])) {
323 300 if (isset($options['multipart'])) {
324 - throw new \InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.');
301 + throw new InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.');
325 302 }
326 303 $options['body'] = \http_build_query($options['form_params'], '', '&');
327 304 unset($options['form_params']);
328 305 // Ensure that we don't have the header in different case and set the new value.
329 - $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
306 + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']);
330 307 $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
331 308 }
332 309 if (isset($options['multipart'])) {
333 310 $options['body'] = new Psr7\MultipartStream($options['multipart']);
@@ -333,24 +310,24 @@
333 310 $options['body'] = new Psr7\MultipartStream($options['multipart']);
334 311 unset($options['multipart']);
335 312 }
336 313 if (isset($options['json'])) {
337 - $options['body'] = \Dudlewebs\WPMCS\s3\GuzzleHttp\json_encode($options['json']);
314 + $options['body'] = Utils::jsonEncode($options['json']);
338 315 unset($options['json']);
339 316 // Ensure that we don't have the header in different case and set the new value.
340 - $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
317 + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']);
341 318 $options['_conditional']['Content-Type'] = 'application/json';
342 319 }
343 320 if (!empty($options['decode_content']) && $options['decode_content'] !== \true) {
344 321 // Ensure that we don't have the header in different case and set the new value.
345 - $options['_conditional'] = Psr7\_caseless_remove(['Accept-Encoding'], $options['_conditional']);
322 + $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']);
346 323 $modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
347 324 }
348 325 if (isset($options['body'])) {
349 326 if (\is_array($options['body'])) {
350 - $this->invalidBody();
327 + throw $this->invalidBody();
351 328 }
352 - $modify['body'] = Psr7\stream_for($options['body']);
329 + $modify['body'] = Psr7\Utils::streamFor($options['body']);
353 330 unset($options['body']);
354 331 }
355 332 if (!empty($options['auth']) && \is_array($options['auth'])) {
356 333 $value = $options['auth'];
@@ -357,9 +334,9 @@
357 334 $type = isset($value[2]) ? \strtolower($value[2]) : 'basic';
358 335 switch ($type) {
359 336 case 'basic':
360 337 // Ensure that we don't have the header in different case and set the new value.
361 - $modify['set_headers'] = Psr7\_caseless_remove(['Authorization'], $modify['set_headers']);
338 + $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']);
362 339 $modify['set_headers']['Authorization'] = 'Basic ' . \base64_encode("{$value[0]}:{$value[1]}");
363 340 break;
364 341 case 'digest':
365 342 // @todo: Do not rely on curl
@@ -374,12 +351,12 @@
374 351 }
375 352 if (isset($options['query'])) {
376 353 $value = $options['query'];
377 354 if (\is_array($value)) {
378 - $value = \http_build_query($value, null, '&', \PHP_QUERY_RFC3986);
355 + $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986);
379 356 }
380 357 if (!\is_string($value)) {
381 - throw new \InvalidArgumentException('query must be a string or array');
358 + throw new InvalidArgumentException('query must be a string or array');
382 359 }
383 360 $modify['query'] = $value;
384 361 unset($options['query']);
385 362 }
@@ -386,16 +363,19 @@
386 363 // Ensure that sink is not an invalid value.
387 364 if (isset($options['sink'])) {
388 365 // TODO: Add more sink validation?
389 366 if (\is_bool($options['sink'])) {
390 - throw new \InvalidArgumentException('sink must not be a boolean');
367 + throw new InvalidArgumentException('sink must not be a boolean');
391 368 }
392 369 }
393 - $request = Psr7\modify_request($request, $modify);
370 + if (isset($options['version'])) {
371 + $modify['version'] = $options['version'];
372 + }
373 + $request = Psr7\Utils::modifyRequest($request, $modify);
394 374 if ($request->getBody() instanceof Psr7\MultipartStream) {
395 375 // Use a multipart/form-data POST if a Content-Type is not set.
396 376 // Ensure that we don't have the header in different case and set the new value.
397 - $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
377 + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']);
398 378 $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary();
399 379 }
400 380 // Merge in conditional headers if they are not present.
401 381 if (isset($options['_conditional'])) {
@@ -405,9 +385,9 @@
405 385 if (!$request->hasHeader($k)) {
406 386 $modify['set_headers'][$k] = $v;
407 387 }
408 388 }
409 - $request = Psr7\modify_request($request, $modify);
389 + $request = Psr7\Utils::modifyRequest($request, $modify);
410 390 // Don't pass this internal value along to middleware/handlers.
411 391 unset($options['_conditional']);
412 392 }
413 393 return $request;
@@ -412,13 +392,11 @@
412 392 }
413 393 return $request;
414 394 }
415 395 /**
416 - * Throw Exception with pre-set message.
417 - * @return void
418 - * @throws \InvalidArgumentException Invalid body.
396 + * Return an InvalidArgumentException with pre-set message.
419 397 */
420 - private function invalidBody()
398 + private function invalidBody() : InvalidArgumentException
421 399 {
422 - throw new \InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a POST request has been deprecated. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.');
400 + return new InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a request is not supported. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.');
423 401 }
424 402 }