/wp-content/plugins/google-calendar-events/third-party/guzzlehttp/promises/src/EachPromise.php

https://github.com/livinglab/openlab · PHP · 207 lines · 139 code · 2 blank · 66 comment · 24 complexity · b1b6f97119a18d19cc7746e83d2669cc MD5 · raw file

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