Coroutine.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace GuzzleHttp\Promise;
  3. use Exception;
  4. use Generator;
  5. use Throwable;
  6. /**
  7. * Creates a promise that is resolved using a generator that yields values or
  8. * promises (somewhat similar to C#'s async keyword).
  9. *
  10. * When called, the coroutine function will start an instance of the generator
  11. * and returns a promise that is fulfilled with its final yielded value.
  12. *
  13. * Control is returned back to the generator when the yielded promise settles.
  14. * This can lead to less verbose code when doing lots of sequential async calls
  15. * with minimal processing in between.
  16. *
  17. * use GuzzleHttp\Promise;
  18. *
  19. * function createPromise($value) {
  20. * return new Promise\FulfilledPromise($value);
  21. * }
  22. *
  23. * $promise = Promise\coroutine(function () {
  24. * $value = (yield createPromise('a'));
  25. * try {
  26. * $value = (yield createPromise($value . 'b'));
  27. * } catch (\Exception $e) {
  28. * // The promise was rejected.
  29. * }
  30. * yield $value . 'c';
  31. * });
  32. *
  33. * // Outputs "abc"
  34. * $promise->then(function ($v) { echo $v; });
  35. *
  36. * @param callable $generatorFn Generator function to wrap into a promise.
  37. *
  38. * @return Promise
  39. * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
  40. */
  41. final class Coroutine implements PromiseInterface
  42. {
  43. /**
  44. * @var PromiseInterface|null
  45. */
  46. private $currentPromise;
  47. /**
  48. * @var Generator
  49. */
  50. private $generator;
  51. /**
  52. * @var Promise
  53. */
  54. private $result;
  55. public function __construct(callable $generatorFn)
  56. {
  57. $this->generator = $generatorFn();
  58. $this->result = new Promise(function () {
  59. while (isset($this->currentPromise)) {
  60. $this->currentPromise->wait();
  61. }
  62. });
  63. $this->nextCoroutine($this->generator->current());
  64. }
  65. public function then(
  66. callable $onFulfilled = null,
  67. callable $onRejected = null
  68. ) {
  69. return $this->result->then($onFulfilled, $onRejected);
  70. }
  71. public function otherwise(callable $onRejected)
  72. {
  73. return $this->result->otherwise($onRejected);
  74. }
  75. public function wait($unwrap = true)
  76. {
  77. return $this->result->wait($unwrap);
  78. }
  79. public function getState()
  80. {
  81. return $this->result->getState();
  82. }
  83. public function resolve($value)
  84. {
  85. $this->result->resolve($value);
  86. }
  87. public function reject($reason)
  88. {
  89. $this->result->reject($reason);
  90. }
  91. public function cancel()
  92. {
  93. $this->currentPromise->cancel();
  94. $this->result->cancel();
  95. }
  96. private function nextCoroutine($yielded)
  97. {
  98. $this->currentPromise = promise_for($yielded)
  99. ->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
  100. }
  101. /**
  102. * @internal
  103. */
  104. public function _handleSuccess($value)
  105. {
  106. unset($this->currentPromise);
  107. try {
  108. $next = $this->generator->send($value);
  109. if ($this->generator->valid()) {
  110. $this->nextCoroutine($next);
  111. } else {
  112. $this->result->resolve($value);
  113. }
  114. } catch (Exception $exception) {
  115. $this->result->reject($exception);
  116. } catch (Throwable $throwable) {
  117. $this->result->reject($throwable);
  118. }
  119. }
  120. /**
  121. * @internal
  122. */
  123. public function _handleFailure($reason)
  124. {
  125. unset($this->currentPromise);
  126. try {
  127. $nextYield = $this->generator->throw(exception_for($reason));
  128. // The throw was caught, so keep iterating on the coroutine
  129. $this->nextCoroutine($nextYield);
  130. } catch (Exception $exception) {
  131. $this->result->reject($exception);
  132. } catch (Throwable $throwable) {
  133. $this->result->reject($throwable);
  134. }
  135. }
  136. }