PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
1.4.1 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 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Promise / Utils.php

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

218 lines 7.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 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 $aggregate->reject($reason);
131 })->then(function () use(&$results) {
132 \ksort($results);
133 return $results;
134 });
135 if (\true === $recursive) {
136 $promise = $promise->then(function ($results) use($recursive, &$promises) {
137 foreach ($promises as $promise) {
138 if (Is::pending($promise)) {
139 return self::all($promises, $recursive);
140 }
141 }
142 return $results;
143 });
144 }
145 return $promise;
146 }
147 /**
148 * Initiate a competitive race between multiple promises or values (values
149 * will become immediately fulfilled promises).
150 *
151 * When count amount of promises have been fulfilled, the returned promise
152 * is fulfilled with an array that contains the fulfillment values of the
153 * winners in order of resolution.
154 *
155 * This promise is rejected with a {@see AggregateException} if the number
156 * of fulfilled promises is less than the desired $count.
157 *
158 * @param int $count Total number of promises.
159 * @param mixed $promises Promises or values.
160 */
161 public static function some(int $count, $promises) : PromiseInterface
162 {
163 $results = [];
164 $rejections = [];
165 return Each::of($promises, function ($value, $idx, PromiseInterface $p) use(&$results, $count) : void {
166 if (Is::settled($p)) {
167 return;
168 }
169 $results[$idx] = $value;
170 if (\count($results) >= $count) {
171 $p->resolve(null);
172 }
173 }, function ($reason) use(&$rejections) : void {
174 $rejections[] = $reason;
175 })->then(function () use(&$results, &$rejections, $count) {
176 if (\count($results) !== $count) {
177 throw new AggregateException('Not enough promises to fulfill count', $rejections);
178 }
179 \ksort($results);
180 return \array_values($results);
181 });
182 }
183 /**
184 * Like some(), with 1 as count. However, if the promise fulfills, the
185 * fulfillment value is not an array of 1 but the value directly.
186 *
187 * @param mixed $promises Promises or values.
188 */
189 public static function any($promises) : PromiseInterface
190 {
191 return self::some(1, $promises)->then(function ($values) {
192 return $values[0];
193 });
194 }
195 /**
196 * Returns a promise that is fulfilled when all of the provided promises have
197 * been fulfilled or rejected.
198 *
199 * The returned promise is fulfilled with an array of inspection state arrays.
200 *
201 * @see inspect for the inspection state array format.
202 *
203 * @param mixed $promises Promises or values.
204 */
205 public static function settle($promises) : PromiseInterface
206 {
207 $results = [];
208 return Each::of($promises, function ($value, $idx) use(&$results) : void {
209 $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
210 }, function ($reason, $idx) use(&$results) : void {
211 $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
212 })->then(function () use(&$results) {
213 \ksort($results);
214 return $results;
215 });
216 }
217 }
218