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
final class Utils
7
final class Utils
Línea 15... Línea 17...
15
     * while ($eventLoop->isRunning()) {
17
     * while ($eventLoop->isRunning()) {
16
     *     GuzzleHttp\Promise\Utils::queue()->run();
18
     *     GuzzleHttp\Promise\Utils::queue()->run();
17
     * }
19
     * }
18
     * </code>
20
     * </code>
19
     *
21
     *
20
     * @param TaskQueueInterface $assign Optionally specify a new queue instance.
22
     * @param TaskQueueInterface|null $assign Optionally specify a new queue instance.
21
     *
-
 
22
     * @return TaskQueueInterface
-
 
23
     */
23
     */
24
    public static function queue(TaskQueueInterface $assign = null)
24
    public static function queue(?TaskQueueInterface $assign = null): TaskQueueInterface
25
    {
25
    {
26
        static $queue;
26
        static $queue;
Línea 27... Línea 27...
27
 
27
 
28
        if ($assign) {
28
        if ($assign) {
Línea 37... Línea 37...
37
    /**
37
    /**
38
     * Adds a function to run in the task queue when it is next `run()` and
38
     * Adds a function to run in the task queue when it is next `run()` and
39
     * returns a promise that is fulfilled or rejected with the result.
39
     * returns a promise that is fulfilled or rejected with the result.
40
     *
40
     *
41
     * @param callable $task Task function to run.
41
     * @param callable $task Task function to run.
42
     *
-
 
43
     * @return PromiseInterface
-
 
44
     */
42
     */
45
    public static function task(callable $task)
43
    public static function task(callable $task): PromiseInterface
46
    {
44
    {
47
        $queue = self::queue();
45
        $queue = self::queue();
48
        $promise = new Promise([$queue, 'run']);
46
        $promise = new Promise([$queue, 'run']);
49
        $queue->add(function () use ($task, $promise) {
47
        $queue->add(function () use ($task, $promise): void {
50
            try {
48
            try {
51
                if (Is::pending($promise)) {
49
                if (Is::pending($promise)) {
52
                    $promise->resolve($task());
50
                    $promise->resolve($task());
53
                }
51
                }
54
            } catch (\Throwable $e) {
52
            } catch (\Throwable $e) {
55
                $promise->reject($e);
53
                $promise->reject($e);
56
            } catch (\Exception $e) {
-
 
57
                $promise->reject($e);
-
 
58
            }
54
            }
59
        });
55
        });
Línea 60... Línea 56...
60
 
56
 
61
        return $promise;
57
        return $promise;
Línea 70... Línea 66...
70
     * array will contain a "value" key mapping to the fulfilled value of the
66
     * array will contain a "value" key mapping to the fulfilled value of the
71
     * promise. If the promise is rejected, the array will contain a "reason"
67
     * promise. If the promise is rejected, the array will contain a "reason"
72
     * key mapping to the rejection reason of the promise.
68
     * key mapping to the rejection reason of the promise.
73
     *
69
     *
74
     * @param PromiseInterface $promise Promise or value.
70
     * @param PromiseInterface $promise Promise or value.
75
     *
-
 
76
     * @return array
-
 
77
     */
71
     */
78
    public static function inspect(PromiseInterface $promise)
72
    public static function inspect(PromiseInterface $promise): array
79
    {
73
    {
80
        try {
74
        try {
81
            return [
75
            return [
82
                'state' => PromiseInterface::FULFILLED,
76
                'state' => PromiseInterface::FULFILLED,
83
                'value' => $promise->wait()
77
                'value' => $promise->wait(),
84
            ];
78
            ];
85
        } catch (RejectionException $e) {
79
        } catch (RejectionException $e) {
86
            return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
80
            return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
87
        } catch (\Throwable $e) {
81
        } catch (\Throwable $e) {
88
            return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
82
            return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
89
        } catch (\Exception $e) {
-
 
90
            return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
-
 
91
        }
83
        }
92
    }
84
    }
Línea 93... Línea 85...
93
 
85
 
94
    /**
86
    /**
Línea 98... Línea 90...
98
     * Returns an array of inspection state arrays.
90
     * Returns an array of inspection state arrays.
99
     *
91
     *
100
     * @see inspect for the inspection state array format.
92
     * @see inspect for the inspection state array format.
101
     *
93
     *
102
     * @param PromiseInterface[] $promises Traversable of promises to wait upon.
94
     * @param PromiseInterface[] $promises Traversable of promises to wait upon.
103
     *
-
 
104
     * @return array
-
 
105
     */
95
     */
106
    public static function inspectAll($promises)
96
    public static function inspectAll($promises): array
107
    {
97
    {
108
        $results = [];
98
        $results = [];
109
        foreach ($promises as $key => $promise) {
99
        foreach ($promises as $key => $promise) {
110
            $results[$key] = inspect($promise);
100
            $results[$key] = self::inspect($promise);
111
        }
101
        }
Línea 112... Línea 102...
112
 
102
 
113
        return $results;
103
        return $results;
Línea 120... Línea 110...
120
     * order the promises were provided). An exception is thrown if any of the
110
     * order the promises were provided). An exception is thrown if any of the
121
     * promises are rejected.
111
     * promises are rejected.
122
     *
112
     *
123
     * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
113
     * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
124
     *
114
     *
125
     * @return array
-
 
126
     *
-
 
127
     * @throws \Exception on error
115
     * @throws \Throwable on error
128
     * @throws \Throwable on error in PHP >=7
-
 
129
     */
116
     */
130
    public static function unwrap($promises)
117
    public static function unwrap($promises): array
131
    {
118
    {
132
        $results = [];
119
        $results = [];
133
        foreach ($promises as $key => $promise) {
120
        foreach ($promises as $key => $promise) {
134
            $results[$key] = $promise->wait();
121
            $results[$key] = $promise->wait();
135
        }
122
        }
Línea 145... Línea 132...
145
     * respective positions to the original array. If any promise in the array
132
     * respective positions to the original array. If any promise in the array
146
     * rejects, the returned promise is rejected with the rejection reason.
133
     * rejects, the returned promise is rejected with the rejection reason.
147
     *
134
     *
148
     * @param mixed $promises  Promises or values.
135
     * @param mixed $promises  Promises or values.
149
     * @param bool  $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
136
     * @param bool  $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
150
     *
-
 
151
     * @return PromiseInterface
-
 
152
     */
137
     */
153
    public static function all($promises, $recursive = false)
138
    public static function all($promises, bool $recursive = false): PromiseInterface
154
    {
139
    {
155
        $results = [];
140
        $results = [];
156
        $promise = Each::of(
141
        $promise = Each::of(
157
            $promises,
142
            $promises,
158
            function ($value, $idx) use (&$results) {
143
            function ($value, $idx) use (&$results): void {
159
                $results[$idx] = $value;
144
                $results[$idx] = $value;
160
            },
145
            },
161
            function ($reason, $idx, Promise $aggregate) {
146
            function ($reason, $idx, Promise $aggregate): void {
-
 
147
                if (Is::pending($aggregate)) {
162
                $aggregate->reject($reason);
148
                    $aggregate->reject($reason);
-
 
149
                }
163
            }
150
            }
164
        )->then(function () use (&$results) {
151
        )->then(function () use (&$results) {
165
            ksort($results);
152
            ksort($results);
-
 
153
 
166
            return $results;
154
            return $results;
167
        });
155
        });
Línea 168... Línea 156...
168
 
156
 
169
        if (true === $recursive) {
157
        if (true === $recursive) {
170
            $promise = $promise->then(function ($results) use ($recursive, &$promises) {
158
            $promise = $promise->then(function ($results) use ($recursive, &$promises) {
171
                foreach ($promises as $promise) {
159
                foreach ($promises as $promise) {
172
                    if (Is::pending($promise)) {
160
                    if (Is::pending($promise)) {
173
                        return self::all($promises, $recursive);
161
                        return self::all($promises, $recursive);
174
                    }
162
                    }
-
 
163
                }
175
                }
164
 
176
                return $results;
165
                return $results;
177
            });
166
            });
Línea 178... Línea 167...
178
        }
167
        }
Línea 191... Línea 180...
191
     * This promise is rejected with a {@see AggregateException} if the number
180
     * This promise is rejected with a {@see AggregateException} if the number
192
     * of fulfilled promises is less than the desired $count.
181
     * of fulfilled promises is less than the desired $count.
193
     *
182
     *
194
     * @param int   $count    Total number of promises.
183
     * @param int   $count    Total number of promises.
195
     * @param mixed $promises Promises or values.
184
     * @param mixed $promises Promises or values.
196
     *
-
 
197
     * @return PromiseInterface
-
 
198
     */
185
     */
199
    public static function some($count, $promises)
186
    public static function some(int $count, $promises): PromiseInterface
200
    {
187
    {
201
        $results = [];
188
        $results = [];
202
        $rejections = [];
189
        $rejections = [];
Línea 203... Línea 190...
203
 
190
 
204
        return Each::of(
191
        return Each::of(
205
            $promises,
192
            $promises,
206
            function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
193
            function ($value, $idx, PromiseInterface $p) use (&$results, $count): void {
207
                if (Is::settled($p)) {
194
                if (Is::settled($p)) {
208
                    return;
195
                    return;
209
                }
196
                }
210
                $results[$idx] = $value;
197
                $results[$idx] = $value;
211
                if (count($results) >= $count) {
198
                if (count($results) >= $count) {
212
                    $p->resolve(null);
199
                    $p->resolve(null);
213
                }
200
                }
214
            },
201
            },
215
            function ($reason) use (&$rejections) {
202
            function ($reason) use (&$rejections): void {
216
                $rejections[] = $reason;
203
                $rejections[] = $reason;
217
            }
204
            }
218
        )->then(
205
        )->then(
219
            function () use (&$results, &$rejections, $count) {
206
            function () use (&$results, &$rejections, $count) {
Línea 222... Línea 209...
222
                        'Not enough promises to fulfill count',
209
                        'Not enough promises to fulfill count',
223
                        $rejections
210
                        $rejections
224
                    );
211
                    );
225
                }
212
                }
226
                ksort($results);
213
                ksort($results);
-
 
214
 
227
                return array_values($results);
215
                return array_values($results);
228
            }
216
            }
229
        );
217
        );
230
    }
218
    }
Línea 231... Línea 219...
231
 
219
 
232
    /**
220
    /**
233
     * Like some(), with 1 as count. However, if the promise fulfills, the
221
     * Like some(), with 1 as count. However, if the promise fulfills, the
234
     * fulfillment value is not an array of 1 but the value directly.
222
     * fulfillment value is not an array of 1 but the value directly.
235
     *
223
     *
236
     * @param mixed $promises Promises or values.
-
 
237
     *
-
 
238
     * @return PromiseInterface
224
     * @param mixed $promises Promises or values.
239
     */
225
     */
240
    public static function any($promises)
226
    public static function any($promises): PromiseInterface
241
    {
227
    {
242
        return self::some(1, $promises)->then(function ($values) {
228
        return self::some(1, $promises)->then(function ($values) {
243
            return $values[0];
229
            return $values[0];
244
        });
230
        });
Línea 251... Línea 237...
251
     * The returned promise is fulfilled with an array of inspection state arrays.
237
     * The returned promise is fulfilled with an array of inspection state arrays.
252
     *
238
     *
253
     * @see inspect for the inspection state array format.
239
     * @see inspect for the inspection state array format.
254
     *
240
     *
255
     * @param mixed $promises Promises or values.
241
     * @param mixed $promises Promises or values.
256
     *
-
 
257
     * @return PromiseInterface
-
 
258
     */
242
     */
259
    public static function settle($promises)
243
    public static function settle($promises): PromiseInterface
260
    {
244
    {
261
        $results = [];
245
        $results = [];
Línea 262... Línea 246...
262
 
246
 
263
        return Each::of(
247
        return Each::of(
264
            $promises,
248
            $promises,
265
            function ($value, $idx) use (&$results) {
249
            function ($value, $idx) use (&$results): void {
266
                $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
250
                $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
267
            },
251
            },
268
            function ($reason, $idx) use (&$results) {
252
            function ($reason, $idx) use (&$results): void {
269
                $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
253
                $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
270
            }
254
            }
271
        )->then(function () use (&$results) {
255
        )->then(function () use (&$results) {
-
 
256
            ksort($results);
272
            ksort($results);
257
 
273
            return $results;
258
            return $results;
274
        });
259
        });
275
    }
260
    }