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
/**
-
 
8
 * Represents a promise that iterates over many promises and invokes
-
 
9
 * side-effect functions in the process.
6
 * Represents a promise that iterates over many promises and invokes
10
 *
7
 * side-effect functions in the process.
11
 * @final
8
 */
12
 */
9
class EachPromise implements PromisorInterface
13
class EachPromise implements PromisorInterface
Línea 67... Línea 71...
67
            $this->onRejected = $config['rejected'];
71
            $this->onRejected = $config['rejected'];
68
        }
72
        }
69
    }
73
    }
Línea 70... Línea 74...
70
 
74
 
71
    /** @psalm-suppress InvalidNullableReturnType */
75
    /** @psalm-suppress InvalidNullableReturnType */
72
    public function promise()
76
    public function promise(): PromiseInterface
73
    {
77
    {
74
        if ($this->aggregate) {
78
        if ($this->aggregate) {
75
            return $this->aggregate;
79
            return $this->aggregate;
Línea 80... Línea 84...
80
            /** @psalm-assert Promise $this->aggregate */
84
            /** @psalm-assert Promise $this->aggregate */
81
            $this->iterable->rewind();
85
            $this->iterable->rewind();
82
            $this->refillPending();
86
            $this->refillPending();
83
        } catch (\Throwable $e) {
87
        } catch (\Throwable $e) {
84
            $this->aggregate->reject($e);
88
            $this->aggregate->reject($e);
85
        } catch (\Exception $e) {
-
 
86
            $this->aggregate->reject($e);
-
 
87
        }
89
        }
Línea 88... Línea 90...
88
 
90
 
89
        /**
91
        /**
90
         * @psalm-suppress NullableReturnStatement
-
 
91
         * @phpstan-ignore-next-line
92
         * @psalm-suppress NullableReturnStatement
92
         */
93
         */
93
        return $this->aggregate;
94
        return $this->aggregate;
Línea 94... Línea 95...
94
    }
95
    }
95
 
96
 
96
    private function createPromise()
97
    private function createPromise(): void
97
    {
98
    {
98
        $this->mutex = false;
99
        $this->mutex = false;
99
        $this->aggregate = new Promise(function () {
100
        $this->aggregate = new Promise(function (): void {
100
            if ($this->checkIfFinished()) {
101
            if ($this->checkIfFinished()) {
101
                return;
102
                return;
102
            }
103
            }
Línea 111... Línea 112...
111
                }
112
                }
112
            }
113
            }
113
        });
114
        });
Línea 114... Línea 115...
114
 
115
 
115
        // Clear the references when the promise is resolved.
116
        // Clear the references when the promise is resolved.
116
        $clearFn = function () {
117
        $clearFn = function (): void {
117
            $this->iterable = $this->concurrency = $this->pending = null;
118
            $this->iterable = $this->concurrency = $this->pending = null;
118
            $this->onFulfilled = $this->onRejected = null;
119
            $this->onFulfilled = $this->onRejected = null;
119
            $this->nextPendingIndex = 0;
120
            $this->nextPendingIndex = 0;
Línea 120... Línea 121...
120
        };
121
        };
121
 
122
 
Línea 122... Línea 123...
122
        $this->aggregate->then($clearFn, $clearFn);
123
        $this->aggregate->then($clearFn, $clearFn);
123
    }
124
    }
124
 
125
 
125
    private function refillPending()
126
    private function refillPending(): void
126
    {
127
    {
-
 
128
        if (!$this->concurrency) {
-
 
129
            // Add all pending promises.
127
        if (!$this->concurrency) {
130
            while ($this->addPending() && $this->advanceIterator()) {
128
            // Add all pending promises.
131
            }
Línea 129... Línea 132...
129
            while ($this->addPending() && $this->advanceIterator());
132
 
130
            return;
133
            return;
131
        }
134
        }
132
 
135
 
133
        // Add only up to N pending promises.
136
        // Add only up to N pending promises.
134
        $concurrency = is_callable($this->concurrency)
137
        $concurrency = is_callable($this->concurrency)
135
            ? call_user_func($this->concurrency, count($this->pending))
138
            ? ($this->concurrency)(count($this->pending))
136
            : $this->concurrency;
139
            : $this->concurrency;
Línea 145... Línea 148...
145
        // not advance the iterator after adding the first promise. This
148
        // not advance the iterator after adding the first promise. This
146
        // helps work around issues with generators that might not have the
149
        // helps work around issues with generators that might not have the
147
        // next value to yield until promise callbacks are called.
150
        // next value to yield until promise callbacks are called.
148
        while (--$concurrency
151
        while (--$concurrency
149
            && $this->advanceIterator()
152
            && $this->advanceIterator()
150
            && $this->addPending());
153
            && $this->addPending()) {
-
 
154
        }
151
    }
155
    }
