| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace YoastSEO_Vendor\GuzzleHttp; |
| 4 | 4 | |
| 5 | +use YoastSEO_Vendor\GuzzleHttp\Promise as P; | |
| 5 | 6 | use YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise; |
| 6 | 7 | use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface; |
| 7 | 8 | use YoastSEO_Vendor\GuzzleHttp\Promise\PromisorInterface; |
| 8 | 9 | use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; |
| @@ -15,12 +16,16 @@ | ||
| 15 | 16 | * |
| 16 | 17 | * When a function is yielded by the iterator, the function is provided the |
| 17 | 18 | * "request_options" array that should be merged on top of any existing |
| 18 | 19 | * options, and the function MUST then return a wait-able promise. |
| 20 | + * | |
| 21 | + * @final | |
| 19 | 22 | */ |
| 20 | 23 | class Pool implements \YoastSEO_Vendor\GuzzleHttp\Promise\PromisorInterface |
| 21 | 24 | { |
| 22 | - /** @var EachPromise */ | |
| 25 | + /** | |
| 26 | + * @var EachPromise | |
| 27 | + */ | |
| 23 | 28 | private $each; |
| 24 | 29 | /** |
| 25 | 30 | * @param ClientInterface $client Client used to send the requests. |
| 26 | 31 | * @param array|\Iterator $requests Requests or functions that return |
| @@ -25,19 +30,16 @@ | ||
| 25 | 30 | * @param ClientInterface $client Client used to send the requests. |
| 26 | 31 | * @param array|\Iterator $requests Requests or functions that return |
| 27 | 32 | * requests to send concurrently. |
| 28 | 33 | * @param array $config Associative array of options |
| 29 | - * - concurrency: (int) Maximum number of requests to send concurrently | |
| 30 | - * - options: Array of request options to apply to each request. | |
| 31 | - * - fulfilled: (callable) Function to invoke when a request completes. | |
| 32 | - * - rejected: (callable) Function to invoke when a request is rejected. | |
| 34 | + * - concurrency: (int) Maximum number of requests to send concurrently | |
| 35 | + * - options: Array of request options to apply to each request. | |
| 36 | + * - fulfilled: (callable) Function to invoke when a request completes. | |
| 37 | + * - rejected: (callable) Function to invoke when a request is rejected. | |
| 33 | 38 | */ |
| 34 | 39 | public function __construct(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $config = []) |
| 35 | 40 | { |
| 36 | - // Backwards compatibility. | |
| 37 | - if (isset($config['pool_size'])) { | |
| 38 | - $config['concurrency'] = $config['pool_size']; | |
| 39 | - } elseif (!isset($config['concurrency'])) { | |
| 41 | + if (!isset($config['concurrency'])) { | |
| 40 | 42 | $config['concurrency'] = 25; |
| 41 | 43 | } |
| 42 | 44 | if (isset($config['options'])) { |
| 43 | 45 | $opts = $config['options']; |
| @@ -44,10 +46,14 @@ | ||
| 44 | 46 | unset($config['options']); |
| 45 | 47 | } else { |
| 46 | 48 | $opts = []; |
| 47 | 49 | } |
| 48 | - $iterable = \YoastSEO_Vendor\GuzzleHttp\Promise\iter_for($requests); | |
| 49 | - $requests = function () use($iterable, $client, $opts) { | |
| 50 | + if (!\is_iterable($requests)) { | |
| 51 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/guzzle', '7.11', 'Passing a non-iterable request collection to %s::__construct() or %s::batch() is deprecated; guzzlehttp/guzzle 8.0 will require an iterable.', __CLASS__, __CLASS__); | |
| 52 | + $requests = [$requests]; | |
| 53 | + } | |
| 54 | + $iterable = \YoastSEO_Vendor\GuzzleHttp\Promise\Create::iterFor($requests); | |
| 55 | + $requests = static function () use($iterable, $client, $opts) { | |
| 50 | 56 | foreach ($iterable as $key => $rfn) { |
| 51 | 57 | if ($rfn instanceof \YoastSEO_Vendor\Psr\Http\Message\RequestInterface) { |
| 52 | 58 | (yield $key => $client->sendAsync($rfn, $opts)); |
| 53 | 59 | } elseif (\is_callable($rfn)) { |
| @@ -52,9 +58,9 @@ | ||
| 52 | 58 | (yield $key => $client->sendAsync($rfn, $opts)); |
| 53 | 59 | } elseif (\is_callable($rfn)) { |
| 54 | 60 | (yield $key => $rfn($opts)); |
| 55 | 61 | } else { |
| 56 | - throw new \InvalidArgumentException('Each value yielded by ' . 'the iterator must be a Psr7\\Http\\Message\\RequestInterface ' . 'or a callable that returns a promise that fulfills ' . 'with a Psr7\\Message\\Http\\ResponseInterface object.'); | |
| 62 | + throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr\\Http\\Message\\RequestInterface or a callable that returns a promise that fulfills with a Psr\\Http\\Message\\ResponseInterface object.'); | |
| 57 | 63 | } |
| 58 | 64 | } |
| 59 | 65 | }; |
| 60 | 66 | $this->each = new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($requests(), $config); |
| @@ -60,12 +66,10 @@ | ||
| 60 | 66 | $this->each = new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($requests(), $config); |
| 61 | 67 | } |
| 62 | 68 | /** |
| 63 | 69 | * Get promise |
| 64 | - * | |
| 65 | - * @return PromiseInterface | |
| 66 | 70 | */ |
| 67 | - public function promise() | |
| 71 | + public function promise() : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 68 | 72 | { |
| 69 | 73 | return $this->each->promise(); |
| 70 | 74 | } |
| 71 | 75 | /** |
| @@ -78,15 +82,16 @@ | ||
| 78 | 82 | * |
| 79 | 83 | * @param ClientInterface $client Client used to send the requests |
| 80 | 84 | * @param array|\Iterator $requests Requests to send concurrently. |
| 81 | 85 | * @param array $options Passes through the options available in |
| 82 | - * {@see GuzzleHttp\Pool::__construct} | |
| 86 | + * {@see Pool::__construct} | |
| 83 | 87 | * |
| 84 | 88 | * @return array Returns an array containing the response or an exception |
| 85 | 89 | * in the same order that the requests were sent. |
| 90 | + * | |
| 86 | 91 | * @throws \InvalidArgumentException if the event format is incorrect. |
| 87 | 92 | */ |
| 88 | - public static function batch(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $options = []) | |
| 93 | + public static function batch(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $options = []) : array | |
| 89 | 94 | { |
| 90 | 95 | $res = []; |
| 91 | 96 | self::cmpCallback($options, 'fulfilled', $res); |
| 92 | 97 | self::cmpCallback($options, 'rejected', $res); |
| @@ -96,20 +101,18 @@ | ||
| 96 | 101 | return $res; |
| 97 | 102 | } |
| 98 | 103 | /** |
| 99 | 104 | * Execute callback(s) |
| 100 | - * | |
| 101 | - * @return void | |
| 102 | 105 | */ |
| 103 | - private static function cmpCallback(array &$options, $name, array &$results) | |
| 106 | + private static function cmpCallback(array &$options, string $name, array &$results) : void | |
| 104 | 107 | { |
| 105 | 108 | if (!isset($options[$name])) { |
| 106 | - $options[$name] = function ($v, $k) use(&$results) { | |
| 109 | + $options[$name] = static function ($v, $k) use(&$results) { | |
| 107 | 110 | $results[$k] = $v; |
| 108 | 111 | }; |
| 109 | 112 | } else { |
| 110 | 113 | $currentFn = $options[$name]; |
| 111 | - $options[$name] = function ($v, $k) use(&$results, $currentFn) { | |
| 114 | + $options[$name] = static function ($v, $k) use(&$results, $currentFn) { | |
| 112 | 115 | $currentFn($v, $k); |
| 113 | 116 | $results[$k] = $v; |
| 114 | 117 | }; |
| 115 | 118 | } |