PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Pool.php

Pool.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.7, at vendor_prefixed/guzzlehttp/guzzle/src/Pool.php

117 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $iterable = \YoastSEO_Vendor\GuzzleHttp\Promise\Create::iterFor($requests);
51 $requests = static function () use($iterable, $client, $opts) {
52 foreach ($iterable as $key => $rfn) {
53 if ($rfn instanceof \YoastSEO_Vendor\Psr\Http\Message\RequestInterface) {
54 (yield $key => $client->sendAsync($rfn, $opts));
55 } elseif (\is_callable($rfn)) {
56 (yield $key => $rfn($opts));
57 } else {
58 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.');
59 }
60 }
61 };
62 $this->each = new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($requests(), $config);
63 }
64 /**
65 * Get promise
66 */
67 public function promise() : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
68 {
69 return $this->each->promise();
70 }
71 /**
72 * Sends multiple requests concurrently and returns an array of responses
73 * and exceptions that uses the same ordering as the provided requests.
74 *
75 * IMPORTANT: This method keeps every request and response in memory, and
76 * as such, is NOT recommended when sending a large number or an
77 * indeterminate number of requests concurrently.
78 *
79 * @param ClientInterface $client Client used to send the requests
80 * @param array|\Iterator $requests Requests to send concurrently.
81 * @param array $options Passes through the options available in
82 * {@see Pool::__construct}
83 *
84 * @return array Returns an array containing the response or an exception
85 * in the same order that the requests were sent.
86 *
87 * @throws \InvalidArgumentException if the event format is incorrect.
88 */
89 public static function batch(\YoastSEO_Vendor\GuzzleHttp\ClientInterface $client, $requests, array $options = []) : array
90 {
91 $res = [];
92 self::cmpCallback($options, 'fulfilled', $res);
93 self::cmpCallback($options, 'rejected', $res);
94 $pool = new static($client, $requests, $options);
95 $pool->promise()->wait();
96 \ksort($res);
97 return $res;
98 }
99 /**
100 * Execute callback(s)
101 */
102 private static function cmpCallback(array &$options, string $name, array &$results) : void
103 {
104 if (!isset($options[$name])) {
105 $options[$name] = static function ($v, $k) use(&$results) {
106 $results[$k] = $v;
107 };
108 } else {
109 $currentFn = $options[$name];
110 $options[$name] = static function ($v, $k) use(&$results, $currentFn) {
111 $currentFn($v, $k);
112 $results[$k] = $v;
113 };
114 }
115 }
116 }
117