Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
Línea -... Línea 2...
-
 
2
 
-
 
3
declare(strict_types=1);
2
 
4
 
Línea 3... Línea 5...
3
namespace GuzzleHttp\Promise;
5
namespace GuzzleHttp\Promise;
4
 
6
 
5
/**
7
/**
6
 * Promises/A+ implementation that avoids recursion when possible.
8
 * Promises/A+ implementation that avoids recursion when possible.
-
 
9
 *
-
 
10
 * @see https://promisesaplus.com/
7
 *
11
 *
8
 * @link https://promisesaplus.com/
12
 * @final
9
 */
13
 */
10
class Promise implements PromiseInterface
14
class Promise implements PromiseInterface
11
{
15
{
Línea 19... Línea 23...
19
    /**
23
    /**
20
     * @param callable $waitFn   Fn that when invoked resolves the promise.
24
     * @param callable $waitFn   Fn that when invoked resolves the promise.
21
     * @param callable $cancelFn Fn that when invoked cancels the promise.
25
     * @param callable $cancelFn Fn that when invoked cancels the promise.
22
     */
26
     */
23
    public function __construct(
27
    public function __construct(
24
        callable $waitFn = null,
28
        ?callable $waitFn = null,
25
        callable $cancelFn = null
29
        ?callable $cancelFn = null
26
    ) {
30
    ) {
27
        $this->waitFn = $waitFn;
31
        $this->waitFn = $waitFn;
28
        $this->cancelFn = $cancelFn;
32
        $this->cancelFn = $cancelFn;
29
    }
33
    }
Línea 30... Línea 34...
30
 
34
 
31
    public function then(
35
    public function then(
32
        callable $onFulfilled = null,
36
        ?callable $onFulfilled = null,
33
        callable $onRejected = null
37
        ?callable $onRejected = null
34
    ) {
38
    ): PromiseInterface {
35
        if ($this->state === self::PENDING) {
39
        if ($this->state === self::PENDING) {
36
            $p = new Promise(null, [$this, 'cancel']);
40
            $p = new Promise(null, [$this, 'cancel']);
37
            $this->handlers[] = [$p, $onFulfilled, $onRejected];
41
            $this->handlers[] = [$p, $onFulfilled, $onRejected];
38
            $p->waitList = $this->waitList;
42
            $p->waitList = $this->waitList;
-
 
43
            $p->waitList[] = $this;
39
            $p->waitList[] = $this;
44
 
40
            return $p;
45
            return $p;
Línea 41... Línea 46...
41
        }
46
        }
42
 
47
 
43
        // Return a fulfilled promise and immediately invoke any callbacks.
48
        // Return a fulfilled promise and immediately invoke any callbacks.
-
 
49
        if ($this->state === self::FULFILLED) {
44
        if ($this->state === self::FULFILLED) {
50
            $promise = Create::promiseFor($this->result);
45
            $promise = Create::promiseFor($this->result);
51
 
Línea 46... Línea 52...
46
            return $onFulfilled ? $promise->then($onFulfilled) : $promise;
52
            return $onFulfilled ? $promise->then($onFulfilled) : $promise;
47
        }
53
        }
48
 
54
 
-
 
55
        // It's either cancelled or rejected, so return a rejected promise
49
        // It's either cancelled or rejected, so return a rejected promise
56
        // and immediately invoke any callbacks.
50
        // and immediately invoke any callbacks.
57
        $rejection = Create::rejectionFor($this->result);
Línea 51... Línea 58...
51
        $rejection = Create::rejectionFor($this->result);
58
 
52
        return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
59
        return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
53
    }
60
    }
54
 
61
 
Línea 55... Línea 62...
55
    public function otherwise(callable $onRejected)
62
    public function otherwise(callable $onRejected): PromiseInterface
56
    {
63
    {
57
        return $this->then(null, $onRejected);
64
        return $this->then(null, $onRejected);
Línea 58... Línea 65...
58
    }
65
    }
59
 
66
 
Línea 71... Línea 78...
71
            // It's rejected so "unwrap" and throw an exception.
78
            // It's rejected so "unwrap" and throw an exception.
72
            throw Create::exceptionFor($this->result);
79
            throw Create::exceptionFor($this->result);
73
        }
80
        }
74
    }
81
    }
Línea 75... Línea 82...
75
 
82
 
76
    public function getState()
