1 | <?php |
||
22 | class Client implements HttpClient |
||
23 | { |
||
24 | use RequestWriter; |
||
25 | use ResponseReader; |
||
26 | |||
27 | private $config = [ |
||
28 | 'remote_socket' => null, |
||
29 | 'timeout' => null, |
||
30 | 'stream_context_options' => [], |
||
31 | 'stream_context_param' => [], |
||
32 | 'ssl' => null, |
||
33 | 'write_buffer_size' => 8192, |
||
34 | 'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param array $config { |
||
41 | * |
||
42 | * @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address) |
||
43 | * @var int $timeout Timeout before canceling request |
||
44 | * @var array $stream_context_options Context options as defined in the PHP documentation |
||
45 | * @var array $stream_context_param Context params as defined in the PHP documentation |
||
46 | * @var bool $ssl Use ssl, default to scheme from request, false if not present |
||
47 | * @var int $write_buffer_size Buffer when writing the request body, defaults to 8192 |
||
48 | * @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT |
||
49 | * } |
||
50 | */ |
||
51 | 64 | public function __construct($config1 = [], $config2 = null, array $config = []) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 64 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
97 | |||
98 | /** |
||
99 | * Create the socket to write request and read response on it. |
||
100 | * |
||
101 | * @param RequestInterface $request Request for |
||
102 | * @param string $remote Entrypoint for the connection |
||
103 | * @param bool $useSsl Whether to use ssl or not |
||
104 | * |
||
105 | * @throws ConnectionException|SSLConnectionException When the connection fail |
||
106 | * |
||
107 | * @return resource Socket resource |
||
108 | */ |
||
109 | 63 | protected function createSocket(RequestInterface $request, string $remote, bool $useSsl) |
|
131 | |||
132 | /** |
||
133 | * Close the socket, used when having an error. |
||
134 | * |
||
135 | * @param resource $socket |
||
136 | */ |
||
137 | 1 | protected function closeSocket($socket) |
|
141 | |||
142 | /** |
||
143 | * Return configuration for the socket client. |
||
144 | * |
||
145 | * @param array $config Configuration from user |
||
146 | * |
||
147 | * @return array Configuration resolved |
||
148 | */ |
||
149 | 64 | protected function configure(array $config = []) |
|
150 | { |
||
151 | 64 | $resolver = new OptionsResolver(); |
|
152 | 64 | $resolver->setDefaults($this->config); |
|
153 | $resolver->setDefault('stream_context', function (Options $options) { |
||
154 | 64 | return stream_context_create($options['stream_context_options'], $options['stream_context_param']); |
|
155 | 64 | }); |
|
156 | |||
157 | 64 | $resolver->setDefault('timeout', ini_get('default_socket_timeout') * 1000); |
|
158 | |||
159 | 64 | $resolver->setAllowedTypes('stream_context_options', 'array'); |
|
160 | 64 | $resolver->setAllowedTypes('stream_context_param', 'array'); |
|
161 | 64 | $resolver->setAllowedTypes('stream_context', 'resource'); |
|
162 | 64 | $resolver->setAllowedTypes('ssl', ['bool', 'null']); |
|
163 | |||
164 | 64 | return $resolver->resolve($config); |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Return remote socket from the request. |
||
169 | * |
||
170 | * @throws InvalidRequestException When no remote can be determined from the request |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 55 | private function determineRemoteFromRequest(RequestInterface $request) |
|
191 | } |
||
192 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.