| @@ -1,120 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace YoastSEO_Vendor\GuzzleHttp; | |
| 4 | - | |
| 5 | -use YoastSEO_Vendor\GuzzleHttp\Promise as P; | |
| 6 | -use YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise; | |
| 7 | -use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface; | |
| 8 | -use YoastSEO_Vendor\GuzzleHttp\Promise\PromisorInterface; | |
| 9 | -use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; | |
| 10 | -/** | |
| 11 | - * Sends an iterator of requests concurrently using a capped pool size. | |
| 12 | - * | |
| 13 | - * The pool will read from an iterator until it is cancelled or until the | |
| 14 | - * iterator is consumed. When a request is yielded, the request is sent after | |
| 15 | - * applying the "request_options" request options (if provided in the ctor). | |
| 16 | - * | |
| 17 | - * When a function is yielded by the iterator, the function is provided the | |
| 18 | - * "request_options" array that should be merged on top of any existing | |
| 19 | - * options, and the function MUST then return a wait-able promise. | |
| 20 | - * | |
| 21 | - * @final | |
| 22 | - */ | |
| 23 | -class Pool implements \YoastSEO_Vendor\GuzzleHttp\Promise\PromisorInterface | |
| 24 | -{ | |
| 25 | - /** | |
| 26 | - * @var EachPromise | |
| 27 | - */ | |
| 28 | - private $each; | |
| 29 | - /** | |
| 30 | - * @param ClientInterface $client Client used to send the requests. | |
| 31 | - * @param array|\Iterator $requests Requests or functions that return | |
| 32 | - * requests to send concurrently. | |
| 33 | - * @param array $config Associative array of options | |
| 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. | |
| 38 | - */ | |
| 39 | - public function __construct(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $config = []) | |
| 40 | - { | |
| 41 | - if (!isset($config['concurrency'])) { | |
| 42 | - $config['concurrency'] = 25; | |
| 43 | - } | |
| 44 | - if (isset($config['options'])) { | |
| 45 | - $opts = $config['options']; | |
| 46 | - unset($config['options']); | |
| 47 | - } else { | |
| 48 | - $opts = []; | |
| 49 | - } | |
| 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) { | |
| 56 | - foreach ($iterable as $key => $rfn) { | |
| 57 | - if ($rfn instanceof \YoastSEO_Vendor\Psr\Http\Message\RequestInterface) { | |
| 58 | - (yield $key => $client->sendAsync($rfn, $opts)); | |
| 59 | - } elseif (\is_callable($rfn)) { | |
| 60 | - (yield $key => $rfn($opts)); | |
| 61 | - } else { | |
| 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.'); | |
| 63 | - } | |
| 64 | - } | |
| 65 | - }; | |
| 66 | - $this->each = new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($requests(), $config); | |
| 67 | - } | |
| 68 | - /** | |
| 69 | - * Get promise | |
| 70 | - */ | |
| 71 | - public function promise() : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 72 | - { | |
| 73 | - return $this->each->promise(); | |
| 74 | - } | |
| 75 | - /** | |
| 76 | - * Sends multiple requests concurrently and returns an array of responses | |
| 77 | - * and exceptions that uses the same ordering as the provided requests. | |
| 78 | - * | |
| 79 | - * IMPORTANT: This method keeps every request and response in memory, and | |
| 80 | - * as such, is NOT recommended when sending a large number or an | |
| 81 | - * indeterminate number of requests concurrently. | |
| 82 | - * | |
| 83 | - * @param ClientInterface $client Client used to send the requests | |
| 84 | - * @param array|\Iterator $requests Requests to send concurrently. | |
| 85 | - * @param array $options Passes through the options available in | |
| 86 | - * {@see Pool::__construct} | |
| 87 | - * | |
| 88 | - * @return array Returns an array containing the response or an exception | |
| 89 | - * in the same order that the requests were sent. | |
| 90 | - * | |
| 91 | - * @throws \InvalidArgumentException if the event format is incorrect. | |
| 92 | - */ | |
| 93 | - public static function batch(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $options = []) : array | |
| 94 | - { | |
| 95 | - $res = []; | |
| 96 | - self::cmpCallback($options, 'fulfilled', $res); | |
| 97 | - self::cmpCallback($options, 'rejected', $res); | |
| 98 | - $pool = new static($client, $requests, $options); | |
| 99 | - $pool->promise()->wait(); | |
| 100 | - \ksort($res); | |
| 101 | - return $res; | |
| 102 | - } | |
| 103 | - /** | |
| 104 | - * Execute callback(s) | |
| 105 | - */ | |
| 106 | - private static function cmpCallback(array &$options, string $name, array &$results) : void | |
| 107 | - { | |
| 108 | - if (!isset($options[$name])) { | |
| 109 | - $options[$name] = static function ($v, $k) use(&$results) { | |
| 110 | - $results[$k] = $v; | |
| 111 | - }; | |
| 112 | - } else { | |
| 113 | - $currentFn = $options[$name]; | |
| 114 | - $options[$name] = static function ($v, $k) use(&$results, $currentFn) { | |
| 115 | - $currentFn($v, $k); | |
| 116 | - $results[$k] = $v; | |
| 117 | - }; | |
| 118 | - } | |
| 119 | - } | |
| 120 | -} | |