PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Promise / Utils.php

Utils.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Promise/Utils.php

220 lines 7.9 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 Dudlewebs\WPMCS\s3\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(?TaskQueueInterface $assign = null) : TaskQueueInterface
24 {
25 static $queue;
26 if ($assign) {
27 $queue = $assign;
28 } elseif (!$queue) {
29 $queue = new 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) : PromiseInterface
40 {
41 $queue = self::queue();
42 $promise = new Promise([$queue, 'run']);
43 $queue->add(function () use($task, $promise) : void {
44 try {
45 if (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(PromiseInterface $promise) : array
67 {
68 try {
69 return ['state' => PromiseInterface::FULFILLED, 'value' => $promise->wait()];
70 } catch (RejectionException $e) {
71 return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
72 } catch (\Throwable $e) {
73 return ['state' => 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) : PromiseInterface
125 {
126 $results = [];
127 $promise = Each::of($promises, function ($value, $idx) use(&$results) : void {
128 $results[$idx] = $value;
129 }, function ($reason, $idx, Promise $aggregate) : void {
130 if (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 (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) : PromiseInterface
164 {
165 $results = [];
166 $rejections = [];
167 return Each::of($promises, function ($value, $idx, PromiseInterface $p) use(&$results, $count) : void {
168 if (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 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) : 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) : PromiseInterface
208 {
209 $results = [];
210 return Each::of($promises, function ($value, $idx) use(&$results) : void {
211 $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
212 }, function ($reason, $idx) use(&$results) : void {
213 $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
214 })->then(function () use(&$results) {
215 \ksort($results);
216 return $results;
217 });
218 }
219 }
220