| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace GuzzleHttp\Promise; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class EachPromise implements PromisorInterface |
| 14 |
{ |
| 15 |
private $pending = []; |
| 16 |
|
| 17 |
private $nextPendingIndex = 0; |
| 18 |
|
| 19 |
|
| 20 |
private $iterable; |
| 21 |
|
| 22 |
|
| 23 |
private $concurrency; |
| 24 |
|
| 25 |
|
| 26 |
private $onFulfilled; |
| 27 |
|
| 28 |
|
| 29 |
private $onRejected; |
| 30 |
|
| 31 |
|
| 32 |
private $aggregate; |
| 33 |
|
| 34 |
|
| 35 |
private $mutex; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct($iterable, array $config = []) |
| 59 |
{ |
| 60 |
if (!is_iterable($iterable)) { |
| 61 |
\trigger_deprecation( |
| 62 |
'guzzlehttp/promises', |
| 63 |
'2.5', |
| 64 |
'Passing a non-iterable to %s::%s() is deprecated; guzzlehttp/promises 3.0 will require an iterable.', |
| 65 |
__CLASS__, |
| 66 |
__FUNCTION__ |
| 67 |
); |
| 68 |
|
| 69 |
$iterable = [$iterable]; |
| 70 |
} |
| 71 |
|
| 72 |
$this->iterable = Create::iterFor($iterable); |
| 73 |
|
| 74 |
if (isset($config['concurrency'])) { |
| 75 |
$this->concurrency = $config['concurrency']; |
| 76 |
} |
| 77 |
|
| 78 |
if (isset($config['fulfilled'])) { |
| 79 |
$this->onFulfilled = $config['fulfilled']; |
| 80 |
} |
| 81 |
|
| 82 |
if (isset($config['rejected'])) { |
| 83 |
$this->onRejected = $config['rejected']; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public function promise(): PromiseInterface |
| 89 |
{ |
| 90 |
if ($this->aggregate) { |
| 91 |
return $this->aggregate; |
| 92 |
} |
| 93 |
|
| 94 |
try { |
| 95 |
$this->createPromise(); |
| 96 |
|
| 97 |
$this->iterable->rewind(); |
| 98 |
$this->refillPending(); |
| 99 |
if (!$this->pending) { |
| 100 |
Utils::queue()->add(function (): void { |
| 101 |
if (!$this->aggregate || Is::settled($this->aggregate)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
try { |
| 106 |
$this->checkIfFinished(); |
| 107 |
} catch (\Throwable $e) { |
| 108 |
$this->aggregate->reject($e); |
| 109 |
} |
| 110 |
}); |
| 111 |
} |
| 112 |
} catch (\Throwable $e) { |
| 113 |
$this->aggregate->reject($e); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return $this->aggregate; |
| 120 |
} |
| 121 |
|
| 122 |
private function createPromise(): void |
| 123 |
{ |
| 124 |
$this->mutex = false; |
| 125 |
$this->aggregate = new Promise(function (): void { |
| 126 |
if ($this->checkIfFinished()) { |
| 127 |
return; |
| 128 |
} |
| 129 |
reset($this->pending); |
| 130 |
|
| 131 |
|
| 132 |
while ($promise = current($this->pending)) { |
| 133 |
next($this->pending); |
| 134 |
$promise->wait(); |
| 135 |
if (Is::settled($this->aggregate)) { |
| 136 |
return; |
| 137 |
} |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
|
| 142 |
$clearFn = function (): void { |
| 143 |
$this->iterable = $this->concurrency = $this->pending = null; |
| 144 |
$this->onFulfilled = $this->onRejected = null; |
| 145 |
$this->nextPendingIndex = 0; |
| 146 |
}; |
| 147 |
|
| 148 |
$this->aggregate->then($clearFn, $clearFn); |
| 149 |
} |
| 150 |
|
| 151 |
private function refillPending(): void |
| 152 |
{ |
| 153 |
if (!$this->concurrency) { |
| 154 |
|
| 155 |
while ($this->addPending() && $this->advanceIterator()) { |
| 156 |
} |
| 157 |
|
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
$concurrency = is_callable($this->concurrency) |
| 163 |
? ($this->concurrency)(count($this->pending)) |
| 164 |
: $this->concurrency; |
| 165 |
$concurrency = max($concurrency - count($this->pending), 0); |
| 166 |
|
| 167 |
if (!$concurrency) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$this->addPending(); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
while (--$concurrency |
| 177 |
&& $this->advanceIterator() |
| 178 |
&& $this->addPending()) { |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private function addPending(): bool |
| 183 |
{ |
| 184 |
if (!$this->iterable || !$this->iterable->valid()) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
$promise = Create::promiseFor($this->iterable->current()); |
| 189 |
$key = $this->iterable->key(); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
$idx = $this->nextPendingIndex++; |
| 194 |
|
| 195 |
$this->pending[$idx] = $promise->then( |
| 196 |
function ($value) use ($idx, $key): void { |
| 197 |
if ($this->onFulfilled) { |
| 198 |
($this->onFulfilled)( |
| 199 |
$value, |
| 200 |
$key, |
| 201 |
$this->aggregate |
| 202 |
); |
| 203 |
} |
| 204 |
$this->step($idx); |
| 205 |
}, |
| 206 |
function ($reason) use ($idx, $key): void { |
| 207 |
if ($this->onRejected) { |
| 208 |
($this->onRejected)( |
| 209 |
$reason, |
| 210 |
$key, |
| 211 |
$this->aggregate |
| 212 |
); |
| 213 |
} |
| 214 |
$this->step($idx); |
| 215 |
} |
| 216 |
); |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
private function advanceIterator(): bool |
| 222 |
{ |
| 223 |
|
| 224 |
|
| 225 |
if ($this->mutex) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
$this->mutex = true; |
| 230 |
|
| 231 |
try { |
| 232 |
$this->iterable->next(); |
| 233 |
$this->mutex = false; |
| 234 |
|
| 235 |
return true; |
| 236 |
} catch (\Throwable $e) { |
| 237 |
$this->aggregate->reject($e); |
| 238 |
$this->mutex = false; |
| 239 |
|
| 240 |
return false; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private function step(int $idx): void |
| 245 |
{ |
| 246 |
|
| 247 |
if (Is::settled($this->aggregate)) { |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
unset($this->pending[$idx]); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
if ($this->advanceIterator() && !$this->checkIfFinished()) { |
| 257 |
|
| 258 |
$this->refillPending(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
private function checkIfFinished(): bool |
| 263 |
{ |
| 264 |
if (!$this->pending && !$this->iterable->valid()) { |
| 265 |
|
| 266 |
$this->aggregate->resolve(null); |
| 267 |
|
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
return false; |
| 272 |
} |
| 273 |
} |