Línea 152... Línea 156...
152
 
156
 
153
    private function addPending()
157
    private function addPending(): bool
154
    {
158
    {
155
        if (!$this->iterable || !$this->iterable->valid()) {
159
        if (!$this->iterable || !$this->iterable->valid()) {
156
            return false;
160
            return false;
Línea 162... Línea 166...
162
        // Iterable keys may not be unique, so we use a counter to
166
        // Iterable keys may not be unique, so we use a counter to
163
        // guarantee uniqueness
167
        // guarantee uniqueness
164
        $idx = $this->nextPendingIndex++;
168
        $idx = $this->nextPendingIndex++;
Línea 165... Línea 169...
165
 
169
 
166
        $this->pending[$idx] = $promise->then(
170
        $this->pending[$idx] = $promise->then(
167
            function ($value) use ($idx, $key) {
171
            function ($value) use ($idx, $key): void {
168
                if ($this->onFulfilled) {
-
 
169
                    call_user_func(
172
                if ($this->onFulfilled) {
170
                        $this->onFulfilled,
173
                    ($this->onFulfilled)(
171
                        $value,
174
                        $value,
172
                        $key,
175
                        $key,
173
                        $this->aggregate
176
                        $this->aggregate
174
                    );
177
                    );
175
                }
178
                }
176
                $this->step($idx);
179
                $this->step($idx);
177
            },
180
            },
178
            function ($reason) use ($idx, $key) {
181
            function ($reason) use ($idx, $key): void {
179
                if ($this->onRejected) {
-
 
180
                    call_user_func(
182
                if ($this->onRejected) {
181
                        $this->onRejected,
183
                    ($this->onRejected)(
182
                        $reason,
184
                        $reason,
183
                        $key,
185
                        $key,
184
                        $this->aggregate
186
                        $this->aggregate
185
                    );
187
                    );
Línea 189... Línea 191...
189
        );
191
        );
Línea 190... Línea 192...
190
 
192
 
191
        return true;
193
        return true;
Línea 192... Línea 194...
192
    }
194
    }
193
 
195
 
194
    private function advanceIterator()
196
    private function advanceIterator(): bool
195
    {
197
    {
196
        // Place a lock on the iterator so that we ensure to not recurse,
198
        // Place a lock on the iterator so that we ensure to not recurse,
197
        // preventing fatal generator errors.
199
        // preventing fatal generator errors.
Línea 202... Línea 204...
202
        $this->mutex = true;
204
        $this->mutex = true;
Línea 203... Línea 205...
203
 
205
 
204
        try {
206
        try {
205
            $this->iterable->next();
207
            $this->iterable->next();
-
 
208
            $this->mutex = false;
206
            $this->mutex = false;
209
 
207
            return true;
210
            return true;
208
        } catch (\Throwable $e) {
211
        } catch (\Throwable $e) {
209
            $this->aggregate->reject($e);
212
            $this->aggregate->reject($e);
210
            $this->mutex = false;
-
 
211
            return false;
-
 
212
        } catch (\Exception $e) {
-
 
213
            $this->aggregate->reject($e);
-
 
-
 
213
            $this->mutex = false;
214
            $this->mutex = false;
214
 
215
            return false;
215
            return false;
216
        }
216
        }
Línea 217... Línea 217...
217
    }
217
    }
218
 
218
 
219
    private function step($idx)
219
    private function step(int $idx): void
220
    {
220
    {
221
        // If the promise was already resolved, then ignore this step.
221
        // If the promise was already resolved, then ignore this step.
222
        if (Is::settled($this->aggregate)) {
222
        if (Is::settled($this->aggregate)) {
Línea 232... Línea 232...
232
            // Add more pending promises if possible.
232
            // Add more pending promises if possible.
233
            $this->refillPending();
233
            $this->refillPending();
234
        }
234
        }
235
    }
235
    }
Línea 236... Línea 236...
236
 
236
 
237
    private function checkIfFinished()
237
    private function checkIfFinished(): bool
238
    {
238
    {
239
        if (!$this->pending && !$this->iterable->valid()) {
239
        if (!$this->pending && !$this->iterable->valid()) {
240
            // Resolve the promise if there's nothing left to do.
240
            // Resolve the promise if there's nothing left to do.
-
 
241
            $this->aggregate->resolve(null);
241
            $this->aggregate->resolve(null);
242
 
242
            return true;
243
            return true;
Línea 243... Línea 244...
243
        }
244
        }
244
 
245