Skip to content

Commit e8f06b5

Browse files
[HttpClient] add StreamableInterface to ease turning responses into PHP streams
1 parent 61aaa98 commit e8f06b5

12 files changed

+50
-27
lines changed

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
-----

Internal/HttplugWaitLoop.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
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;
20-
use Symfony\Component\HttpClient\Response\TraceableResponse;
2120
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2221
use Symfony\Contracts\HttpClient\HttpClientInterface;
2322
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -120,7 +119,7 @@ public function createPsr7Response(ResponseInterface $response, bool $buffer = f
120119
}
121120
}
122121

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

Psr18Client.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
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;
32-
use Symfony\Component\HttpClient\Response\TraceableResponse;
3332
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
3433
use Symfony\Contracts\HttpClient\HttpClientInterface;
3534

@@ -105,7 +104,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
105104
}
106105
}
107106

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

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

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;

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 <[email protected]>
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
};

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
{

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;

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 <[email protected]>
2525
*/
26-
class MockResponse implements ResponseInterface
26+
class MockResponse implements ResponseInterface, StreamableInterface
2727
{
2828
use CommonResponseTrait;
2929
use TransportResponseTrait {

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;

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)