Skip to content

Commit c8208a4

Browse files
[HttpClient] Make HttplugClient implement PSR-17 factories instead of Httplug's
1 parent 8a5b9a2 commit c8208a4

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

UPGRADE-6.2.md

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ HttpFoundation
4141
* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead
4242
* Deprecate calling `JsonResponse::setCallback()`, `Response::setExpires/setLastModified/setEtag()`, `MockArraySessionStorage/NativeSessionStorage::setMetadataBag()`, `NativeSessionStorage::setSaveHandler()` without arguments
4343

44+
HttpClient
45+
----------
46+
47+
* Deprecate implementing `Http\Message\RequestFactory`, `StreamFactory` and `UriFactory` on `HttplugClient`
48+
4449
HttpKernel
4550
----------
4651

src/Symfony/Component/HttpClient/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Make `HttplugClient` implement `Psr\Http\Message\RequestFactoryInterface`, `StreamFactoryInterface` and `UriFactoryInterface`
8+
* Deprecate implementing `Http\Message\RequestFactory`, `StreamFactory` and `UriFactory` on `HttplugClient`
9+
410
6.1
511
---
612

src/Symfony/Component/HttpClient/HttplugClient.php

+42-11
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
*
5858
* @author Nicolas Grekas <p@tchwork.com>
5959
*/
60-
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface
60+
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, RequestFactory, StreamFactory, UriFactory, ResetInterface
6161
{
6262
private HttpClientInterface $client;
6363
private ResponseFactoryInterface $responseFactory;
@@ -142,8 +142,15 @@ public function wait(float $maxDuration = null, float $idleTimeout = null): int
142142
return $this->waitLoop->wait(null, $maxDuration, $idleTimeout);
143143
}
144144

145+
/**
146+
* @param string $method
147+
* @param UriInterface|string $uri
148+
*/
145149
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface
146150
{
151+
if (2 < \func_num_args()) {
152+
trigger_deprecation('symfony/http-client', '6.2', 'Passing more than 2 arguments to "%s()" is deprecated.', __METHOD__);
153+
}
147154
if ($this->responseFactory instanceof RequestFactoryInterface) {
148155
$request = $this->responseFactory->createRequest($method, $uri);
149156
} elseif (class_exists(Request::class)) {
@@ -156,7 +163,7 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
156163

157164
$request = $request
158165
->withProtocolVersion($protocolVersion)
159-
->withBody($this->createStream($body))
166+
->withBody($this->createStream($body ?? ''))
160167
;
161168

162169
foreach ($headers as $name => $value) {
@@ -166,18 +173,25 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
166173
return $request;
167174
}
168175

169-
public function createStream($body = null): StreamInterface
176+
/**
177+
* @param string $content
178+
*/
179+
public function createStream($content = ''): StreamInterface
170180
{
171-
if ($body instanceof StreamInterface) {
172-
return $body;
181+
if (!\is_string($content)) {
182+
trigger_deprecation('symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, use "createStreamFrom*()" instead.', get_debug_type($content), __METHOD__);
173183
}
174184

175-
if (\is_string($body ?? '')) {
176-
$stream = $this->streamFactory->createStream($body ?? '');
177-
} elseif (\is_resource($body)) {
178-
$stream = $this->streamFactory->createStreamFromResource($body);
185+
if ($content instanceof StreamInterface) {
186+
return $content;
187+
}
188+
189+
if (\is_string($content ?? '')) {
190+
$stream = $this->streamFactory->createStream($content ?? '');
191+
} elseif (\is_resource($content)) {
192+
$stream = $this->streamFactory->createStreamFromResource($content);
179193
} else {
180-
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
194+
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($content)));
181195
}
182196

183197
if ($stream->isSeekable()) {
@@ -187,8 +201,25 @@ public function createStream($body = null): StreamInterface
187201
return $stream;
188202
}
189203

190-
public function createUri($uri): UriInterface
204+
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
191205
{
206+
return $this->streamFactory->createStreamFromFile($filename, $mode);
207+
}
208+
209+
public function createStreamFromResource($resource): StreamInterface
210+
{
211+
return $this->streamFactory->createStreamFromResource($resource);
212+
}
213+
214+
/**
215+
* @param string $uri
216+
*/
217+
public function createUri($uri = ''): UriInterface
218+
{
219+
if (!\is_string($uri)) {
220+
trigger_deprecation('symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, pass a string instead.', get_debug_type($uri), __METHOD__);
221+
}
222+
192223
if ($uri instanceof UriInterface) {
193224
return $uri;
194225
}

src/Symfony/Component/HttpClient/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"php-http/async-client-implementation": "*",
1919
"php-http/client-implementation": "*",
2020
"psr/http-client-implementation": "1.0",
21+
"symfony/deprecation-contracts": "^2.1|^3",
2122
"symfony/http-client-implementation": "3.0"
2223
},
2324
"require": {

0 commit comments

Comments
 (0)