12
12
use Symfony \Component \OptionsResolver \OptionsResolver ;
13
13
14
14
/**
15
- * Socket Http Client
15
+ * Socket Http Client.
16
16
*
17
17
* Use stream and socket capabilities of the core of PHP to send HTTP requests
18
18
*
@@ -24,20 +24,21 @@ class Client implements HttpClient
24
24
use ResponseReader;
25
25
26
26
private $ config = [
27
- 'remote_socket ' => null ,
28
- 'timeout ' => null ,
29
- 'stream_context_options ' => array () ,
30
- 'stream_context_param ' => array () ,
31
- 'ssl ' => null ,
32
- 'write_buffer_size ' => 8192 ,
33
- 'ssl_method ' => STREAM_CRYPTO_METHOD_TLS_CLIENT
27
+ 'remote_socket ' => null ,
28
+ 'timeout ' => null ,
29
+ 'stream_context_options ' => [] ,
30
+ 'stream_context_param ' => [] ,
31
+ 'ssl ' => null ,
32
+ 'write_buffer_size ' => 8192 ,
33
+ 'ssl_method ' => STREAM_CRYPTO_METHOD_TLS_CLIENT ,
34
34
];
35
35
36
36
/**
37
37
* Constructor.
38
38
*
39
39
* @param ResponseFactory $responseFactory Response factory for creating response
40
- * @param array $config {
40
+ * @param array $config {
41
+ *
41
42
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
42
43
* @var int $timeout Timeout before canceling request
43
44
* @var array $stream_context_options Context options as defined in the PHP documentation
@@ -68,8 +69,8 @@ public function __construct(ResponseFactory $responseFactory = null, array $conf
68
69
*/
69
70
public function sendRequest (RequestInterface $ request )
70
71
{
71
- $ remote = $ this ->config ['remote_socket ' ];
72
- $ useSsl = $ this ->config ['ssl ' ];
72
+ $ remote = $ this ->config ['remote_socket ' ];
73
+ $ useSsl = $ this ->config ['ssl ' ];
73
74
74
75
if (!$ request ->hasHeader ('Connection ' )) {
75
76
$ request = $ request ->withHeader ('Connection ' , 'close ' );
@@ -98,19 +99,19 @@ public function sendRequest(RequestInterface $request)
98
99
}
99
100
100
101
/**
101
- * Create the socket to write request and read response on it
102
+ * Create the socket to write request and read response on it.
102
103
*
103
104
* @param RequestInterface $request Request for
104
105
* @param string $remote Entrypoint for the connection
105
- * @param boolean $useSsl Whether to use ssl or not
106
+ * @param bool $useSsl Whether to use ssl or not
106
107
*
107
108
* @throws NetworkException When the connection fail
108
109
*
109
110
* @return resource Socket resource
110
111
*/
111
112
protected function createSocket (RequestInterface $ request , $ remote , $ useSsl )
112
113
{
113
- $ errNo = null ;
114
+ $ errNo = null ;
114
115
$ errMsg = null ;
115
116
$ socket = @stream_socket_client ($ remote , $ errNo , $ errMsg , floor ($ this ->config ['timeout ' ] / 1000 ), STREAM_CLIENT_CONNECT , $ this ->config ['stream_context ' ]);
116
117
@@ -130,7 +131,7 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
130
131
}
131
132
132
133
/**
133
- * Close the socket, used when having an error
134
+ * Close the socket, used when having an error.
134
135
*
135
136
* @param resource $socket
136
137
*/
@@ -140,7 +141,7 @@ protected function closeSocket($socket)
140
141
}
141
142
142
143
/**
143
- * Return configuration for the socket client
144
+ * Return configuration for the socket client.
144
145
*
145
146
* @param array $config Configuration from user
146
147
*
@@ -165,7 +166,7 @@ protected function configure(array $config = [])
165
166
}
166
167
167
168
/**
168
- * Return remote socket from the request
169
+ * Return remote socket from the request.
169
170
*
170
171
* @param RequestInterface $request
171
172
*
@@ -175,13 +176,13 @@ protected function configure(array $config = [])
175
176
*/
176
177
private function determineRemoteFromRequest (RequestInterface $ request )
177
178
{
178
- if ($ request ->getUri ()->getHost () == "" && !$ request ->hasHeader ('Host ' )) {
179
- throw new NetworkException (" Cannot find connection endpoint for this request " , $ request );
179
+ if ($ request ->getUri ()->getHost () == '' && !$ request ->hasHeader ('Host ' )) {
180
+ throw new NetworkException (' Cannot find connection endpoint for this request ' , $ request );
180
181
}
181
182
182
183
$ host = $ request ->getUri ()->getHost ();
183
- $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () == " https " ? 443 : 80 );
184
- $ endpoint = sprintf (" %s:%s " , $ host , $ port );
184
+ $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () == ' https ' ? 443 : 80 );
185
+ $ endpoint = sprintf (' %s:%s ' , $ host , $ port );
185
186
186
187
// If use the host header if present for the endpoint
187
188
if (empty ($ host ) && $ request ->hasHeader ('Host ' )) {
0 commit comments