| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Http\Discovery\Psr17Factory; |
| 15 |
use Http\Discovery\Psr17FactoryDiscovery; |
| 16 |
use Nyholm\Psr7\Factory\Psr17Factory as NyholmPsr17Factory; |
| 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\Internal\HttplugWaitLoop; |
| 31 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 32 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 33 |
use Symfony\Contracts\Service\ResetInterface; |
| 34 |
|
| 35 |
if (!interface_exists(ClientInterface::class)) { |
| 36 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-client" package is not installed. Try running "composer require php-http/discovery psr/http-client-implementation:*".'); |
| 37 |
} |
| 38 |
|
| 39 |
if (!interface_exists(RequestFactoryInterface::class)) { |
| 40 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require php-http/discovery psr/http-factory-implementation:*".'); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, ResetInterface |
| 52 |
{ |
| 53 |
private HttpClientInterface $client; |
| 54 |
private ResponseFactoryInterface $responseFactory; |
| 55 |
private StreamFactoryInterface $streamFactory; |
| 56 |
private bool $autoUpgradeHttpVersion = true; |
| 57 |
|
| 58 |
public function __construct(?HttpClientInterface $client = null, ?ResponseFactoryInterface $responseFactory = null, ?StreamFactoryInterface $streamFactory = null) |
| 59 |
{ |
| 60 |
$this->client = $client ?? HttpClient::create(); |
| 61 |
$streamFactory ??= $responseFactory instanceof StreamFactoryInterface ? $responseFactory : null; |
| 62 |
|
| 63 |
if (null === $responseFactory || null === $streamFactory) { |
| 64 |
if (class_exists(Psr17Factory::class)) { |
| 65 |
$psr17Factory = new Psr17Factory(); |
| 66 |
} elseif (class_exists(NyholmPsr17Factory::class)) { |
| 67 |
$psr17Factory = new NyholmPsr17Factory(); |
| 68 |
} else { |
| 69 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".'); |
| 70 |
} |
| 71 |
|
| 72 |
$responseFactory ??= $psr17Factory; |
| 73 |
$streamFactory ??= $psr17Factory; |
| 74 |
} |
| 75 |
|
| 76 |
$this->responseFactory = $responseFactory; |
| 77 |
$this->streamFactory = $streamFactory; |
| 78 |
} |
| 79 |
|
| 80 |
public function withOptions(array $options): static |
| 81 |
{ |
| 82 |
$clone = clone $this; |
| 83 |
if (\array_key_exists('auto_upgrade_http_version', $options)) { |
| 84 |
$clone->autoUpgradeHttpVersion = $options['auto_upgrade_http_version']; |
| 85 |
unset($options['auto_upgrade_http_version']); |
| 86 |
} |
| 87 |
$clone->client = $clone->client->withOptions($options); |
| 88 |
|
| 89 |
return $clone; |
| 90 |
} |
| 91 |
|
| 92 |
public function sendRequest(RequestInterface $request): ResponseInterface |
| 93 |
{ |
| 94 |
try { |
| 95 |
$body = $request->getBody(); |
| 96 |
$headers = $request->getHeaders(); |
| 97 |
|
| 98 |
$size = $request->getHeader('content-length')[0] ?? -1; |
| 99 |
if (0 > $size && 0 < $size = $body->getSize() ?? -1) { |
| 100 |
$headers['Content-Length'] = [$size]; |
| 101 |
} |
| 102 |
|
| 103 |
if (0 === $size) { |
| 104 |
$body = ''; |
| 105 |
} elseif (0 < $size && $size < 1 << 21) { |
| 106 |
if ($body->isSeekable()) { |
| 107 |
try { |
| 108 |
$body->seek(0); |
| 109 |
} catch (\RuntimeException) { |
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$body = $body->getContents(); |
| 115 |
} else { |
| 116 |
$body = static function (int $size) use ($body) { |
| 117 |
if ($body->isSeekable()) { |
| 118 |
try { |
| 119 |
$body->seek(0); |
| 120 |
} catch (\RuntimeException) { |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
while (!$body->eof()) { |
| 126 |
yield $body->read($size); |
| 127 |
} |
| 128 |
}; |
| 129 |
} |
| 130 |
|
| 131 |
$options = [ |
| 132 |
'headers' => $headers, |
| 133 |
'body' => $body, |
| 134 |
]; |
| 135 |
|
| 136 |
if (!$this->autoUpgradeHttpVersion || '1.0' === $request->getProtocolVersion()) { |
| 137 |
$options['http_version'] = $request->getProtocolVersion(); |
| 138 |
} |
| 139 |
|
| 140 |
$response = $this->client->request($request->getMethod(), (string) $request->getUri(), $options); |
| 141 |
|
| 142 |
return HttplugWaitLoop::createPsr7Response($this->responseFactory, $this->streamFactory, $this->client, $response, false); |
| 143 |
} catch (TransportExceptionInterface $e) { |
| 144 |
if ($e instanceof \InvalidArgumentException) { |
| 145 |
throw new Psr18RequestException($e, $request); |
| 146 |
} |
| 147 |
|
| 148 |
throw new Psr18NetworkException($e, $request); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function createRequest(string $method, $uri): RequestInterface |
| 153 |
{ |
| 154 |
if ($this->responseFactory instanceof RequestFactoryInterface) { |
| 155 |
return $this->responseFactory->createRequest($method, $uri); |
| 156 |
} |
| 157 |
|
| 158 |
if (class_exists(Psr17FactoryDiscovery::class)) { |
| 159 |
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri); |
| 160 |
} |
| 161 |
|
| 162 |
if (class_exists(Request::class)) { |
| 163 |
return new Request($method, $uri); |
| 164 |
} |
| 165 |
|
| 166 |
throw new \LogicException(\sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__)); |
| 167 |
} |
| 168 |
|
| 169 |
public function createStream(string $content = ''): StreamInterface |
| 170 |
{ |
| 171 |
$stream = $this->streamFactory->createStream($content); |
| 172 |
|
| 173 |
if ($stream->isSeekable()) { |
| 174 |
try { |
| 175 |
$stream->seek(0); |
| 176 |
} catch (\RuntimeException) { |
| 177 |
|
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return $stream; |
| 182 |
} |
| 183 |
|
| 184 |
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 185 |
{ |
| 186 |
return $this->streamFactory->createStreamFromFile($filename, $mode); |
| 187 |
} |
| 188 |
|
| 189 |
public function createStreamFromResource($resource): StreamInterface |
| 190 |
{ |
| 191 |
return $this->streamFactory->createStreamFromResource($resource); |
| 192 |
} |
| 193 |
|
| 194 |
public function createUri(string $uri = ''): UriInterface |
| 195 |
{ |
| 196 |
if ($this->responseFactory instanceof UriFactoryInterface) { |
| 197 |
return $this->responseFactory->createUri($uri); |
| 198 |
} |
| 199 |
|
| 200 |
if (class_exists(Psr17FactoryDiscovery::class)) { |
| 201 |
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri); |
| 202 |
} |
| 203 |
|
| 204 |
if (class_exists(Uri::class)) { |
| 205 |
return new Uri($uri); |
| 206 |
} |
| 207 |
|
| 208 |
throw new \LogicException(\sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__)); |
| 209 |
} |
| 210 |
|
| 211 |
public function reset(): void |
| 212 |
{ |
| 213 |
if ($this->client instanceof ResetInterface) { |
| 214 |
$this->client->reset(); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
class Psr18NetworkException extends \RuntimeException implements NetworkExceptionInterface |
| 223 |
{ |
| 224 |
public function __construct( |
| 225 |
TransportExceptionInterface $e, |
| 226 |
private RequestInterface $request, |
| 227 |
) { |
| 228 |
parent::__construct($e->getMessage(), 0, $e); |
| 229 |
} |
| 230 |
|
| 231 |
public function getRequest(): RequestInterface |
| 232 |
{ |
| 233 |
return $this->request; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
class Psr18RequestException extends \InvalidArgumentException implements RequestExceptionInterface |
| 241 |
{ |
| 242 |
public function __construct( |
| 243 |
TransportExceptionInterface $e, |
| 244 |
private RequestInterface $request, |
| 245 |
) { |
| 246 |
parent::__construct($e->getMessage(), 0, $e); |
| 247 |
} |
| 248 |
|
| 249 |
public function getRequest(): RequestInterface |
| 250 |
{ |
| 251 |
return $this->request; |
| 252 |
} |
| 253 |
} |