Skip to content

Commit a670de1

Browse files
[HttpClient] add StreamableInterface to ease turning responses into PHP streams
1 parent 75031a1 commit a670de1

File tree

12 files changed

+50
-25
lines changed

12 files changed

+50
-25
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `AsyncDecoratorTrait` to ease processing responses without breaking async
88
* added support for pausing responses with a new `pause_handler` callable exposed as an info item
9+
* added `StreamableInterface` to ease turning responses into PHP streams
910

1011
5.1.0
1112
-----

src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Psr\Http\Message\ResponseFactoryInterface;
1616
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
1717
use Psr\Http\Message\StreamFactoryInterface;
18-
use Symfony\Component\HttpClient\Response\CommonResponseTrait;
18+
use Symfony\Component\HttpClient\Response\StreamableInterface;
1919
use Symfony\Component\HttpClient\Response\StreamWrapper;
2020
use Symfony\Component\HttpClient\Response\TraceableResponse;
2121
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@@ -120,7 +120,7 @@ public function createPsr7Response(ResponseInterface $response, bool $buffer = f
120120
}
121121
}
122122

123-
if ($response instanceof TraceableResponse || isset(class_uses($response)[CommonResponseTrait::class])) {
123+
if ($response instanceof StreamableInterface) {
124124
$body = $this->streamFactory->createStreamFromResource($response->toStream(false));
125125
} elseif (!$buffer) {
126126
$body = $this->streamFactory->createStreamFromResource(StreamWrapper::createResource($response, $this->client));

src/Symfony/Component/HttpClient/Psr18Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use Psr\Http\Message\StreamInterface;
2828
use Psr\Http\Message\UriFactoryInterface;
2929
use Psr\Http\Message\UriInterface;
30-
use Symfony\Component\HttpClient\Response\CommonResponseTrait;
30+
use Symfony\Component\HttpClient\Response\StreamableInterface;
3131
use Symfony\Component\HttpClient\Response\StreamWrapper;
3232
use Symfony\Component\HttpClient\Response\TraceableResponse;
3333
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@@ -105,7 +105,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
105105
}
106106
}
107107

108-
$body = $response instanceof TraceableResponse || isset(class_uses($response)[CommonResponseTrait::class]) ? $response->toStream(false) : StreamWrapper::createResource($response, $this->client);
108+
$body = $response instanceof StreamableInterface ? $response->toStream(false) : StreamWrapper::createResource($response, $this->client);
109109
$body = $this->streamFactory->createStreamFromResource($body);
110110

111111
if ($body->isSeekable()) {

src/Symfony/Component/HttpClient/Response/AmpResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @internal
3535
*/
36-
final class AmpResponse implements ResponseInterface
36+
final class AmpResponse implements ResponseInterface, StreamableInterface
3737
{
3838
use CommonResponseTrait;
3939
use TransportResponseTrait;

src/Symfony/Component/HttpClient/Response/AsyncResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Nicolas Grekas <p@tchwork.com>
2727
*/
28-
final class AsyncResponse implements ResponseInterface
28+
final class AsyncResponse implements ResponseInterface, StreamableInterface
2929
{
3030
use CommonResponseTrait;
3131

@@ -95,7 +95,7 @@ public function toStream(bool $throw = true)
9595
}
9696

9797
$handle = function () {
98-
$stream = StreamWrapper::createResource($this->response);
98+
$stream = $this->response instanceof StreamableInterface ? $this->response->toStream(false) : StreamWrapper::createResource($this->response);
9999

100100
return stream_get_meta_data($stream)['wrapper_data']->stream_cast(STREAM_CAST_FOR_SELECT);
101101
};

src/Symfony/Component/HttpClient/Response/CommonResponseTrait.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
use Symfony\Component\HttpClient\Exception\RedirectionException;
1717
use Symfony\Component\HttpClient\Exception\ServerException;
1818
use Symfony\Component\HttpClient\Exception\TransportException;
19-
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
20-
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
21-
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
22-
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2319

2420
/**
2521
* Implements common logic for response classes.
@@ -123,14 +119,7 @@ public function toArray(bool $throw = true): array
123119
}
124120

125121
/**
126-
* Casts the response to a PHP stream resource.
127-
*
128-
* @return resource
129-
*
130-
* @throws TransportExceptionInterface When a network error occurs
131-
* @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached
132-
* @throws ClientExceptionInterface On a 4xx when $throw is true
133-
* @throws ServerExceptionInterface On a 5xx when $throw is true
122+
* {@inheritdoc}
134123
*/
135124
public function toStream(bool $throw = true)
136125
{

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @internal
2727
*/
28-
final class CurlResponse implements ResponseInterface
28+
final class CurlResponse implements ResponseInterface, StreamableInterface
2929
{
3030
use CommonResponseTrait {
3131
getContent as private doGetContent;

src/Symfony/Component/HttpClient/Response/MockResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Nicolas Grekas <p@tchwork.com>
2525
*/
26-
class MockResponse implements ResponseInterface
26+
class MockResponse implements ResponseInterface, StreamableInterface
2727
{
2828
use CommonResponseTrait;
2929
use TransportResponseTrait {

src/Symfony/Component/HttpClient/Response/NativeResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @internal
2626
*/
27-
final class NativeResponse implements ResponseInterface
27+
final class NativeResponse implements ResponseInterface, StreamableInterface
2828
{
2929
use CommonResponseTrait;
3030
use TransportResponseTrait;

src/Symfony/Component/HttpClient/Response/StreamWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class StreamWrapper
4949
*/
5050
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
5151
{
52-
if ($response instanceof TraceableResponse || (\is_callable([$response, 'toStream']) && isset(class_uses($response)[CommonResponseTrait::class]))) {
52+
if ($response instanceof StreamableInterface) {
5353
$stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2);
5454

5555
if ($response !== ($stack[1]['object'] ?? null)) {

0 commit comments

Comments
 (0)