From 0cacffae7a6fc2fe1d83b6de3063b0892279961c Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 18 Jul 2020 18:07:20 +0100 Subject: [PATCH 1/4] Bumped version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 164c2b6..7e746f0 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.2.x-dev" + "dev-master": "2.3.x-dev" } } } From a95df4a060dc75569a6b5c3b429c3300d4377bfb Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 18 Jul 2020 18:08:38 +0100 Subject: [PATCH 2/4] Fix broken HttpMethodsClient with Psr\Http\Message\RequestFactoryInterface --- src/HttpMethodsClient.php | 70 +++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index bbe95be..a53e0b3 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -9,6 +9,9 @@ use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UriInterface; final class HttpMethodsClient implements HttpMethodsClientInterface { @@ -22,10 +25,15 @@ final class HttpMethodsClient implements HttpMethodsClientInterface */ private $requestFactory; + /** + * @var StreamFactoryInterface|null + */ + private $streamFactory; + /** * @param RequestFactory|RequestFactoryInterface */ - public function __construct(ClientInterface $httpClient, $requestFactory) + public function __construct(ClientInterface $httpClient, $requestFactory, StreamFactoryInterface $streamFactory = null) { if (!$requestFactory instanceof RequestFactory && !$requestFactory instanceof RequestFactoryInterface) { throw new \TypeError( @@ -33,8 +41,13 @@ public function __construct(ClientInterface $httpClient, $requestFactory) ); } + if (!$requestFactory instanceof RequestFactory && null === $streamFactory) { + @trigger_error(sprintf('Passing a %s without a %s to %s::__construct() is deprecated as of version 2.3 and will be disallowed in version 3.0. A stream factory is required to create a request with a string body.', RequestFactoryInterface::class, StreamFactoryInterface::class, self::class)); + } + $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; + $this->streamFactory = $streamFactory; } public function get($uri, array $headers = []): ResponseInterface @@ -79,12 +92,55 @@ public function options($uri, array $headers = [], $body = null): ResponseInterf public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface { - return $this->sendRequest($this->requestFactory->createRequest( - $method, - $uri, - $headers, - $body - )); + if (!is_string($uri) && !$uri instanceof UriInterface) { + throw new \TypeError( + sprintf('%s::send(): Argument #2 ($uri) must be of type string|%s, %s given', self::class, UriInterface::class, get_debug_type($uri)) + ); + } + + if (!is_string($body) && !$body instanceof StreamInterface && null !== $body) { + throw new \TypeError( + sprintf('%s::send(): Argument #4 ($body) must be of type string|%s|null, %s given', self::class, StreamInterface::class, get_debug_type($body)) + ); + } + + return $this->sendRequest( + self::createRequest($method, $uri, $headers, $body) + ); + } + + /** + * @param string|UriInterface $uri + * @param string|StreamInterface|null $body + */ + private function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface + { + if ($this->requestFactory instanceof RequestFactory) { + return = $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; } public function sendRequest(RequestInterface $request): ResponseInterface From 9d1d462f6f165f3a6781705fb7f93b1a59b86586 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 18 Jul 2020 18:12:05 +0100 Subject: [PATCH 3/4] Fixed syntax error --- src/HttpMethodsClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index a53e0b3..a732bdc 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -116,7 +116,7 @@ public function send(string $method, $uri, array $headers = [], $body = null): R private function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { if ($this->requestFactory instanceof RequestFactory) { - return = $this->requestFactory->createRequest( + return $this->requestFactory->createRequest( $method, $uri, $headers, From e907ad7876cc27ca20dcebde7a84a9b3895255c9 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 19 Jul 2020 10:12:22 +0100 Subject: [PATCH 4/4] Allow the empty string through without a stream factory --- src/HttpMethodsClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index a732bdc..26c253d 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -42,7 +42,7 @@ public function __construct(ClientInterface $httpClient, $requestFactory, Stream } if (!$requestFactory instanceof RequestFactory && null === $streamFactory) { - @trigger_error(sprintf('Passing a %s without a %s to %s::__construct() is deprecated as of version 2.3 and will be disallowed in version 3.0. A stream factory is required to create a request with a string body.', RequestFactoryInterface::class, StreamFactoryInterface::class, self::class)); + @trigger_error(sprintf('Passing a %s without a %s to %s::__construct() is deprecated as of version 2.3 and will be disallowed in version 3.0. A stream factory is required to create a request with a non-empty string body.', RequestFactoryInterface::class, StreamFactoryInterface::class, self::class)); } $this->httpClient = $httpClient; @@ -124,8 +124,8 @@ private function createRequest(string $method, $uri, array $headers = [], $body ); } - if (is_string($body) && null === $this->streamFactory) { - throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a string body.'); + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); } $request = $this->requestFactory->createRequest($method, $uri); @@ -134,7 +134,7 @@ private function createRequest(string $method, $uri, array $headers = [], $body $request = $request->withHeader($key, $value); } - if (null !== $body) { + if (null !== $body && '' !== $body) { $request = $request->withBody( is_string($body) ? $this->streamFactory->createStream($body) : $body );