83
    public function getState(): string
77
    {
84
    {
78
        return $this->state;
85
        return $this->state;
Línea 79... Línea 86...
79
    }
86
    }
80
 
87
 
81
    public function cancel()
88
    public function cancel(): void
82
    {
89
    {
83
        if ($this->state !== self::PENDING) {
90
        if ($this->state !== self::PENDING) {
Línea 91... Línea 98...
91
            $this->cancelFn = null;
98
            $this->cancelFn = null;
92
            try {
99
            try {
93
                $fn();
100
                $fn();
94
            } catch (\Throwable $e) {
101
            } catch (\Throwable $e) {
95
                $this->reject($e);
102
                $this->reject($e);
96
            } catch (\Exception $e) {
-
 
97
                $this->reject($e);
-
 
98
            }
103
            }
99
        }
104
        }
Línea 100... Línea 105...
100
 
105
 
101
        // Reject the promise only if it wasn't rejected in a then callback.
106
        // Reject the promise only if it wasn't rejected in a then callback.
102
        /** @psalm-suppress RedundantCondition */
107
        /** @psalm-suppress RedundantCondition */
103
        if ($this->state === self::PENDING) {
108
        if ($this->state === self::PENDING) {
104
            $this->reject(new CancellationException('Promise has been cancelled'));
109
            $this->reject(new CancellationException('Promise has been cancelled'));
105
        }
110
        }
Línea 106... Línea 111...
106
    }
111
    }
107
 
112
 
108
    public function resolve($value)
113
    public function resolve($value): void
109
    {
114
    {
Línea 110... Línea 115...
110
        $this->settle(self::FULFILLED, $value);
115
        $this->settle(self::FULFILLED, $value);
111
    }
116
    }
112
 
117
 
113
    public function reject($reason)
118
    public function reject($reason): void
Línea 114... Línea 119...
114
    {
119
    {
115
        $this->settle(self::REJECTED, $reason);
120
        $this->settle(self::REJECTED, $reason);
116
    }
121
    }
117
 
122
 
118
    private function settle($state, $value)
123
    private function settle(string $state, $value): void
119
    {
124
    {
Línea 146... Línea 151...
146
        // If the value was not a settled promise or a thenable, then resolve
151
        // If the value was not a settled promise or a thenable, then resolve
147
        // it in the task queue using the correct ID.
152
        // it in the task queue using the correct ID.
148
        if (!is_object($value) || !method_exists($value, 'then')) {
153
        if (!is_object($value) || !method_exists($value, 'then')) {
149
            $id = $state === self::FULFILLED ? 1 : 2;
154
            $id = $state === self::FULFILLED ? 1 : 2;
150
            // It's a success, so resolve the handlers in the queue.
155
            // It's a success, so resolve the handlers in the queue.
151
            Utils::queue()->add(static function () use ($id, $value, $handlers) {
156
            Utils::queue()->add(static function () use ($id, $value, $handlers): void {
152
                foreach ($handlers as $handler) {
157
                foreach ($handlers as $handler) {
153
                    self::callHandler($id, $value, $handler);
158
                    self::callHandler($id, $value, $handler);
154
                }
159
                }
155
            });
160
            });
156
        } elseif ($value instanceof Promise && Is::pending($value)) {
161
        } elseif ($value instanceof Promise && Is::pending($value)) {
157
            // We can just merge our handlers onto the next promise.
162
            // We can just merge our handlers onto the next promise.
158
            $value->handlers = array_merge($value->handlers, $handlers);
163
            $value->handlers = array_merge($value->handlers, $handlers);
159
        } else {
164
        } else {
160
            // Resolve the handlers when the forwarded promise is resolved.
165
            // Resolve the handlers when the forwarded promise is resolved.
161
            $value->then(
166
            $value->then(
162
                static function ($value) use ($handlers) {
167
                static function ($value) use ($handlers): void {
163
                    foreach ($handlers as $handler) {
168
                    foreach ($handlers as $handler) {
164
                        self::callHandler(1, $value, $handler);
169
                        self::callHandler(1, $value, $handler);
165
                    }
170
                    }
166
                },
171
                },
167
                static function ($reason) use ($handlers) {
172
                static function ($reason) use ($handlers): void {
168
                    foreach ($handlers as $handler) {
173
                    foreach ($handlers as $handler) {
169
                        self::callHandler(2, $reason, $handler);
174
                        self::callHandler(2, $reason, $handler);
170
                    }
175
                    }
171
                }
176
                }
172
            );
177
            );
Línea 178... Línea 183...
178
     *
183
     *
179
     * @param int   $index   1 (resolve) or 2 (reject).
184
     * @param int   $index   1 (resolve) or 2 (reject).
180
     * @param mixed $value   Value to pass to the callback.
185
     * @param mixed $value   Value to pass to the callback.
181
     * @param array $handler Array of handler data (promise and callbacks).
186
     * @param array $handler Array of handler data (promise and callbacks).
182
     */
187
     */
183
    private static function callHandler($index, $value, array $handler)
188
    private static function callHandler(int $index, $value, array $handler): void
184
    {
189
    {
185
        /** @var PromiseInterface $promise */
190
        /** @var PromiseInterface $promise */
186
        $promise = $handler[0];
191
        $promise = $handler[0];
Línea 187... Línea 192...
187
 
192
 
Línea 209... Línea 214...
209
                // Forward rejections down the chain.
214
                // Forward rejections down the chain.
210
                $promise->reject($value);
215
                $promise->reject($value);
211
            }
216
            }
212
        } catch (\Throwable $reason) {
217
        } catch (\Throwable $reason) {
213
            $promise->reject($reason);
218
            $promise->reject($reason);
214
        } catch (\Exception $reason) {
-
 
215
            $promise->reject($reason);
-
 
216
        }
219
        }
217
    }
220
    }
