LazyPromise.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace React\Promise;
  3. /**
  4. * @deprecated 2.8.0 LazyPromise is deprecated and should not be used anymore.
  5. */
  6. class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
  7. {
  8. private $factory;
  9. private $promise;
  10. public function __construct(callable $factory)
  11. {
  12. $this->factory = $factory;
  13. }
  14. public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
  15. {
  16. return $this->promise()->then($onFulfilled, $onRejected, $onProgress);
  17. }
  18. public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
  19. {
  20. return $this->promise()->done($onFulfilled, $onRejected, $onProgress);
  21. }
  22. public function otherwise(callable $onRejected)
  23. {
  24. return $this->promise()->otherwise($onRejected);
  25. }
  26. public function always(callable $onFulfilledOrRejected)
  27. {
  28. return $this->promise()->always($onFulfilledOrRejected);
  29. }
  30. public function progress(callable $onProgress)
  31. {
  32. return $this->promise()->progress($onProgress);
  33. }
  34. public function cancel()
  35. {
  36. return $this->promise()->cancel();
  37. }
  38. /**
  39. * @internal
  40. * @see Promise::settle()
  41. */
  42. public function promise()
  43. {
  44. if (null === $this->promise) {
  45. try {
  46. $this->promise = resolve(\call_user_func($this->factory));
  47. } catch (\Throwable $exception) {
  48. $this->promise = new RejectedPromise($exception);
  49. } catch (\Exception $exception) {
  50. $this->promise = new RejectedPromise($exception);
  51. }
  52. }
  53. return $this->promise;
  54. }
  55. }