|
2 | 2 |
|
3 | 3 | namespace Http\Adapter\React;
|
4 | 4 |
|
5 |
| -use React\EventLoop\LoopInterface; |
6 |
| -use Http\Client\Exception; |
| 5 | +use Http\Client\Exception as HttplugException; |
7 | 6 | use Http\Promise\Promise as HttpPromise;
|
| 7 | +use Psr\Http\Message\RequestInterface; |
8 | 8 | use Psr\Http\Message\ResponseInterface;
|
| 9 | +use React\EventLoop\LoopInterface; |
| 10 | +use React\Promise\PromiseInterface; |
| 11 | +use RuntimeException; |
| 12 | +use UnexpectedValueException; |
9 | 13 |
|
10 | 14 | /**
|
11 | 15 | * React promise adapter implementation.
|
12 | 16 | *
|
13 |
| - * @author Stéphane Hulard <[email protected]> |
| 17 | + * @author Stéphane Hulard <[email protected]> |
14 | 18 | *
|
15 | 19 | * @internal
|
16 | 20 | */
|
@@ -38,128 +42,65 @@ final class Promise implements HttpPromise
|
38 | 42 | private $exception;
|
39 | 43 |
|
40 | 44 | /**
|
41 |
| - * @var callable|null |
42 |
| - */ |
43 |
| - private $onFulfilled; |
44 |
| - |
45 |
| - /** |
46 |
| - * @var callable|null |
47 |
| - */ |
48 |
| - private $onRejected; |
49 |
| - |
50 |
| - /** |
51 |
| - * React Event Loop used for synchronous processing. |
| 45 | + * HTTP Request |
52 | 46 | *
|
53 |
| - * @var LoopInterface |
| 47 | + * @var RequestInterface |
54 | 48 | */
|
55 |
| - private $loop; |
56 |
| - |
57 |
| - public function __construct(LoopInterface $loop) |
58 |
| - { |
59 |
| - $this->loop = $loop; |
60 |
| - } |
| 49 | + private $request; |
61 | 50 |
|
62 | 51 | /**
|
63 |
| - * Allow to apply callable when the promise resolve. |
| 52 | + * Adapted ReactPHP promise |
64 | 53 | *
|
65 |
| - * @param callable|null $onFulfilled |
66 |
| - * @param callable|null $onRejected |
67 |
| - * |
68 |
| - * @return Promise |
| 54 | + * @var PromiseInterface |
69 | 55 | */
|
70 |
| - public function then(callable $onFulfilled = null, callable $onRejected = null) |
71 |
| - { |
72 |
| - $newPromise = new self($this->loop); |
73 |
| - |
74 |
| - $onFulfilled = null !== $onFulfilled ? $onFulfilled : function (ResponseInterface $response) { |
75 |
| - return $response; |
76 |
| - }; |
77 |
| - |
78 |
| - $onRejected = null !== $onRejected ? $onRejected : function (Exception $exception) { |
79 |
| - throw $exception; |
80 |
| - }; |
81 |
| - |
82 |
| - $this->onFulfilled = function (ResponseInterface $response) use ($onFulfilled, $newPromise) { |
83 |
| - try { |
84 |
| - $return = $onFulfilled($response); |
85 |
| - |
86 |
| - $newPromise->resolve(null !== $return ? $return : $response); |
87 |
| - } catch (Exception $exception) { |
88 |
| - $newPromise->reject($exception); |
89 |
| - } |
90 |
| - }; |
91 |
| - |
92 |
| - $this->onRejected = function (Exception $exception) use ($onRejected, $newPromise) { |
93 |
| - try { |
94 |
| - $newPromise->resolve($onRejected($exception)); |
95 |
| - } catch (Exception $exception) { |
96 |
| - $newPromise->reject($exception); |
97 |
| - } |
98 |
| - }; |
99 |
| - |
100 |
| - if (HttpPromise::FULFILLED === $this->state) { |
101 |
| - $this->doResolve($this->response); |
102 |
| - } |
103 |
| - |
104 |
| - if (HttpPromise::REJECTED === $this->state) { |
105 |
| - $this->doReject($this->exception); |
106 |
| - } |
107 |
| - |
108 |
| - return $newPromise; |
109 |
| - } |
| 56 | + private $promise; |
110 | 57 |
|
111 | 58 | /**
|
112 |
| - * Resolve this promise. |
| 59 | + * ReactPHP LoopInterface |
113 | 60 | *
|
114 |
| - * @param ResponseInterface $response |
115 |
| - * |
116 |
| - * @internal |
| 61 | + * @var LoopInterface |
117 | 62 | */
|
118 |
| - public function resolve(ResponseInterface $response) |
119 |
| - { |
120 |
| - if (HttpPromise::PENDING !== $this->state) { |
121 |
| - throw new \RuntimeException('Promise is already resolved'); |
122 |
| - } |
123 |
| - |
124 |
| - $this->state = HttpPromise::FULFILLED; |
125 |
| - $this->response = $response; |
126 |
| - $this->doResolve($response); |
127 |
| - } |
128 |
| - |
129 |
| - private function doResolve(ResponseInterface $response) |
130 |
| - { |
131 |
| - $onFulfilled = $this->onFulfilled; |
132 |
| - |
133 |
| - if (null !== $onFulfilled) { |
134 |
| - $onFulfilled($response); |
135 |
| - } |
136 |
| - } |
| 63 | + private $loop; |
137 | 64 |
|
138 |
| - /** |
139 |
| - * Reject this promise. |
140 |
| - * |
141 |
| - * @param Exception $exception |
142 |
| - * |
143 |
| - * @internal |
144 |
| - */ |
145 |
| - public function reject(Exception $exception) |
| 65 | + public function __construct(PromiseInterface $promise, LoopInterface $loop, RequestInterface $request) |
146 | 66 | {
|
147 |
| - if (HttpPromise::PENDING !== $this->state) { |
148 |
| - throw new \RuntimeException('Promise is already resolved'); |
149 |
| - } |
| 67 | + $this->state = self::PENDING; |
| 68 | + |
| 69 | + $this->request = $request; |
| 70 | + $this->loop = $loop; |
| 71 | + $this->promise = $promise->then( |
| 72 | + /** |
| 73 | + * @param ResponseInterface|null $response |
| 74 | + */ |
| 75 | + function (?ResponseInterface $response): ResponseInterface { |
| 76 | + $this->response = $response; |
| 77 | + $this->state = self::FULFILLED; |
| 78 | + |
| 79 | + return $response; |
| 80 | + }, |
| 81 | + /** |
| 82 | + * @param mixed $reason |
| 83 | + */ |
| 84 | + function ($reason): void { |
| 85 | + $this->state = self::REJECTED; |
| 86 | + |
| 87 | + if ($reason instanceof HttplugException) { |
| 88 | + $this->exception = $reason; |
| 89 | + } elseif ($reason instanceof RuntimeException) { |
| 90 | + $this->exception = new HttplugException\NetworkException($reason->getMessage(), $this->request, $reason); |
| 91 | + } elseif ($reason instanceof \Throwable) { |
| 92 | + $this->exception = new HttplugException\TransferException('Invalid exception returned from ReactPHP', 0, $reason); |
| 93 | + } else { |
| 94 | + $this->exception = new UnexpectedValueException('Reason returned from ReactPHP must be an Exception'); |
| 95 | + } |
150 | 96 |
|
151 |
| - $this->state = HttpPromise::REJECTED; |
152 |
| - $this->exception = $exception; |
153 |
| - $this->doReject($exception); |
| 97 | + throw $this->exception; |
| 98 | + }); |
154 | 99 | }
|
155 | 100 |
|
156 |
| - private function doReject(Exception $exception) |
| 101 | + public function then(?callable $onFulfilled = null, ?callable $onRejected = null) |
157 | 102 | {
|
158 |
| - $onRejected = $this->onRejected; |
159 |
| - |
160 |
| - if (null !== $onRejected) { |
161 |
| - $onRejected($exception); |
162 |
| - } |
| 103 | + return new self($this->promise->then($onFulfilled, $onRejected), $this->loop, $this->request); |
163 | 104 | }
|
164 | 105 |
|
165 | 106 | /**
|
|
0 commit comments