1 | <?php |
||
20 | class Client implements HttpClient |
||
21 | { |
||
22 | use RequestWriter; |
||
23 | use ResponseReader; |
||
24 | |||
25 | private $config = [ |
||
26 | 'remote_socket' => null, |
||
27 | 'timeout' => null, |
||
28 | 'stream_context_options' => [], |
||
29 | 'stream_context_param' => [], |
||
30 | 'ssl' => null, |
||
31 | 'write_buffer_size' => 8192, |
||
32 | 'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT, |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param ResponseFactory $responseFactory Response factory for creating response |
||
39 | * @param array $config { |
||
40 | * |
||
41 | * @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address) |
||
42 | * @var int $timeout Timeout before canceling request |
||
43 | * @var array $stream_context_options Context options as defined in the PHP documentation |
||
44 | * @var array $stream_context_param Context params as defined in the PHP documentation |
||
45 | * @var bool $ssl Use ssl, default to scheme from request, false if not present |
||
46 | * @var int $write_buffer_size Buffer when writing the request body, defaults to 8192 |
||
47 | * @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLS_CLIENT |
||
48 | * } |
||
49 | */ |
||
50 | 64 | public function __construct(ResponseFactory $responseFactory = null, array $config = []) |
|
51 | { |
||
52 | 64 | if (null === $responseFactory) { |
|
53 | 53 | $responseFactory = MessageFactoryDiscovery::find(); |
|
54 | 53 | } |
|
55 | |||
56 | 64 | $this->responseFactory = $responseFactory; |
|
57 | 64 | $this->config = $this->configure($config); |
|
58 | 64 | } |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 64 | public function sendRequest(RequestInterface $request) |
|
93 | |||
94 | /** |
||
95 | * Create the socket to write request and read response on it. |
||
96 | * |
||
97 | * @param RequestInterface $request Request for |
||
98 | * @param string $remote Entrypoint for the connection |
||
99 | * @param bool $useSsl Whether to use ssl or not |
||
100 | * |
||
101 | * @throws NetworkException When the connection fail |
||
102 | * |
||
103 | * @return resource Socket resource |
||
104 | */ |
||
105 | 63 | protected function createSocket(RequestInterface $request, $remote, $useSsl) |
|
125 | |||
126 | /** |
||
127 | * Close the socket, used when having an error. |
||
128 | * |
||
129 | * @param resource $socket |
||
130 | */ |
||
131 | 1 | protected function closeSocket($socket) |
|
135 | |||
136 | /** |
||
137 | * Return configuration for the socket client. |
||
138 | * |
||
139 | * @param array $config Configuration from user |
||
140 | * |
||
141 | * @return array Configuration resolved |
||
142 | */ |
||
143 | 64 | protected function configure(array $config = []) |
|
160 | |||
161 | /** |
||
162 | * Return remote socket from the request. |
||
163 | * |
||
164 | * @param RequestInterface $request |
||
165 | * |
||
166 | * @throws NetworkException When no remote can be determined from the request |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | 58 | private function determineRemoteFromRequest(RequestInterface $request) |
|
187 | } |
||
188 |