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