PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
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.2.12, at includes/sdk/s3/GuzzleHttp/Promise/Utils.php

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