PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 / promises / src / Utils.php

Utils.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at vendor_prefixed/guzzlehttp/promises/src/Utils.php

220 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace YoastSEO_Vendor\GuzzleHttp\Promise;
5
6 final class Utils
7 {
8 /**
9 * Get the global task queue used for promise resolution.
10 *
11 * This task queue MUST be run in an event loop in order for promises to be
12 * settled asynchronously. It will be automatically run when synchronously
13 * waiting on a promise.
14 *
15 * <code>
16 * while ($eventLoop->isRunning()) {
17 * GuzzleHttp\Promise\Utils::queue()->run();
18 * }
19 * </code>
20 *
21 * @param TaskQueueInterface|null $assign Optionally specify a new queue instance.
22 */
23 public static function queue(?\YoastSEO_Vendor\GuzzleHttp\Promise\TaskQueueInterface $assign = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\TaskQueueInterface
24 {
25 static $queue;
26 if ($assign) {
27 $queue = $assign;
28 } elseif (!$queue) {
29 $queue = new \YoastSEO_Vendor\GuzzleHttp\Promise\TaskQueue();
30 }
31 return $queue;
32 }
33 /**
34 * Adds a function to run in the task queue when it is next `run()` and
35 * returns a promise that is fulfilled or rejected with the result.
36 *
37 * @param callable $task Task function to run.
38 */
39 public static function task(callable $task) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
40 {
41 $queue = self::queue();
42 $promise = new \YoastSEO_Vendor\GuzzleHttp\Promise\Promise([$queue, 'run']);
43 $queue->add(function () use($task, $promise) : void {
44 try {
45 if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::pending($promise)) {
46 $promise->resolve($task());
47 }
48 } catch (\Throwable $e) {
49 $promise->reject($e);
50 }
51 });
52 return $promise;
53 }
54 /**
55 * Synchronously waits on a promise to resolve and returns an inspection
56 * state array.
57 *
58 * Returns a state associative array containing a "state" key mapping to a
59 * valid promise state. If the state of the promise is "fulfilled", the
60 * array will contain a "value" key mapping to the fulfilled value of the
61 * promise. If the promise is rejected, the array will contain a "reason"
62 * key mapping to the rejection reason of the promise.
63 *
64 * @param PromiseInterface $promise Promise or value.
65 */
66 public static function inspect(\YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $promise) : array
67 {
68 try {
69 return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::FULFILLED, 'value' => $promise->wait()];
70 } catch (\YoastSEO_Vendor\GuzzleHttp\Promise\RejectionException $e) {
71 return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e->getReason()];
72 } catch (\Throwable $e) {
73 return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e];
74 }
75 }
76 /**
77 * Waits on all of the provided promises, but does not unwrap rejected
78 * promises as thrown exception.
79 *
80 * Returns an array of inspection state arrays.
81 *
82 * @see inspect for the inspection state array format.
83 *
84 * @param PromiseInterface[] $promises Traversable of promises to wait upon.
85 */
86 public static function inspectAll($promises) : array
87 {
88 $results = [];
89 foreach ($promises as $key => $promise) {
90 $results[$key] = self::inspect($promise);
91 }
92 return $results;
93 }
94 /**
95 * Waits on all of the provided promises and returns the fulfilled values.
96 *
97 * Returns an array that contains the value of each promise (in the same
98 * order the promises were provided). An exception is thrown if any of the
99 * promises are rejected.
100 *
101 * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
102 *
103 * @throws \Throwable on error
104 */
105 public static function unwrap($promises) : array
106 {
107 $results = [];
108 foreach ($promises as $key => $promise) {
109 $results[$key] = $promise->wait();
110 }
111 return $results;
112 }
113 /**
114 * Given an array of promises, return a promise that is fulfilled when all
115 * the items in the array are fulfilled.
116 *
117 * The promise's fulfillment value is an array with fulfillment values at
118 * respective positions to the original array. If any promise in the array
119 * rejects, the returned promise is rejected with the rejection reason.
120 *
121 * @param mixed $promises Promises or values.
122 * @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
123 */
124 public static function all($promises, bool $recursive = \false) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
125 {
126 $results = [];
127 $promise = \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) : void {
128 $results[$idx] = $value;
129 }, function ($reason, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\Promise $aggregate) : void {
130 if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::pending($aggregate)) {
131 $aggregate->reject($reason);
132 }
133 })->then(function () use(&$results) {
134 \ksort($results);
135 return $results;
136 });
137 if (\true === $recursive) {
138 $promise = $promise->then(function ($results) use($recursive, &$promises) {
139 foreach ($promises as $promise) {
140 if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::pending($promise)) {
141 return self::all($promises, $recursive);
142 }
143 }
144 return $results;
145 });
146 }
147 return $promise;
148 }
149 /**
150 * Initiate a competitive race between multiple promises or values (values
151 * will become immediately fulfilled promises).
152 *
153 * When count amount of promises have been fulfilled, the returned promise
154 * is fulfilled with an array that contains the fulfillment values of the
155 * winners in order of resolution.
156 *
157 * This promise is rejected with a {@see AggregateException} if the number
158 * of fulfilled promises is less than the desired $count.
159 *
160 * @param int $count Total number of promises.
161 * @param mixed $promises Promises or values.
162 */
163 public static function some(int $count, $promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
164 {
165 $results = [];
166 $rejections = [];
167 return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $p) use(&$results, $count) : void {
168 if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::settled($p)) {
169 return;
170 }
171 $results[$idx] = $value;
172 if (\count($results) >= $count) {
173 $p->resolve(null);
174 }
175 }, function ($reason) use(&$rejections) : void {
176 $rejections[] = $reason;
177 })->then(function () use(&$results, &$rejections, $count) {
178 if (\count($results) !== $count) {
179 throw new \YoastSEO_Vendor\GuzzleHttp\Promise\AggregateException('Not enough promises to fulfill count', $rejections);
180 }
181 \ksort($results);
182 return \array_values($results);
183 });
184 }
185 /**
186 * Like some(), with 1 as count. However, if the promise fulfills, the
187 * fulfillment value is not an array of 1 but the value directly.
188 *
189 * @param mixed $promises Promises or values.
190 */
191 public static function any($promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
192 {
193 return self::some(1, $promises)->then(function ($values) {
194 return $values[0];
195 });
196 }
197 /**
198 * Returns a promise that is fulfilled when all of the provided promises have
199 * been fulfilled or rejected.
200 *
201 * The returned promise is fulfilled with an array of inspection state arrays.
202 *
203 * @see inspect for the inspection state array format.
204 *
205 * @param mixed $promises Promises or values.
206 */
207 public static function settle($promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
208 {
209 $results = [];
210 return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) : void {
211 $results[$idx] = ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::FULFILLED, 'value' => $value];
212 }, function ($reason, $idx) use(&$results) : void {
213 $results[$idx] = ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $reason];
214 })->then(function () use(&$results) {
215 \ksort($results);
216 return $results;
217 });
218 }
219 }
220