| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Http\Discovery\Exception\NotFoundException; |
| 15 |
use Http\Discovery\Psr17FactoryDiscovery; |
| 16 |
use Nyholm\Psr7\Factory\Psr17Factory; |
| 17 |
use Nyholm\Psr7\Request; |
| 18 |
use Nyholm\Psr7\Uri; |
| 19 |
use Psr\Http\Client\ClientInterface; |
| 20 |
use Psr\Http\Client\NetworkExceptionInterface; |
| 21 |
use Psr\Http\Client\RequestExceptionInterface; |
| 22 |
use Psr\Http\Message\RequestFactoryInterface; |
| 23 |
use Psr\Http\Message\RequestInterface; |
| 24 |
use Psr\Http\Message\ResponseFactoryInterface; |
| 25 |
use Psr\Http\Message\ResponseInterface; |
| 26 |
use Psr\Http\Message\StreamFactoryInterface; |
| 27 |
use Psr\Http\Message\StreamInterface; |
| 28 |
use Psr\Http\Message\UriFactoryInterface; |
| 29 |
use Psr\Http\Message\UriInterface; |
| 30 |
use Symfony\Component\HttpClient\Response\StreamableInterface; |
| 31 |
use Symfony\Component\HttpClient\Response\StreamWrapper; |
| 32 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 33 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 34 |
use Symfony\Contracts\Service\ResetInterface; |
| 35 |
|
| 36 |
if (!interface_exists(RequestFactoryInterface::class)) { |
| 37 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".'); |
| 38 |
} |
| 39 |
|
| 40 |
if (!interface_exists(ClientInterface::class)) { |
| 41 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-client" package is not installed. Try running "composer require psr/http-client".'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* An adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface. |
| 46 |
* |
| 47 |
* Run "composer require psr/http-client" to install the base ClientInterface. Run |
| 48 |
* "composer require nyholm/psr7" to install an efficient implementation of response |
| 49 |
* and stream factories with flex-provided autowiring aliases. |
| 50 |
* |
| 51 |
* @author Nicolas Grekas <[email protected]> |
| 52 |
*/ |
| 53 |
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, ResetInterface |
| 54 |
{ |
| 55 |
private $client; |
| 56 |
private $responseFactory; |
| 57 |
private $streamFactory; |
| 58 |
|
| 59 |
public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null) |
| 60 |
{ |
| 61 |
$this->client = $client ?? HttpClient::create(); |
| 62 |
$this->responseFactory = $responseFactory; |
| 63 |
$this->streamFactory = $streamFactory ?? ($responseFactory instanceof StreamFactoryInterface ? $responseFactory : null); |
| 64 |
|
| 65 |
if (null !== $this->responseFactory && null !== $this->streamFactory) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) { |
| 70 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".'); |
| 71 |
} |
| 72 |
|
| 73 |
try { |
| 74 |
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null; |
| 75 |
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory(); |
| 76 |
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory(); |
| 77 |
} catch (NotFoundException $e) { |
| 78 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been found. Try running "composer require nyholm/psr7".', 0, $e); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* {@inheritdoc} |
| 84 |
*/ |
| 85 |
public function sendRequest(RequestInterface $request): ResponseInterface |
| 86 |
{ |
| 87 |
try { |
| 88 |
$body = $request->getBody(); |
| 89 |
|
| 90 |
if ($body->isSeekable()) { |
| 91 |
$body->seek(0); |
| 92 |
} |
| 93 |
|
| 94 |
$options = [ |
| 95 |
'headers' => $request->getHeaders(), |
| 96 |
'body' => $body->getContents(), |
| 97 |
]; |
| 98 |
|
| 99 |
if ('1.0' === $request->getProtocolVersion()) { |
| 100 |
$options['http_version'] = '1.0'; |
| 101 |
} |
| 102 |
|
| 103 |
$response = $this->client->request($request->getMethod(), (string) $request->getUri(), $options); |
| 104 |
|
| 105 |
$psrResponse = $this->responseFactory->createResponse($response->getStatusCode()); |
| 106 |
|
| 107 |
foreach ($response->getHeaders(false) as $name => $values) { |
| 108 |
foreach ($values as $value) { |
| 109 |
try { |
| 110 |
$psrResponse = $psrResponse->withAddedHeader($name, $value); |
| 111 |
} catch (\InvalidArgumentException $e) { |
| 112 |
// ignore invalid header |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$body = $response instanceof StreamableInterface ? $response->toStream(false) : StreamWrapper::createResource($response, $this->client); |
| 118 |
$body = $this->streamFactory->createStreamFromResource($body); |
| 119 |
|
| 120 |
if ($body->isSeekable()) { |
| 121 |
$body->seek(0); |
| 122 |
} |
| 123 |
|
| 124 |
return $psrResponse->withBody($body); |
| 125 |
} catch (TransportExceptionInterface $e) { |
| 126 |
if ($e instanceof \InvalidArgumentException) { |
| 127 |
throw new Psr18RequestException($e, $request); |
| 128 |
} |
| 129 |
|
| 130 |
throw new Psr18NetworkException($e, $request); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* {@inheritdoc} |
| 136 |
*/ |
| 137 |
public function createRequest(string $method, $uri): RequestInterface |
| 138 |
{ |
| 139 |
if ($this->responseFactory instanceof RequestFactoryInterface) { |
| 140 |
return $this->responseFactory->createRequest($method, $uri); |
| 141 |
} |
| 142 |
|
| 143 |
if (class_exists(Request::class)) { |
| 144 |
return new Request($method, $uri); |
| 145 |
} |
| 146 |
|
| 147 |
if (class_exists(Psr17FactoryDiscovery::class)) { |
| 148 |
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri); |
| 149 |
} |
| 150 |
|
| 151 |
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* {@inheritdoc} |
| 156 |
*/ |
| 157 |
public function createStream(string $content = ''): StreamInterface |
| 158 |
{ |
| 159 |
$stream = $this->streamFactory->createStream($content); |
| 160 |
|
| 161 |
if ($stream->isSeekable()) { |
| 162 |
$stream->seek(0); |
| 163 |
} |
| 164 |
|
| 165 |
return $stream; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* {@inheritdoc} |
| 170 |
*/ |
| 171 |
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 172 |
{ |
| 173 |
return $this->streamFactory->createStreamFromFile($filename, $mode); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* {@inheritdoc} |
| 178 |
*/ |
| 179 |
public function createStreamFromResource($resource): StreamInterface |
| 180 |
{ |
| 181 |
return $this->streamFactory->createStreamFromResource($resource); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* {@inheritdoc} |
| 186 |
*/ |
| 187 |
public function createUri(string $uri = ''): UriInterface |
| 188 |
{ |
| 189 |
if ($this->responseFactory instanceof UriFactoryInterface) { |
| 190 |
return $this->responseFactory->createUri($uri); |
| 191 |
} |
| 192 |
|
| 193 |
if (class_exists(Uri::class)) { |
| 194 |
return new Uri($uri); |
| 195 |
} |
| 196 |
|
| 197 |
if (class_exists(Psr17FactoryDiscovery::class)) { |
| 198 |
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri); |
| 199 |
} |
| 200 |
|
| 201 |
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
| 202 |
} |
| 203 |
|
| 204 |
public function reset() |
| 205 |
{ |
| 206 |
if ($this->client instanceof ResetInterface) { |
| 207 |
$this->client->reset(); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @internal |
| 214 |
*/ |
| 215 |
class Psr18NetworkException extends \RuntimeException implements NetworkExceptionInterface |
| 216 |
{ |
| 217 |
private $request; |
| 218 |
|
| 219 |
public function __construct(TransportExceptionInterface $e, RequestInterface $request) |
| 220 |
{ |
| 221 |
parent::__construct($e->getMessage(), 0, $e); |
| 222 |
$this->request = $request; |
| 223 |
} |
| 224 |
|
| 225 |
public function getRequest(): RequestInterface |
| 226 |
{ |
| 227 |
return $this->request; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @internal |
| 233 |
*/ |
| 234 |
class Psr18RequestException extends \InvalidArgumentException implements RequestExceptionInterface |
| 235 |
{ |
| 236 |
private $request; |
| 237 |
|
| 238 |
public function __construct(TransportExceptionInterface $e, RequestInterface $request) |
| 239 |
{ |
| 240 |
parent::__construct($e->getMessage(), 0, $e); |
| 241 |
$this->request = $request; |
| 242 |
} |
| 243 |
|
| 244 |
public function getRequest(): RequestInterface |
| 245 |
{ |
| 246 |
return $this->request; |
| 247 |
} |
| 248 |
} |
| 249 |
|