1313namespace chillerlan \HTTP \Psr18 ;
1414
1515use chillerlan \HTTP \HTTPOptions ;
16- use chillerlan \HTTP \Psr17 \{RequestFactory , ResponseFactory };
16+ use chillerlan \HTTP \Psr17 \{ResponseFactory };
1717use chillerlan \Settings \SettingsContainerInterface ;
18- use Psr \Http \Message \{RequestFactoryInterface , ResponseFactoryInterface , ResponseInterface };
18+ use Fig \Http \Message \RequestMethodInterface ;
19+ use Psr \Http \Client \ClientInterface ;
20+ use Psr \Http \Message \ResponseFactoryInterface ;
1921use Psr \Log \{LoggerAwareInterface , LoggerAwareTrait , LoggerInterface , NullLogger };
2022
21- use function chillerlan \HTTP \Psr7 \{merge_query , normalize_request_headers };
22- use function chillerlan \HTTP \Psr17 \create_stream ;
23- use function http_build_query , in_array , is_array , is_object , json_encode , strtoupper ;
24-
25- use const PHP_QUERY_RFC1738 ;
26-
27- abstract class HTTPClientAbstract implements HTTPClientInterface, LoggerAwareInterface{
23+ abstract class HTTPClientAbstract implements ClientInterface, LoggerAwareInterface, RequestMethodInterface{
2824 use LoggerAwareTrait;
2925
3026 /**
3127 * @var \chillerlan\HTTP\HTTPOptions
3228 */
3329 protected $ options ;
3430
35- /**
36- * @var \Psr\Http\Message\RequestFactoryInterface
37- */
38- protected $ requestFactory ;
39-
4031 /**
4132 * @var \Psr\Http\Message\ResponseFactoryInterface
4233 */
@@ -46,66 +37,17 @@ abstract class HTTPClientAbstract implements HTTPClientInterface, LoggerAwareInt
4637 * HTTPClientAbstract constructor.
4738 *
4839 * @param \chillerlan\Settings\SettingsContainerInterface|null $options
49- * @param \Psr\Http\Message\RequestFactoryInterface|null $requestFactory
5040 * @param \Psr\Http\Message\ResponseFactoryInterface|null $responseFactory
5141 * @param \Psr\Log\LoggerInterface|null $logger
5242 */
5343 public function __construct (
5444 SettingsContainerInterface $ options = null ,
55- RequestFactoryInterface $ requestFactory = null ,
5645 ResponseFactoryInterface $ responseFactory = null ,
5746 LoggerInterface $ logger = null
5847 ){
5948 $ this ->options = $ options ?? new HTTPOptions ;
60- $ this ->requestFactory = $ requestFactory ?? new RequestFactory ;
6149 $ this ->responseFactory = $ responseFactory ?? new ResponseFactory ;
6250 $ this ->logger = $ logger ?? new NullLogger ;
6351 }
6452
65- /**
66- * @deprecated: messy, under-used, missing flexibility
67- *
68- * @param string $uri
69- * @param string|null $method
70- * @param array|null $query
71- * @param mixed|null $body
72- * @param array|null $headers
73- *
74- * @return \Psr\Http\Message\ResponseInterface
75- */
76- public function request (string $ uri , string $ method = null , array $ query = null , $ body = null , array $ headers = null ):ResponseInterface {
77- $ method = strtoupper ($ method ?? 'GET ' );
78- $ headers = normalize_request_headers ($ headers );
79- $ request = $ this ->requestFactory ->createRequest ($ method , merge_query ($ uri , $ query ?? []));
80-
81- if (in_array ($ method , ['DELETE ' , 'PATCH ' , 'POST ' , 'PUT ' ], true ) && $ body !== null ){
82-
83- if (is_array ($ body ) || is_object ($ body )){
84-
85- if (!isset ($ headers ['Content-type ' ])){
86- $ headers ['Content-type ' ] = 'application/x-www-form-urlencoded ' ;
87- }
88-
89- if ($ headers ['Content-type ' ] === 'application/x-www-form-urlencoded ' ){
90- $ body = http_build_query ($ body , '' , '& ' , PHP_QUERY_RFC1738 );
91- }
92- elseif ($ headers ['Content-type ' ] === 'application/json ' ){
93- $ body = json_encode ($ body );
94- }
95- else {
96- $ body = null ; // @todo
97- }
98-
99- }
100-
101- $ request = $ request ->withBody (create_stream ((string )$ body ));
102- }
103-
104- foreach ($ headers as $ header => $ value ){
105- $ request = $ request ->withAddedHeader ($ header , $ value );
106- }
107-
108- return $ this ->sendRequest ($ request );
109- }
110-
11153}
0 commit comments