8000 [HttpClient] Make HttplugClient implement PSR-17 factories instead of… · symfony/symfony@c3aa36c · GitHub
[go: up one dir, main page]

Skip to content

Commit c3aa36c

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

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

UPGRADE-6.2.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/message-factory" package is not installed. Try running "composer require nyholm/psr7".');
5050
}
5151

52+
if (!interface_exists(RequestFactoryInterface::class)) {
53+
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".');
54+
}
55+
5256
/**
5357
* An adapter to turn a Symfony HttpClientInterface into an Httplug client.
5458
*
@@ -57,7 +61,7 @@
5761
*
5862
* @author Nicolas Grekas <p@tchwork.com>
5963
*/
60-
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface
64+
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, RequestFactory, StreamFactory, UriFactory, ResetInterface
6165
{
6266
private HttpClientInterface $client;
6367
private ResponseFactoryInterface $responseFactory;
@@ -142,8 +146,15 @@ public function wait(float $maxDuration = null, float $idleTimeout = null): int
142146
return $this->waitLoop->wait(null, $maxDuration, $idleTimeout);
143147
}
144148

149+
/**
150+
* @param string $method
151+
* @param UriInterface|string $uri
152+
*/
145153
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface
146154
{
155+
if (2 < \func_num_args()) {
156+
trigger_deprecation('symfony/http-client', '6.2', 'Passing more than 2 arguments to "%s()" is deprecated.', __METHOD__);
157+
}
147158
if ($this->responseFactory instanceof RequestFactoryInterface) {
148159
$request = $this->responseFactory->createRequest($method, $uri);
149160
} elseif (class_exists(Request::class)) {
@@ -156,7 +167,7 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
156167

157168
$request = $request
158169
->withProtocolVersion($protocolVersion)
159-
->withBody($this->createStream($body))
170+
->withBody($this->createStream($body ?? ''))
160171
;
161172

162173
foreach ($headers as $name => $value) {
@@ -166,18 +177,25 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
166177
return $request;
167178
}
168179

169-
public function createStream($body = null): StreamInterface
180+
/**
181+
* @param string $content
182+
*/
183+
public function createStream($content = ''): StreamInterface
170184
{
171-
if ($body instanceof StreamInterface) {
172-
return $body;
185+
if (!\is_string($content)) {
186+
trigger_deprecation('symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, use "createStreamFrom*()" instead.', get_debug_type($content), __METHOD__);
173187
}
174188

175-
if (\is_string($body ?? '')) {
176-
$stream = $this->streamFactory->createStream($body ?? '');
177-
} elseif (\is_resource($body)) {
178-
$stream = $this->streamFactory->createStreamFromResource($body);
189+
if ($content instanceof StreamInterface) {
190+
return $content;
191+
}
192+
193+
if (\is_string($content ?? '')) {
194+
$stream = $this->streamFactory->createStream($content ?? '');
195+
} elseif (\is_resource($content)) {
196+
$stream = $this->streamFactory->createStreamFromResource($content);
179197
} else {
180-
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
198+
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($content)));
181199
}
182200

183201
if ($stream->isSeekable()) {
@@ -187,8 +205,25 @@ public function createStream($body = null): StreamInterface
187205
return $stream;
188206
}
189207

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

src/Symfony/Component/HttpClient/composer.json

Lines changed: 1 addition & 0 deletions
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)
< 2AF4 /div>
0