酒店预订平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

170 líneas
4.2 KiB

  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::of method 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::of(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. *
  40. * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
  41. */
  42. final class Coroutine implements PromiseInterface
  43. {
  44. /**
  45. * @var PromiseInterface|null
  46. */
  47. private $currentPromise;
  48. /**
  49. * @var Generator
  50. */
  51. private $generator;
  52. /**
  53. * @var Promise
  54. */
  55. private $result;
  56. public function __construct(callable $generatorFn)
  57. {
  58. $this->generator = $generatorFn();
  59. $this->result = new Promise(function () {
  60. while (isset($this->currentPromise)) {
  61. $this->currentPromise->wait();
  62. }
  63. });
  64. try {
  65. $this->nextCoroutine($this->generator->current());
  66. } catch (\Exception $exception) {
  67. $this->result->reject($exception);
  68. } catch (Throwable $throwable) {
  69. $this->result->reject($throwable);
  70. }
  71. }
  72. /**
  73. * Create a new coroutine.
  74. *
  75. * @return self
  76. */
  77. public static function of(callable $generatorFn)
  78. {
  79. return new self($generatorFn);
  80. }
  81. public function then(
  82. callable $onFulfilled = null,
  83. callable $onRejected = null
  84. ) {
  85. return $this->result->then($onFulfilled, $onRejected);
  86. }
  87. public function otherwise(callable $onRejected)
  88. {
  89. return $this->result->otherwise($onRejected);
  90. }
  91. public function wait($unwrap = true)
  92. {
  93. return $this->result->wait($unwrap);
  94. }
  95. public function getState()
  96. {
  97. return $this->result->getState();
  98. }
  99. public function resolve($value)
  100. {
  101. $this->result->resolve($value);
  102. }
  103. public function reject($reason)
  104. {
  105. $this->result->reject($reason);
  106. }
  107. public function cancel()
  108. {
  109. $this->currentPromise->cancel();
  110. $this->result->cancel();
  111. }
  112. private function nextCoroutine($yielded)
  113. {
  114. $this->currentPromise = Create::promiseFor($yielded)
  115. ->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
  116. }
  117. /**
  118. * @internal
  119. */
  120. public function _handleSuccess($value)
  121. {
  122. unset($this->currentPromise);
  123. try {
  124. $next = $this->generator->send($value);
  125. if ($this->generator->valid()) {
  126. $this->nextCoroutine($next);
  127. } else {
  128. $this->result->resolve($value);
  129. }
  130. } catch (Exception $exception) {
  131. $this->result->reject($exception);
  132. } catch (Throwable $throwable) {
  133. $this->result->reject($throwable);
  134. }
  135. }
  136. /**
  137. * @internal
  138. */
  139. public function _handleFailure($reason)
  140. {
  141. unset($this->currentPromise);
  142. try {
  143. $nextYield = $this->generator->throw(Create::exceptionFor($reason));
  144. // The throw was caught, so keep iterating on the coroutine
  145. $this->nextCoroutine($nextYield);
  146. } catch (Exception $exception) {
  147. $this->result->reject($exception);
  148. } catch (Throwable $throwable) {
  149. $this->result->reject($throwable);
  150. }
  151. }
  152. }