11
11
use Http \Client \HttpAsyncClient ;
12
12
use Http \Client \Exception \HttpException ;
13
13
use Http \Client \Exception \RequestException ;
14
- use Http \Promise \Promise ;
15
14
use Http \Message \MessageFactory ;
16
15
use Psr \Http \Message \RequestInterface ;
17
16
use Psr \Http \Message \StreamInterface ;
18
17
19
18
/**
20
- * Client for the React promise implementation
19
+ * Client for the React promise implementation.
20
+ *
21
21
* @author Stéphane Hulard <stephane@hlrd.me>
22
22
*/
23
23
class Client implements HttpClient, HttpAsyncClient
24
24
{
25
25
/**
26
- * React HTTP client
26
+ * React HTTP client.
27
+ *
27
28
* @var Client
28
29
*/
29
30
private $ client ;
30
31
31
32
/**
32
- * React event loop
33
+ * React event loop.
34
+ *
33
35
* @var LoopInterface
34
36
*/
35
37
private $ loop ;
36
38
37
39
/**
38
- * HttpPlug message factory
40
+ * HttpPlug message factory.
41
+ *
39
42
* @var MessageFactory
40
43
*/
41
44
private $ messageFactory ;
42
45
43
46
/**
44
- * Initialize the React client
47
+ * Initialize the React client.
48
+ *
45
49
* @param LoopInterface|null $loop React Event loop
46
50
* @param Resolver $resolver React async DNS resolver
47
51
* @param ReactClient $client React client to use
@@ -51,14 +55,13 @@ public function __construct(
51
55
LoopInterface $ loop = null ,
52
56
ReactClient $ client = null
53
57
) {
54
- $ this ->loop = null === $ loop ?ReactFactory::buildEventLoop ():$ loop ;
55
- if (null === $ client ) {
56
- $ this ->client = ReactFactory::buildHttpClient ($ this ->loop );
57
- } elseif (null === $ loop ) {
58
+ if (null !== $ client && null === $ loop ) {
58
59
throw new \RuntimeException (
59
- " You must give a LoopInterface instance with the Client "
60
+ ' You must give a LoopInterface instance with the Client '
60
61
);
61
62
}
63
+ $ this ->loop = (null !== $ loop ) ?: ReactFactory::buildEventLoop ();
64
+ $ this ->client = (null !== $ client ) ?: ReactFactory::buildHttpClient ($ this ->loop );
62
65
63
66
$ this ->messageFactory = $ messageFactory ;
64
67
}
@@ -78,70 +81,70 @@ public function sendRequest(RequestInterface $request)
78
81
*/
79
82
public function sendAsyncRequest (RequestInterface $ request )
80
83
{
81
- $ requestStream = $ this ->buildReactRequest ($ request );
84
+ $ reactRequest = $ this ->buildReactRequest ($ request );
82
85
$ deferred = new Deferred ();
83
86
84
- $ requestStream ->on ('error ' , function (\Exception $ error ) use ($ deferred , $ request ) {
87
+ $ reactRequest ->on ('error ' , function (\Exception $ error ) use ($ deferred , $ request ) {
85
88
$ deferred ->reject (new RequestException (
86
89
$ error ->getMessage (),
87
90
$ request ,
88
91
$ error
89
92
));
90
93
});
91
- $ requestStream ->on ('response ' , function (ReactResponse $ response = null ) use ($ deferred , $ requestStream , $ request ) {
94
+ $ reactRequest ->on ('response ' , function (ReactResponse $ reactResponse = null ) use ($ deferred , $ reactRequest , $ request ) {
92
95
$ bodyStream = null ;
93
- $ response ->on ('data ' , function ($ data ) use (&$ bodyStream ) {
96
+ $ reactResponse ->on ('data ' , function ($ data ) use (&$ bodyStream ) {
94
97
if ($ data instanceof StreamInterface) {
95
98
$ bodyStream = $ data ;
96
99
} else {
97
100
$ bodyStream ->write ($ data );
98
101
}
99
102
});
100
103
101
- $ response ->on ('end ' , function (\Exception $ error = null ) use ($ deferred , $ request , $ response , &$ bodyStream ) {
104
+ $ reactResponse ->on ('end ' , function (\Exception $ error = null ) use ($ deferred , $ request , $ reactResponse , &$ bodyStream ) {
102
105
$ bodyStream ->rewind ();
103
- $ psr7Response = $ this ->buildResponse (
104
- $ response ,
106
+ $ response = $ this ->buildResponse (
107
+ $ reactResponse ,
105
108
$ bodyStream
106
109
);
107
110
if (null !== $ error ) {
108
111
$ deferred ->reject (new HttpException (
109
112
$ error ->getMessage (),
110
113
$ request ,
111
- $ psr7Response ,
114
+ $ response ,
112
115
$ error
113
116
));
114
117
} else {
115
- $ deferred ->resolve ($ psr7Response );
118
+ $ deferred ->resolve ($ response );
116
119
}
117
120
});
118
121
});
119
122
120
- $ requestStream ->end ((string )$ request ->getBody ());
123
+ $ reactRequest ->end ((string ) $ request ->getBody ());
121
124
122
- $ promise = new ReactPromiseAdapter ($ deferred ->promise ());
125
+ $ promise = new Promise ($ deferred ->promise ());
123
126
$ promise ->setLoop ($ this ->loop );
127
+
124
128
return $ promise ;
125
129
}
126
130
127
131
/**
128
- * Build a React request from the PSR7 RequestInterface
129
- * @param RequestInterface $request
132
+ * Build a React request from the PSR7 RequestInterface.
133
+ *
134
+ * @param RequestInterface $request
135
+ *
130
136
* @return ReactRequest
131
137
*/
132
138
private function buildReactRequest (RequestInterface $ request )
133
139
{
134
140
$ headers = [];
135
141
foreach ($ request ->getHeaders () as $ name => $ value ) {
136
- $ headers [$ name ] = (is_array ($ value )?$ value [0 ]:$ value );
137
- }
138
- if ($ request ->getBody ()->getSize () > 0 ) {
139
- $ headers ['Content-Length ' ] = $ request ->getBody ()->getSize ();
142
+ $ headers [$ name ] = (is_array ($ value ) ? $ value [0 ] : $ value );
140
143
}
141
144
142
145
$ reactRequest = $ this ->client ->request (
143
146
$ request ->getMethod (),
144
- (string )$ request ->getUri (),
147
+ (string ) $ request ->getUri (),
145
148
$ headers ,
146
149
$ request ->getProtocolVersion ()
147
150
);
@@ -150,15 +153,18 @@ private function buildReactRequest(RequestInterface $request)
150
153
}
151
154
152
155
/**
153
- * Transform a React Response to a valid PSR7 ResponseInterface instance
154
- * @param ReactResponse $response
156
+ * Transform a React Response to a valid PSR7 ResponseInterface instance.
157
+ *
158
+ * @param ReactResponse $response
159
+ *
155
160
* @return ResponseInterface
156
161
*/
157
162
private function buildResponse (
158
163
ReactResponse $ response ,
159
164
StreamInterface $ body
160
165
) {
161
166
$ body ->rewind ();
167
+
162
168
return $ this ->messageFactory ->createResponse (
163
169
$ response ->getCode (),
164
170
$ response ->getReasonPhrase (),
0 commit comments