Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
promise.php
1<?php
2
10namespace Bitrix\Main\Web\Http;
11
12use Http\Promise\Promise as PromiseInterface;
13use Psr\Http\Message\RequestInterface;
14use Psr\Http\Message\ResponseInterface;
15
16abstract class Promise implements PromiseInterface
17{
18 protected string $state = self::PENDING;
19
21 protected $onFulfilled = [];
22
24 protected $onRejected = [];
25
26 protected $handler;
27
28 protected Queue $queue;
29
30 protected ResponseInterface $response;
31
32 protected ?ClientException $exception = null;
33
34 protected string $id = '';
35
36 public function __construct($handler, Queue $queue)
37 {
38 $this->handler = $handler;
39 $this->queue = $queue;
40 }
41
45 public function then(callable $onFulfilled = null, callable $onRejected = null)
46 {
47 $state = $this->getState();
48
49 if ($onFulfilled)
50 {
51 if ($state == self::PENDING)
52 {
53 $this->onFulfilled[] = $onFulfilled;
54 }
55 elseif ($state == self::FULFILLED)
56 {
57 call_user_func($onFulfilled, $this->response);
58 }
59 }
60
61 if ($onRejected)
62 {
63 if ($state == self::PENDING)
64 {
65 $this->onRejected[] = $onRejected;
66 }
67 elseif ($state == self::REJECTED)
68 {
69 $this->exception = call_user_func($onRejected, $this->exception);
70 }
71 }
72
73 return new static($this->handler, $this->queue);
74 }
75
79 public function getState()
80 {
81 return $this->state;
82 }
83
87 public function wait($unwrap = true)
88 {
89 $this->queue->wait($this);
90
91 if ($unwrap)
92 {
93 if ($this->getState() == self::REJECTED)
94 {
95 throw $this->exception;
96 }
97
98 return $this->response;
99 }
100
101 return null;
102 }
103
110 public function fulfill(ResponseInterface $response): void
111 {
112 $this->state = self::FULFILLED;
113 $this->response = $response;
114
115 while (!empty($this->onFulfilled))
116 {
117 $callback = array_shift($this->onFulfilled);
118 $response = call_user_func($callback, $this->response);
119
120 if ($response instanceof ResponseInterface)
121 {
122 $this->response = $response;
123 }
124 }
125 }
126
133 public function reject(ClientException $exception): void
134 {
135 $this->state = self::REJECTED;
136 $this->exception = $exception;
137
138 while (!empty($this->onRejected))
139 {
140 $callback = array_shift($this->onRejected);
141 try
142 {
143 $exception = call_user_func($callback, $exception);
144 $this->exception = $exception;
145 }
147 {
148 $this->exception = $exception;
149 }
150 }
151 }
152
156 public function getRequest(): RequestInterface
157 {
158 return $this->handler->getRequest();
159 }
160
166 public function getId(): string
167 {
168 return $this->id;
169 }
170
171 abstract public function getHandler();
172}
ResponseInterface $response
Definition promise.php:30
then(callable $onFulfilled=null, callable $onRejected=null)
Definition promise.php:45
fulfill(ResponseInterface $response)
Definition promise.php:110
ClientException $exception
Definition promise.php:32
__construct($handler, Queue $queue)
Definition promise.php:36
reject(ClientException $exception)
Definition promise.php:133