RejectedPromise.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace React\Promise\Internal;
  3. use React\Promise\PromiseInterface;
  4. use function React\Promise\_checkTypehint;
  5. use function React\Promise\resolve;
  6. use function React\Promise\set_rejection_handler;
  7. /**
  8. * @internal
  9. *
  10. * @template-implements PromiseInterface<never>
  11. */
  12. final class RejectedPromise implements PromiseInterface
  13. {
  14. /** @var \Throwable */
  15. private $reason;
  16. /** @var bool */
  17. private $handled = false;
  18. /**
  19. * @param \Throwable $reason
  20. */
  21. public function __construct(\Throwable $reason)
  22. {
  23. $this->reason = $reason;
  24. }
  25. /** @throws void */
  26. public function __destruct()
  27. {
  28. if ($this->handled) {
  29. return;
  30. }
  31. $handler = set_rejection_handler(null);
  32. if ($handler === null) {
  33. $message = 'Unhandled promise rejection with ' . $this->reason;
  34. \error_log($message);
  35. return;
  36. }
  37. try {
  38. $handler($this->reason);
  39. } catch (\Throwable $e) {
  40. \preg_match('/^([^:\s]++)(.*+)$/sm', (string) $e, $match);
  41. \assert(isset($match[1], $match[2]));
  42. $message = 'Fatal error: Uncaught ' . $match[1] . ' from unhandled promise rejection handler' . $match[2];
  43. \error_log($message);
  44. exit(255);
  45. }
  46. }
  47. /**
  48. * @template TRejected
  49. * @param ?callable $onFulfilled
  50. * @param ?(callable(\Throwable): (PromiseInterface<TRejected>|TRejected)) $onRejected
  51. * @return PromiseInterface<($onRejected is null ? never : TRejected)>
  52. */
  53. public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
  54. {
  55. if (null === $onRejected) {
  56. return $this;
  57. }
  58. $this->handled = true;
  59. try {
  60. return resolve($onRejected($this->reason));
  61. } catch (\Throwable $exception) {
  62. return new RejectedPromise($exception);
  63. }
  64. }
  65. /**
  66. * @template TThrowable of \Throwable
  67. * @template TRejected
  68. * @param callable(TThrowable): (PromiseInterface<TRejected>|TRejected) $onRejected
  69. * @return PromiseInterface<TRejected>
  70. */
  71. public function catch(callable $onRejected): PromiseInterface
  72. {
  73. if (!_checkTypehint($onRejected, $this->reason)) {
  74. return $this;
  75. }
  76. /**
  77. * @var callable(\Throwable):(PromiseInterface<TRejected>|TRejected) $onRejected
  78. */
  79. return $this->then(null, $onRejected);
  80. }
  81. public function finally(callable $onFulfilledOrRejected): PromiseInterface
  82. {
  83. return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected): PromiseInterface {
  84. return resolve($onFulfilledOrRejected())->then(function () use ($reason): PromiseInterface {
  85. return new RejectedPromise($reason);
  86. });
  87. });
  88. }
  89. public function cancel(): void
  90. {
  91. $this->handled = true;
  92. }
  93. /**
  94. * @deprecated 3.0.0 Use `catch()` instead
  95. * @see self::catch()
  96. */
  97. public function otherwise(callable $onRejected): PromiseInterface
  98. {
  99. return $this->catch($onRejected);
  100. }
  101. /**
  102. * @deprecated 3.0.0 Use `always()` instead
  103. * @see self::always()
  104. */
  105. public function always(callable $onFulfilledOrRejected): PromiseInterface
  106. {
  107. return $this->finally($onFulfilledOrRejected);
  108. }
  109. }