Skip to content

[HttpClient] Make HttplugClient implement PSR-17 factories instead of Httplug's #47832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ HttpFoundation
* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead
* Deprecate calling `JsonResponse::setCallback()`, `Response::setExpires/setLastModified/setEtag()`, `MockArraySessionStorage/NativeSessionStorage::setMetadataBag()`, `NativeSessionStorage::setSaveHandler()` without arguments

HttpClient
----------

* Deprecate implementing `Http\Message\RequestFactory`, `StreamFactory` and `UriFactory` on `HttplugClient`

HttpKernel
----------

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.2
---

* Make `HttplugClient` implement `Psr\Http\Message\RequestFactoryInterface`, `StreamFactoryInterface` and `UriFactoryInterface`
* Deprecate implementing `Http\Message\RequestFactory`, `StreamFactory` and `UriFactory` on `HttplugClient`

6.1
---

Expand Down
57 changes: 46 additions & 11 deletions src/Symfony/Component/HttpClient/HttplugClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
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".');
}

if (!interface_exists(RequestFactoryInterface::class)) {
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".');
}

/**
* An adapter to turn a Symfony HttpClientInterface into an Httplug client.
*
Expand All @@ -57,7 +61,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, RequestFactory, StreamFactory, UriFactory, ResetInterface
{
private HttpClientInterface $client;
private ResponseFactoryInterface $responseFactory;
Expand Down Expand Up @@ -142,8 +146,15 @@ public function wait(float $maxDuration = null, float $idleTimeout = null): int
return $this->waitLoop->wait(null, $maxDuration, $idleTimeout);
}

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

$request = $request
->withProtocolVersion($protocolVersion)
->withBody($this->createStream($body))
->withBody($this->createStream($body ?? ''))
;

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

public function createStream($body = null): StreamInterface
/**
* @param string $content
*/
public function createStream($content = ''): StreamInterface
{
if ($body instanceof StreamInterface) {
return $body;
if (!\is_string($content)) {
trigger_deprecation('symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, use "createStreamFrom*()" instead.', get_debug_type($content), __METHOD__);
}

if (\is_string($body ?? '')) {
$stream = $this->streamFactory->createStream($body ?? '');
} elseif (\is_resource($body)) {
$stream = $this->streamFactory->createStreamFromResource($body);
if ($content instanceof StreamInterface) {
return $content;
}

if (\is_string($content ?? '')) {
$stream = $this->streamFactory->createStream($content ?? '');
} elseif (\is_resource($content)) {
$stream = $this->streamFactory->createStreamFromResource($content);
} else {
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($content)));
}

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

public function createUri($uri): UriInterface
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return $this->streamFactory->createStreamFromFile($filename, $mode);
}

public function createStreamFromResource($resource): StreamInterface
{
return $this->streamFactory->createStreamFromResource($resource);
}

/**
* @param string $uri
*/
public function createUri($uri = ''): UriInterface
{
if (!\is_string($uri)) {
trigger_deprecation('symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, pass a string instead.', get_debug_type($uri), __METHOD__);
}

if ($uri instanceof UriInterface) {
return $uri;
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"php-http/async-client-implementation": "*",
"php-http/client-implementation": "*",
"psr/http-client-implementation": "1.0",
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/http-client-implementation": "3.0"
},
"require": {
Expand Down