Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Promise;
|
Chris@0
|
3
|
Chris@0
|
4 /**
|
Chris@0
|
5 * Promises/A+ implementation that avoids recursion when possible.
|
Chris@0
|
6 *
|
Chris@0
|
7 * @link https://promisesaplus.com/
|
Chris@0
|
8 */
|
Chris@0
|
9 class Promise implements PromiseInterface
|
Chris@0
|
10 {
|
Chris@0
|
11 private $state = self::PENDING;
|
Chris@0
|
12 private $result;
|
Chris@0
|
13 private $cancelFn;
|
Chris@0
|
14 private $waitFn;
|
Chris@0
|
15 private $waitList;
|
Chris@0
|
16 private $handlers = [];
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @param callable $waitFn Fn that when invoked resolves the promise.
|
Chris@0
|
20 * @param callable $cancelFn Fn that when invoked cancels the promise.
|
Chris@0
|
21 */
|
Chris@0
|
22 public function __construct(
|
Chris@0
|
23 callable $waitFn = null,
|
Chris@0
|
24 callable $cancelFn = null
|
Chris@0
|
25 ) {
|
Chris@0
|
26 $this->waitFn = $waitFn;
|
Chris@0
|
27 $this->cancelFn = $cancelFn;
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 public function then(
|
Chris@0
|
31 callable $onFulfilled = null,
|
Chris@0
|
32 callable $onRejected = null
|
Chris@0
|
33 ) {
|
Chris@0
|
34 if ($this->state === self::PENDING) {
|
Chris@0
|
35 $p = new Promise(null, [$this, 'cancel']);
|
Chris@0
|
36 $this->handlers[] = [$p, $onFulfilled, $onRejected];
|
Chris@0
|
37 $p->waitList = $this->waitList;
|
Chris@0
|
38 $p->waitList[] = $this;
|
Chris@0
|
39 return $p;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 // Return a fulfilled promise and immediately invoke any callbacks.
|
Chris@0
|
43 if ($this->state === self::FULFILLED) {
|
Chris@0
|
44 return $onFulfilled
|
Chris@0
|
45 ? promise_for($this->result)->then($onFulfilled)
|
Chris@0
|
46 : promise_for($this->result);
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 // It's either cancelled or rejected, so return a rejected promise
|
Chris@0
|
50 // and immediately invoke any callbacks.
|
Chris@0
|
51 $rejection = rejection_for($this->result);
|
Chris@0
|
52 return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 public function otherwise(callable $onRejected)
|
Chris@0
|
56 {
|
Chris@0
|
57 return $this->then(null, $onRejected);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 public function wait($unwrap = true)
|
Chris@0
|
61 {
|
Chris@0
|
62 $this->waitIfPending();
|
Chris@0
|
63
|
Chris@0
|
64 $inner = $this->result instanceof PromiseInterface
|
Chris@0
|
65 ? $this->result->wait($unwrap)
|
Chris@0
|
66 : $this->result;
|
Chris@0
|
67
|
Chris@0
|
68 if ($unwrap) {
|
Chris@0
|
69 if ($this->result instanceof PromiseInterface
|
Chris@0
|
70 || $this->state === self::FULFILLED
|
Chris@0
|
71 ) {
|
Chris@0
|
72 return $inner;
|
Chris@0
|
73 } else {
|
Chris@0
|
74 // It's rejected so "unwrap" and throw an exception.
|
Chris@0
|
75 throw exception_for($inner);
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 public function getState()
|
Chris@0
|
81 {
|
Chris@0
|
82 return $this->state;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 public function cancel()
|
Chris@0
|
86 {
|
Chris@0
|
87 if ($this->state !== self::PENDING) {
|
Chris@0
|
88 return;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 $this->waitFn = $this->waitList = null;
|
Chris@0
|
92
|
Chris@0
|
93 if ($this->cancelFn) {
|
Chris@0
|
94 $fn = $this->cancelFn;
|
Chris@0
|
95 $this->cancelFn = null;
|
Chris@0
|
96 try {
|
Chris@0
|
97 $fn();
|
Chris@0
|
98 } catch (\Throwable $e) {
|
Chris@0
|
99 $this->reject($e);
|
Chris@0
|
100 } catch (\Exception $e) {
|
Chris@0
|
101 $this->reject($e);
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 // Reject the promise only if it wasn't rejected in a then callback.
|
Chris@0
|
106 if ($this->state === self::PENDING) {
|
Chris@0
|
107 $this->reject(new CancellationException('Promise has been cancelled'));
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 public function resolve($value)
|
Chris@0
|
112 {
|
Chris@0
|
113 $this->settle(self::FULFILLED, $value);
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 public function reject($reason)
|
Chris@0
|
117 {
|
Chris@0
|
118 $this->settle(self::REJECTED, $reason);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 private function settle($state, $value)
|
Chris@0
|
122 {
|
Chris@0
|
123 if ($this->state !== self::PENDING) {
|
Chris@0
|
124 // Ignore calls with the same resolution.
|
Chris@0
|
125 if ($state === $this->state && $value === $this->result) {
|
Chris@0
|
126 return;
|
Chris@0
|
127 }
|
Chris@0
|
128 throw $this->state === $state
|
Chris@0
|
129 ? new \LogicException("The promise is already {$state}.")
|
Chris@0
|
130 : new \LogicException("Cannot change a {$this->state} promise to {$state}");
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 if ($value === $this) {
|
Chris@0
|
134 throw new \LogicException('Cannot fulfill or reject a promise with itself');
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 // Clear out the state of the promise but stash the handlers.
|
Chris@0
|
138 $this->state = $state;
|
Chris@0
|
139 $this->result = $value;
|
Chris@0
|
140 $handlers = $this->handlers;
|
Chris@0
|
141 $this->handlers = null;
|
Chris@0
|
142 $this->waitList = $this->waitFn = null;
|
Chris@0
|
143 $this->cancelFn = null;
|
Chris@0
|
144
|
Chris@0
|
145 if (!$handlers) {
|
Chris@0
|
146 return;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 // If the value was not a settled promise or a thenable, then resolve
|
Chris@0
|
150 // it in the task queue using the correct ID.
|
Chris@0
|
151 if (!method_exists($value, 'then')) {
|
Chris@0
|
152 $id = $state === self::FULFILLED ? 1 : 2;
|
Chris@0
|
153 // It's a success, so resolve the handlers in the queue.
|
Chris@0
|
154 queue()->add(static function () use ($id, $value, $handlers) {
|
Chris@0
|
155 foreach ($handlers as $handler) {
|
Chris@0
|
156 self::callHandler($id, $value, $handler);
|
Chris@0
|
157 }
|
Chris@0
|
158 });
|
Chris@0
|
159 } elseif ($value instanceof Promise
|
Chris@0
|
160 && $value->getState() === self::PENDING
|
Chris@0
|
161 ) {
|
Chris@0
|
162 // We can just merge our handlers onto the next promise.
|
Chris@0
|
163 $value->handlers = array_merge($value->handlers, $handlers);
|
Chris@0
|
164 } else {
|
Chris@0
|
165 // Resolve the handlers when the forwarded promise is resolved.
|
Chris@0
|
166 $value->then(
|
Chris@0
|
167 static function ($value) use ($handlers) {
|
Chris@0
|
168 foreach ($handlers as $handler) {
|
Chris@0
|
169 self::callHandler(1, $value, $handler);
|
Chris@0
|
170 }
|
Chris@0
|
171 },
|
Chris@0
|
172 static function ($reason) use ($handlers) {
|
Chris@0
|
173 foreach ($handlers as $handler) {
|
Chris@0
|
174 self::callHandler(2, $reason, $handler);
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177 );
|
Chris@0
|
178 }
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * Call a stack of handlers using a specific callback index and value.
|
Chris@0
|
183 *
|
Chris@0
|
184 * @param int $index 1 (resolve) or 2 (reject).
|
Chris@0
|
185 * @param mixed $value Value to pass to the callback.
|
Chris@0
|
186 * @param array $handler Array of handler data (promise and callbacks).
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return array Returns the next group to resolve.
|
Chris@0
|
189 */
|
Chris@0
|
190 private static function callHandler($index, $value, array $handler)
|
Chris@0
|
191 {
|
Chris@0
|
192 /** @var PromiseInterface $promise */
|
Chris@0
|
193 $promise = $handler[0];
|
Chris@0
|
194
|
Chris@0
|
195 // The promise may have been cancelled or resolved before placing
|
Chris@0
|
196 // this thunk in the queue.
|
Chris@0
|
197 if ($promise->getState() !== self::PENDING) {
|
Chris@0
|
198 return;
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 try {
|
Chris@0
|
202 if (isset($handler[$index])) {
|
Chris@0
|
203 $promise->resolve($handler[$index]($value));
|
Chris@0
|
204 } elseif ($index === 1) {
|
Chris@0
|
205 // Forward resolution values as-is.
|
Chris@0
|
206 $promise->resolve($value);
|
Chris@0
|
207 } else {
|
Chris@0
|
208 // Forward rejections down the chain.
|
Chris@0
|
209 $promise->reject($value);
|
Chris@0
|
210 }
|
Chris@0
|
211 } catch (\Throwable $reason) {
|
Chris@0
|
212 $promise->reject($reason);
|
Chris@0
|
213 } catch (\Exception $reason) {
|
Chris@0
|
214 $promise->reject($reason);
|
Chris@0
|
215 }
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 private function waitIfPending()
|
Chris@0
|
219 {
|
Chris@0
|
220 if ($this->state !== self::PENDING) {
|
Chris@0
|
221 return;
|
Chris@0
|
222 } elseif ($this->waitFn) {
|
Chris@0
|
223 $this->invokeWaitFn();
|
Chris@0
|
224 } elseif ($this->waitList) {
|
Chris@0
|
225 $this->invokeWaitList();
|
Chris@0
|
226 } else {
|
Chris@0
|
227 // If there's not wait function, then reject the promise.
|
Chris@0
|
228 $this->reject('Cannot wait on a promise that has '
|
Chris@0
|
229 . 'no internal wait function. You must provide a wait '
|
Chris@0
|
230 . 'function when constructing the promise to be able to '
|
Chris@0
|
231 . 'wait on a promise.');
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 queue()->run();
|
Chris@0
|
235
|
Chris@0
|
236 if ($this->state === self::PENDING) {
|
Chris@0
|
237 $this->reject('Invoking the wait callback did not resolve the promise');
|
Chris@0
|
238 }
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 private function invokeWaitFn()
|
Chris@0
|
242 {
|
Chris@0
|
243 try {
|
Chris@0
|
244 $wfn = $this->waitFn;
|
Chris@0
|
245 $this->waitFn = null;
|
Chris@0
|
246 $wfn(true);
|
Chris@0
|
247 } catch (\Exception $reason) {
|
Chris@0
|
248 if ($this->state === self::PENDING) {
|
Chris@0
|
249 // The promise has not been resolved yet, so reject the promise
|
Chris@0
|
250 // with the exception.
|
Chris@0
|
251 $this->reject($reason);
|
Chris@0
|
252 } else {
|
Chris@0
|
253 // The promise was already resolved, so there's a problem in
|
Chris@0
|
254 // the application.
|
Chris@0
|
255 throw $reason;
|
Chris@0
|
256 }
|
Chris@0
|
257 }
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 private function invokeWaitList()
|
Chris@0
|
261 {
|
Chris@0
|
262 $waitList = $this->waitList;
|
Chris@0
|
263 $this->waitList = null;
|
Chris@0
|
264
|
Chris@0
|
265 foreach ($waitList as $result) {
|
Chris@0
|
266 while (true) {
|
Chris@0
|
267 $result->waitIfPending();
|
Chris@0
|
268
|
Chris@0
|
269 if ($result->result instanceof Promise) {
|
Chris@0
|
270 $result = $result->result;
|
Chris@0
|
271 } else {
|
Chris@0
|
272 if ($result->result instanceof PromiseInterface) {
|
Chris@0
|
273 $result->result->wait(false);
|
Chris@0
|
274 }
|
Chris@0
|
275 break;
|
Chris@0
|
276 }
|
Chris@0
|
277 }
|
Chris@0
|
278 }
|
Chris@0
|
279 }
|
Chris@0
|
280 }
|