PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/Pool.php

121 lines 5.2 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 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 }
121