diff --git a/UPGRADE-6.2.md b/UPGRADE-6.2.md index cf5aec88ddfee..90af24307a7cb 100644 --- a/UPGRADE-6.2.md +++ b/UPGRADE-6.2.md @@ -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 ---------- diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index 576aa5ba1f523..2fb4537b20b7d 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Component/HttpClient/HttplugClient.php b/src/Symfony/Component/HttpClient/HttplugClient.php index 8245067f06d28..9fdf02992ee08 100644 --- a/src/Symfony/Component/HttpClient/HttplugClient.php +++ b/src/Symfony/Component/HttpClient/HttplugClient.php @@ -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. * @@ -57,7 +61,7 @@ * * @author Nicolas Grekas
*/ -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; @@ -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)) { @@ -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) { @@ -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()) { @@ -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; } diff --git a/src/Symfony/Component/HttpClient/composer.json b/src/Symfony/Component/HttpClient/composer.json index fe9e0f4c98254..ed277b4f0db82 100644 --- a/src/Symfony/Component/HttpClient/composer.json +++ b/src/Symfony/Component/HttpClient/composer.json @@ -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": {