[HttpClient] In StreamWrapper::createResource use the more efficient Response::toStream method if safe and available

This commit is contained in:
Chris Tanaskoski 2019-12-19 08:07:53 +01:00 committed by Nicolas Grekas
parent e9f0cfe9db
commit e2c2397f1e

View File

@ -49,6 +49,14 @@ class StreamWrapper
*/
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
{
if (null === $client && \is_callable([$response, 'toStream']) && isset(class_uses($response)[ResponseTrait::class])) {
$stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if ($response !== ($stack[1]['object'] ?? null)) {
return $response->toStream(false);
}
}
if (null === $client && !method_exists($response, 'stream')) {
throw new \InvalidArgumentException(sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__));
}