酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 line
1.9 KiB

  1. <?php
  2. namespace GuzzleHttp\Promise;
  3. /**
  4. * A promise that has been fulfilled.
  5. *
  6. * Thenning off of this promise will invoke the onFulfilled callback
  7. * immediately and ignore other callbacks.
  8. */
  9. class FulfilledPromise implements PromiseInterface
  10. {
  11. private $value;
  12. public function __construct($value)
  13. {
  14. if (is_object($value) && method_exists($value, 'then')) {
  15. throw new \InvalidArgumentException(
  16. 'You cannot create a FulfilledPromise with a promise.'
  17. );
  18. }
  19. $this->value = $value;
  20. }
  21. public function then(
  22. callable $onFulfilled = null,
  23. callable $onRejected = null
  24. ) {
  25. // Return itself if there is no onFulfilled function.
  26. if (!$onFulfilled) {
  27. return $this;
  28. }
  29. $queue = Utils::queue();
  30. $p = new Promise([$queue, 'run']);
  31. $value = $this->value;
  32. $queue->add(static function () use ($p, $value, $onFulfilled) {
  33. if (Is::pending($p)) {
  34. try {
  35. $p->resolve($onFulfilled($value));
  36. } catch (\Throwable $e) {
  37. $p->reject($e);
  38. } catch (\Exception $e) {
  39. $p->reject($e);
  40. }
  41. }
  42. });
  43. return $p;
  44. }
  45. public function otherwise(callable $onRejected)
  46. {
  47. return $this->then(null, $onRejected);
  48. }
  49. public function wait($unwrap = true, $defaultDelivery = null)
  50. {
  51. return $unwrap ? $this->value : null;
  52. }
  53. public function getState()
  54. {
  55. return self::FULFILLED;
  56. }
  57. public function resolve($value)
  58. {
  59. if ($value !== $this->value) {
  60. throw new \LogicException("Cannot resolve a fulfilled promise");
  61. }
  62. }
  63. public function reject($reason)
  64. {
  65. throw new \LogicException("Cannot reject a fulfilled promise");
  66. }
  67. public function cancel()
  68. {
  69. // pass
  70. }
  71. }