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 -...
3
namespace GuzzleHttp\Promise;
-
 
4
 
5
namespace GuzzleHttp\Promise;
5
use Exception;
6
 
Línea 6... Línea 7...
6
use Generator;
7
use Generator;
7
use Throwable;
8
use Throwable;
Línea 25... Línea 26...
25
 *
26
 *
26
 *     $promise = Promise\Coroutine::of(function () {
27
 *     $promise = Promise\Coroutine::of(function () {
27
 *         $value = (yield createPromise('a'));
28
 *         $value = (yield createPromise('a'));
28
 *         try {
29
 *         try {
29
 *             $value = (yield createPromise($value . 'b'));
30
 *             $value = (yield createPromise($value . 'b'));
30
 *         } catch (\Exception $e) {
31
 *         } catch (\Throwable $e) {
31
 *             // The promise was rejected.
32
 *             // The promise was rejected.
32
 *         }
33
 *         }
33
 *         yield $value . 'c';
34
 *         yield $value . 'c';
34
 *     });
35
 *     });
35
 *
36
 *
Línea 38... Línea 39...
38
 *
39
 *
39
 * @param callable $generatorFn Generator function to wrap into a promise.
40
 * @param callable $generatorFn Generator function to wrap into a promise.
40
 *
41
 *
41
 * @return Promise
42
 * @return Promise
42
 *
43
 *
43
 * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
44
 * @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
44
 */
45
 */
45
final class Coroutine implements PromiseInterface
46
final class Coroutine implements PromiseInterface
46
{
47
{
47
    /**
48
    /**
48
     * @var PromiseInterface|null
49
     * @var PromiseInterface|null
Línea 60... Línea 61...
60
    private $result;
61
    private $result;
Línea 61... Línea 62...
61
 
62
 
62
    public function __construct(callable $generatorFn)
63
    public function __construct(callable $generatorFn)
63
    {
64
    {
64
        $this->generator = $generatorFn();
65
        $this->generator = $generatorFn();
65
        $this->result = new Promise(function () {
66
        $this->result = new Promise(function (): void {
66
            while (isset($this->currentPromise)) {
67
            while (isset($this->currentPromise)) {
67
                $this->currentPromise->wait();
68
                $this->currentPromise->wait();
68
            }
69
            }
69
        });
70
        });
70
        try {
71
        try {
71
            $this->nextCoroutine($this->generator->current());
-
 
72
        } catch (\Exception $exception) {
-
 
73
            $this->result->reject($exception);
72
            $this->nextCoroutine($this->generator->current());
74
        } catch (Throwable $throwable) {
73
        } catch (Throwable $throwable) {
75
            $this->result->reject($throwable);
74
            $this->result->reject($throwable);
76
        }
75
        }
Línea 77... Línea 76...
77
    }
76
    }
78
 
77
 
79
    /**
-
 
80
     * Create a new coroutine.
-
 
81
     *
78
    /**
82
     * @return self
79
     * Create a new coroutine.
83
     */
80
     */
84
    public static function of(callable $generatorFn)
81
    public static function of(callable $generatorFn): self
85
    {
82
    {
Línea 86... Línea 83...
86
        return new self($generatorFn);
83
        return new self($generatorFn);
87
    }
84
    }
88
 
85
 
89
    public function then(
86
    public function then(
90
        callable $onFulfilled = null,
87
        ?callable $onFulfilled = null,
91
        callable $onRejected = null
88
        ?callable $onRejected = null
Línea 92... Línea 89...
92
    ) {
89
    ): PromiseInterface {
93
        return $this->result->then($onFulfilled, $onRejected);
90
        return $this->result->then($onFulfilled, $onRejected);
94
    }
91
    }
95
 
92
 
Línea 96... Línea 93...
96
    public function otherwise(callable $onRejected)
93
    public function otherwise(callable $onRejected): PromiseInterface
97
    {
94
    {
98
        return $this->result->otherwise($onRejected);
95
        return $this->result->otherwise($onRejected);
99
    }
96
    }
Línea 100... Línea 97...
100
 
97
 
101
    public function wait($unwrap = true)
98
    public function wait(bool $unwrap = true)
102
    {
99
    {
103
        return $this->result->wait($unwrap);
100
        return $this->result->wait($unwrap);
Línea 104... Línea 101...
104
    }
101
    }
105
 
102
 
106
    public function getState()
103
    public function getState(): string
107
    {
104
    {
Línea 108... Línea 105...
108
        return $this->result->getState();
105
        return $this->result->getState();
109
    }
106
    }
110
 
107
 
111
    public function resolve($value)
108
    public function resolve($value): void
Línea 112... Línea 109...
112
    {
109
    {
113
        $this->result->resolve($value);
110
        $this->result->resolve($value);
114
    }
111
    }
115
 
112
 
116
    public function reject($reason)
113
    public function reject($reason): void
Línea 117... Línea 114...
117
    {
114
    {
118
        $this->result->reject($reason);
115
        $this->result->reject($reason);
119
    }
116
    }
120
 
117
 
121
    public function cancel()
118
    public function cancel(): void
Línea 122... Línea 119...
122
    {
119
    {
123
        $this->currentPromise->cancel();
120
        $this->currentPromise->cancel();
124
        $this->result->cancel();
121
        $this->result->cancel();
125
    }
122
    }
126
 
123
 
127
    private function nextCoroutine($yielded)
124
    private function nextCoroutine($yielded): void
128
    {
125
    {
129
        $this->currentPromise = Create::promiseFor($yielded)
126
        $this->currentPromise = Create::promiseFor($yielded)
130
            ->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
127
            ->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
131
    }
128
    }
132
 
129
 
133
    /**
130
    /**
134
     * @internal
131
     * @internal
135
     */
-
 
136
    public function _handleSuccess($value)
-
 
137
    {
132
     */
138
        unset($this->currentPromise);
133
    public function _handleSuccess($value): void
139
        try {
134
    {
140
            $next = $this->generator->send($value);
135
        unset($this->currentPromise);
Línea 141... Línea 136...
141
            if ($this->generator->valid()) {
136
        try {
142
                $this->nextCoroutine($next);
137
            $next = $this->generator->send($value);
143
            } else {
138
            if ($this->generator->valid()) {
144
                $this->result->resolve($value);
139
                $this->nextCoroutine($next);
145
            }
140
            } else {
146
        } catch (Exception $exception) {
141
                $this->result->resolve($value);
147
            $this->result->reject($exception);
142
            }
148
        } catch (Throwable $throwable) {
143
        } catch (Throwable $throwable) {
149
            $this->result->reject($throwable);
144
            $this->result->reject($throwable);
150
        }
145
        }
151
    }
-
 
152
 
-
 
153
    /**
146
    }
154
     * @internal
147
 
155
     */
148
    /**
156
    public function _handleFailure($reason)
149
     * @internal
157
    {
150
     */