1 | <?php |
||
15 | class ReactPromiseAdapter implements Promise |
||
16 | { |
||
17 | /** |
||
18 | * Promise status |
||
19 | * @var string |
||
20 | */ |
||
21 | private $state = Promise::PENDING; |
||
22 | |||
23 | /** |
||
24 | * Adapted React promise |
||
25 | * @var ReactPromise |
||
26 | */ |
||
27 | private $promise; |
||
28 | |||
29 | /** |
||
30 | * PSR7 received response |
||
31 | * @var ResponseInterface |
||
32 | */ |
||
33 | private $response; |
||
34 | |||
35 | /** |
||
36 | * Execution error |
||
37 | * @var Exception |
||
38 | */ |
||
39 | private $exception; |
||
40 | |||
41 | /** |
||
42 | * React Event Loop used for synchronous processing |
||
43 | * @var LoopInterface |
||
44 | */ |
||
45 | private $loop; |
||
46 | |||
47 | /** |
||
48 | * Initialize the promise |
||
49 | * @param ReactPromise $promise |
||
50 | */ |
||
51 | 108 | public function __construct(ReactPromise $promise) |
|
52 | { |
||
53 | 108 | $promise->then( |
|
54 | function (ResponseInterface $response) { |
||
55 | 105 | $this->state = Promise::FULFILLED; |
|
56 | 105 | $this->response = $response; |
|
57 | 108 | }, |
|
58 | function (Exception $error) { |
||
59 | 3 | $this->state = Promise::REJECTED; |
|
60 | 3 | $this->exception = $error; |
|
61 | 3 | } |
|
62 | 108 | ); |
|
63 | 108 | $this->promise = $promise; |
|
64 | 108 | } |
|
65 | |||
66 | /** |
||
67 | * Allow to apply callable when the promise resolve |
||
68 | * @param callable|null $onFulfilled |
||
69 | * @param callable|null $onRejected |
||
70 | * @return ReactPromiseAdapter |
||
71 | */ |
||
72 | 55 | public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
73 | { |
||
74 | $this->promise->then(function () use ($onFulfilled) { |
||
75 | 53 | if (null !== $onFulfilled) { |
|
76 | 53 | call_user_func($onFulfilled, $this->response); |
|
77 | 53 | } |
|
78 | 55 | }, function () use ($onRejected) { |
|
79 | 2 | if (null !== $onRejected) { |
|
80 | 2 | call_user_func($onRejected, $this->exception); |
|
81 | 2 | } |
|
82 | 55 | }); |
|
83 | 55 | return $this; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 108 | public function getState() |
|
93 | |||
94 | /** |
||
95 | * Set EventLoop used for synchronous processing |
||
96 | * @param LoopInterface $loop |
||
97 | 52 | * @return ReactPromiseAdapter |
|
98 | */ |
||
99 | 52 | public function setLoop(LoopInterface $loop) |
|
104 | |||
105 | 1 | /** |
|
106 | * {@inheritdoc} |
||
107 | 1 | */ |
|
108 | public function wait($unwrap = true) |
||
125 | } |
||
126 |