| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Promise;
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Represents a promise that iterates over many promises and invokes
|
|---|
| 6 | * side-effect functions in the process.
|
|---|
| 7 | */
|
|---|
| 8 | class EachPromise implements PromisorInterface
|
|---|
| 9 | {
|
|---|
| 10 | private $pending = [];
|
|---|
| 11 |
|
|---|
| 12 | /** @var \Iterator */
|
|---|
| 13 | private $iterable;
|
|---|
| 14 |
|
|---|
| 15 | /** @var callable|int */
|
|---|
| 16 | private $concurrency;
|
|---|
| 17 |
|
|---|
| 18 | /** @var callable */
|
|---|
| 19 | private $onFulfilled;
|
|---|
| 20 |
|
|---|
| 21 | /** @var callable */
|
|---|
| 22 | private $onRejected;
|
|---|
| 23 |
|
|---|
| 24 | /** @var Promise */
|
|---|
| 25 | private $aggregate;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Configuration hash can include the following key value pairs:
|
|---|
| 29 | *
|
|---|
| 30 | * - fulfilled: (callable) Invoked when a promise fulfills. The function
|
|---|
| 31 | * is invoked with three arguments: the fulfillment value, the index
|
|---|
| 32 | * position from the iterable list of the promise, and the aggregate
|
|---|
| 33 | * promise that manages all of the promises. The aggregate promise may
|
|---|
| 34 | * be resolved from within the callback to short-circuit the promise.
|
|---|
| 35 | * - rejected: (callable) Invoked when a promise is rejected. The
|
|---|
| 36 | * function is invoked with three arguments: the rejection reason, the
|
|---|
| 37 | * index position from the iterable list of the promise, and the
|
|---|
| 38 | * aggregate promise that manages all of the promises. The aggregate
|
|---|
| 39 | * promise may be resolved from within the callback to short-circuit
|
|---|
| 40 | * the promise.
|
|---|
| 41 | * - concurrency: (integer) Pass this configuration option to limit the
|
|---|
| 42 | * allowed number of outstanding concurrently executing promises,
|
|---|
| 43 | * creating a capped pool of promises. There is no limit by default.
|
|---|
| 44 | *
|
|---|
| 45 | * @param mixed $iterable Promises or values to iterate.
|
|---|
| 46 | * @param array $config Configuration options
|
|---|
| 47 | */
|
|---|
| 48 | public function __construct($iterable, array $config = [])
|
|---|
| 49 | {
|
|---|
| 50 | $this->iterable = iter_for($iterable);
|
|---|
| 51 |
|
|---|
| 52 | if (isset($config['concurrency'])) {
|
|---|
| 53 | $this->concurrency = $config['concurrency'];
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (isset($config['fulfilled'])) {
|
|---|
| 57 | $this->onFulfilled = $config['fulfilled'];
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | if (isset($config['rejected'])) {
|
|---|
| 61 | $this->onRejected = $config['rejected'];
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public function promise()
|
|---|
| 66 | {
|
|---|
| 67 | if ($this->aggregate) {
|
|---|
| 68 | return $this->aggregate;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | try {
|
|---|
| 72 | $this->createPromise();
|
|---|
| 73 | $this->iterable->rewind();
|
|---|
| 74 | $this->refillPending();
|
|---|
| 75 | } catch (\Exception $e) {
|
|---|
| 76 | $this->aggregate->reject($e);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return $this->aggregate;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | private function createPromise()
|
|---|
| 83 | {
|
|---|
| 84 | $this->aggregate = new Promise(function () {
|
|---|
| 85 | reset($this->pending);
|
|---|
| 86 | // Consume a potentially fluctuating list of promises while
|
|---|
| 87 | // ensuring that indexes are maintained (precluding array_shift).
|
|---|
| 88 | while ($promise = current($this->pending)) {
|
|---|
| 89 | next($this->pending);
|
|---|
| 90 | $promise->wait();
|
|---|
| 91 | if ($this->aggregate->getState() !== PromiseInterface::PENDING) {
|
|---|
| 92 | return;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | });
|
|---|
| 96 |
|
|---|
| 97 | // Clear the references when the promise is resolved.
|
|---|
| 98 | $clearFn = function () {
|
|---|
| 99 | $this->iterable = $this->concurrency = $this->pending = null;
|
|---|
| 100 | $this->onFulfilled = $this->onRejected = null;
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | $this->aggregate->then($clearFn, $clearFn);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | private function refillPending()
|
|---|
| 107 | {
|
|---|
| 108 | if (!$this->concurrency) {
|
|---|
| 109 | // Add all pending promises.
|
|---|
| 110 | while ($this->addPending() && $this->advanceIterator());
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // Add only up to N pending promises.
|
|---|
| 115 | $concurrency = is_callable($this->concurrency)
|
|---|
| 116 | ? call_user_func($this->concurrency, count($this->pending))
|
|---|
| 117 | : $this->concurrency;
|
|---|
| 118 | $concurrency = max($concurrency - count($this->pending), 0);
|
|---|
| 119 | // Concurrency may be set to 0 to disallow new promises.
|
|---|
| 120 | if (!$concurrency) {
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 | // Add the first pending promise.
|
|---|
| 124 | $this->addPending();
|
|---|
| 125 | // Note this is special handling for concurrency=1 so that we do
|
|---|
| 126 | // not advance the iterator after adding the first promise. This
|
|---|
| 127 | // helps work around issues with generators that might not have the
|
|---|
| 128 | // next value to yield until promise callbacks are called.
|
|---|
| 129 | while (--$concurrency
|
|---|
| 130 | && $this->advanceIterator()
|
|---|
| 131 | && $this->addPending());
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | private function addPending()
|
|---|
| 135 | {
|
|---|
| 136 | if (!$this->iterable || !$this->iterable->valid()) {
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | $promise = promise_for($this->iterable->current());
|
|---|
| 141 | $idx = $this->iterable->key();
|
|---|
| 142 |
|
|---|
| 143 | $this->pending[$idx] = $promise->then(
|
|---|
| 144 | function ($value) use ($idx) {
|
|---|
| 145 | if ($this->onFulfilled) {
|
|---|
| 146 | call_user_func(
|
|---|
| 147 | $this->onFulfilled, $value, $idx, $this->aggregate
|
|---|
| 148 | );
|
|---|
| 149 | }
|
|---|
| 150 | $this->step($idx);
|
|---|
| 151 | },
|
|---|
| 152 | function ($reason) use ($idx) {
|
|---|
| 153 | if ($this->onRejected) {
|
|---|
| 154 | call_user_func(
|
|---|
| 155 | $this->onRejected, $reason, $idx, $this->aggregate
|
|---|
| 156 | );
|
|---|
| 157 | }
|
|---|
| 158 | $this->step($idx);
|
|---|
| 159 | }
|
|---|
| 160 | );
|
|---|
| 161 |
|
|---|
| 162 | return true;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | private function advanceIterator()
|
|---|
| 166 | {
|
|---|
| 167 | try {
|
|---|
| 168 | $this->iterable->next();
|
|---|
| 169 | return true;
|
|---|
| 170 | } catch (\Exception $e) {
|
|---|
| 171 | $this->aggregate->reject($e);
|
|---|
| 172 | return false;
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | private function step($idx)
|
|---|
| 177 | {
|
|---|
| 178 | // If the promise was already resolved, then ignore this step.
|
|---|
| 179 | if ($this->aggregate->getState() !== PromiseInterface::PENDING) {
|
|---|
| 180 | return;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | unset($this->pending[$idx]);
|
|---|
| 184 | $this->advanceIterator();
|
|---|
| 185 |
|
|---|
| 186 | if (!$this->checkIfFinished()) {
|
|---|
| 187 | // Add more pending promises if possible.
|
|---|
| 188 | $this->refillPending();
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | private function checkIfFinished()
|
|---|
| 193 | {
|
|---|
| 194 | if (!$this->pending && !$this->iterable->valid()) {
|
|---|
| 195 | // Resolve the promise if there's nothing left to do.
|
|---|
| 196 | $this->aggregate->resolve(null);
|
|---|
| 197 | return true;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | return false;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|