Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
promise.php
1
<?php
2
10
namespace
Bitrix\Main\Web\Http
;
11
12
use Http\Promise\Promise as PromiseInterface;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
abstract
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
}
146
catch
(
ClientException
$exception
)
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
}
Bitrix\Main\Web\Http\ClientException
Definition
clientexception.php:10
Bitrix\Main\Web\Http\Promise
Definition
promise.php:17
Bitrix\Main\Web\Http\Promise\getId
getId()
Definition
promise.php:166
Bitrix\Main\Web\Http\Promise\wait
wait($unwrap=true)
Definition
promise.php:87
Bitrix\Main\Web\Http\Promise\$id
string $id
Definition
promise.php:34
Bitrix\Main\Web\Http\Promise\$response
ResponseInterface $response
Definition
promise.php:30
Bitrix\Main\Web\Http\Promise\then
then(callable $onFulfilled=null, callable $onRejected=null)
Definition
promise.php:45
Bitrix\Main\Web\Http\Promise\$state
string $state
Definition
promise.php:18
Bitrix\Main\Web\Http\Promise\fulfill
fulfill(ResponseInterface $response)
Definition
promise.php:110
Bitrix\Main\Web\Http\Promise\$handler
$handler
Definition
promise.php:26
Bitrix\Main\Web\Http\Promise\$onFulfilled
$onFulfilled
Definition
promise.php:21
Bitrix\Main\Web\Http\Promise\$exception
ClientException $exception
Definition
promise.php:32
Bitrix\Main\Web\Http\Promise\__construct
__construct($handler, Queue $queue)
Definition
promise.php:36
Bitrix\Main\Web\Http\Promise\$queue
Queue $queue
Definition
promise.php:28
Bitrix\Main\Web\Http\Promise\getRequest
getRequest()
Definition
promise.php:156
Bitrix\Main\Web\Http\Promise\reject
reject(ClientException $exception)
Definition
promise.php:133
Bitrix\Main\Web\Http\Promise\$onRejected
$onRejected
Definition
promise.php:24
Bitrix\Main\Web\Http\Promise\getHandler
getHandler()
Bitrix\Main\Web\Http\Promise\getState
getState()
Definition
promise.php:79
Bitrix\Main\Web\Http\Queue
Definition
queue.php:15
Bitrix\Main\Web\Http
Definition
clientexception.php:3
modules
main
lib
web
http
promise.php
Создано системой
1.10.0