Línea 218... Línea 221...
218
 
221
 
219
    private function waitIfPending()
222
    private function waitIfPending(): void
220
    {
223
    {
221
        if ($this->state !== self::PENDING) {
224
        if ($this->state !== self::PENDING) {
222
            return;
225
            return;
223
        } elseif ($this->waitFn) {
226
        } elseif ($this->waitFn) {
224
            $this->invokeWaitFn();
227
            $this->invokeWaitFn();
225
        } elseif ($this->waitList) {
228
        } elseif ($this->waitList) {
226
            $this->invokeWaitList();
229
            $this->invokeWaitList();
227
        } else {
230
        } else {
228
            // If there's no wait function, then reject the promise.
231
            // If there's no wait function, then reject the promise.
229
            $this->reject('Cannot wait on a promise that has '
232
            $this->reject('Cannot wait on a promise that has '
230
                . 'no internal wait function. You must provide a wait '
233
                .'no internal wait function. You must provide a wait '
231
                . 'function when constructing the promise to be able to '
234
                .'function when constructing the promise to be able to '
232
                . 'wait on a promise.');
235
                .'wait on a promise.');
Línea 233... Línea 236...
233
        }
236
        }
Línea 234... Línea 237...
234
 
237
 
235
        Utils::queue()->run();
238
        Utils::queue()->run();
236
 
239
 
237
        /** @psalm-suppress RedundantCondition */
240
        /** @psalm-suppress RedundantCondition */
238
        if ($this->state === self::PENDING) {
241
        if ($this->state === self::PENDING) {
Línea 239... Línea 242...
239
            $this->reject('Invoking the wait callback did not resolve the promise');
242
            $this->reject('Invoking the wait callback did not resolve the promise');
240
        }
243
        }
241
    }
244
    }
242
 
245
 
243
    private function invokeWaitFn()
246
    private function invokeWaitFn(): void
244
    {
247
    {
245
        try {
248
        try {
246
            $wfn = $this->waitFn;
249
            $wfn = $this->waitFn;
247
            $this->waitFn = null;
250
            $this->waitFn = null;
248
            $wfn(true);
251
            $wfn(true);
249
        } catch (\Exception $reason) {
252
        } catch (\Throwable $reason) {
250
            if ($this->state === self::PENDING) {
253
            if ($this->state === self::PENDING) {
Línea 257... Línea 260...
257
                throw $reason;
260
                throw $reason;
258
            }
261
            }
259
        }
262
        }
260
    }
263
    }
Línea 261... Línea 264...
261
 
264
 
262
    private function invokeWaitList()
265
    private function invokeWaitList(): void
263
    {
266
    {
264
        $waitList = $this->waitList;
267
        $waitList = $this->waitList;
Línea 265... Línea 268...
265
        $this->waitList = null;
268
        $this->waitList = null;