| 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 GuzzleHttp\Promise\Promise as GuzzlePromise; |
| 15 |
use GuzzleHttp\Promise\RejectedPromise; |
| 16 |
use GuzzleHttp\Promise\Utils; |
| 17 |
use Http\Client\Exception\NetworkException; |
| 18 |
use Http\Client\Exception\RequestException; |
| 19 |
use Http\Client\HttpAsyncClient; |
| 20 |
use Http\Client\HttpClient as HttplugInterface; |
| 21 |
use Http\Discovery\Exception\NotFoundException; |
| 22 |
use Http\Discovery\Psr17FactoryDiscovery; |
| 23 |
use Http\Message\RequestFactory; |
| 24 |
use Http\Message\StreamFactory; |
| 25 |
use Http\Message\UriFactory; |
| 26 |
use Http\Promise\Promise; |
| 27 |
use Nyholm\Psr7\Factory\Psr17Factory; |
| 28 |
use Nyholm\Psr7\Request; |
| 29 |
use Nyholm\Psr7\Uri; |
| 30 |
use Psr\Http\Message\RequestFactoryInterface; |
| 31 |
use Psr\Http\Message\RequestInterface; |
| 32 |
use Psr\Http\Message\ResponseFactoryInterface; |
| 33 |
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface; |
| 34 |
use Psr\Http\Message\StreamFactoryInterface; |
| 35 |
use Psr\Http\Message\StreamInterface; |
| 36 |
use Psr\Http\Message\UriFactoryInterface; |
| 37 |
use Psr\Http\Message\UriInterface; |
| 38 |
use Symfony\Component\HttpClient\Internal\HttplugWaitLoop; |
| 39 |
use Symfony\Component\HttpClient\Response\HttplugPromise; |
| 40 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 41 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 42 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 43 |
use Symfony\Contracts\Service\ResetInterface; |
| 44 |
|
| 45 |
if (!interface_exists(HttplugInterface::class)) { |
| 46 |
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/httplug" package is not installed. Try running "composer require php-http/httplug".'); |
| 47 |
} |
| 48 |
|
| 49 |
if (!interface_exists(RequestFactory::class)) { |
| 50 |
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/message-factory" package is not installed. Try running "composer require php-http/message-factory".'); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* An adapter to turn a Symfony HttpClientInterface into an Httplug client. |
| 55 |
* |
| 56 |
* Run "composer require nyholm/psr7" to install an efficient implementation of response |
| 57 |
* and stream factories with flex-provided autowiring aliases. |
| 58 |
* |
| 59 |
* @author Nicolas Grekas <[email protected]> |
| 60 |
*/ |
| 61 |
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface |
| 62 |
{ |
| 63 |
private $client; |
| 64 |
private $responseFactory; |
| 65 |
private $streamFactory; |
| 66 |
|
| 67 |
/** |
| 68 |
* @var \SplObjectStorage<ResponseInterface, array{RequestInterface, Promise}>|null |
| 69 |
*/ |
| 70 |
private $promisePool; |
| 71 |
|
| 72 |
private $waitLoop; |
| 73 |
|
| 74 |
public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null) |
| 75 |
{ |
| 76 |
$this->client = $client ?? HttpClient::create(); |
| 77 |
$this->responseFactory = $responseFactory; |
| 78 |
$this->streamFactory = $streamFactory ?? ($responseFactory instanceof StreamFactoryInterface ? $responseFactory : null); |
| 79 |
$this->promisePool = class_exists(Utils::class) ? new \SplObjectStorage() : null; |
| 80 |
|
| 81 |
if (null === $this->responseFactory || null === $this->streamFactory) { |
| 82 |
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) { |
| 83 |
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".'); |
| 84 |
} |
| 85 |
|
| 86 |
try { |
| 87 |
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null; |
| 88 |
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory(); |
| 89 |
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory(); |
| 90 |
} catch (NotFoundException $e) { |
| 91 |
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); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$this->waitLoop = new HttplugWaitLoop($this->client, $this->promisePool, $this->responseFactory, $this->streamFactory); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* {@inheritdoc} |
| 100 |
*/ |
| 101 |
public function sendRequest(RequestInterface $request): Psr7ResponseInterface |
| 102 |
{ |
| 103 |
try { |
| 104 |
return $this->waitLoop->createPsr7Response($this->sendPsr7Request($request)); |
| 105 |
} catch (TransportExceptionInterface $e) { |
| 106 |
throw new NetworkException($e->getMessage(), $request, $e); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* {@inheritdoc} |
| 112 |
* |
| 113 |
* @return HttplugPromise |
| 114 |
*/ |
| 115 |
public function sendAsyncRequest(RequestInterface $request): Promise |
| 116 |
{ |
| 117 |
if (!$promisePool = $this->promisePool) { |
| 118 |
throw new \LogicException(sprintf('You cannot use "%s()" as the "guzzlehttp/promises" package is not installed. Try running "composer require guzzlehttp/promises".', __METHOD__)); |
| 119 |
} |
| 120 |
|
| 121 |
try { |
| 122 |
$response = $this->sendPsr7Request($request, true); |
| 123 |
} catch (NetworkException $e) { |
| 124 |
return new HttplugPromise(new RejectedPromise($e)); |
| 125 |
} |
| 126 |
|
| 127 |
$waitLoop = $this->waitLoop; |
| 128 |
|
| 129 |
$promise = new GuzzlePromise(static function () use ($response, $waitLoop) { |
| 130 |
$waitLoop->wait($response); |
| 131 |
}, static function () use ($response, $promisePool) { |
| 132 |
$response->cancel(); |
| 133 |
unset($promisePool[$response]); |
| 134 |
}); |
| 135 |
|
| 136 |
$promisePool[$response] = [$request, $promise]; |
| 137 |
|
| 138 |
return new HttplugPromise($promise); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Resolves pending promises that complete before the timeouts are reached. |
| 143 |
* |
| 144 |
* When $maxDuration is null and $idleTimeout is reached, promises are rejected. |
| 145 |
* |
| 146 |
* @return int The number of remaining pending promises |
| 147 |
*/ |
| 148 |
public function wait(float $maxDuration = null, float $idleTimeout = null): int |
| 149 |
{ |
| 150 |
return $this->waitLoop->wait(null, $maxDuration, $idleTimeout); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* {@inheritdoc} |
| 155 |
*/ |
| 156 |
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface |
| 157 |
{ |
| 158 |
if ($this->responseFactory instanceof RequestFactoryInterface) { |
| 159 |
$request = $this->responseFactory->createRequest($method, $uri); |
| 160 |
} elseif (class_exists(Request::class)) { |
| 161 |
$request = new Request($method, $uri); |
| 162 |
} elseif (class_exists(Psr17FactoryDiscovery::class)) { |
| 163 |
$request = Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri); |
| 164 |
} else { |
| 165 |
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
| 166 |
} |
| 167 |
|
| 168 |
$request = $request |
| 169 |
->withProtocolVersion($protocolVersion) |
| 170 |
->withBody($this->createStream($body)) |
| 171 |
; |
| 172 |
|
| 173 |
foreach ($headers as $name => $value) { |
| 174 |
$request = $request->withAddedHeader($name, $value); |
| 175 |
} |
| 176 |
|
| 177 |
return $request; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* {@inheritdoc} |
| 182 |
*/ |
| 183 |
public function createStream($body = null): StreamInterface |
| 184 |
{ |
| 185 |
if ($body instanceof StreamInterface) { |
| 186 |
return $body; |
| 187 |
} |
| 188 |
|
| 189 |
if (\is_string($body ?? '')) { |
| 190 |
$stream = $this->streamFactory->createStream($body ?? ''); |
| 191 |
} elseif (\is_resource($body)) { |
| 192 |
$stream = $this->streamFactory->createStreamFromResource($body); |
| 193 |
} else { |
| 194 |
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body))); |
| 195 |
} |
| 196 |
|
| 197 |
if ($stream->isSeekable()) { |
| 198 |
$stream->seek(0); |
| 199 |
} |
| 200 |
|
| 201 |
return $stream; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* {@inheritdoc} |
| 206 |
*/ |
| 207 |
public function createUri($uri): UriInterface |
| 208 |
{ |
| 209 |
if ($uri instanceof UriInterface) { |
| 210 |
return $uri; |
| 211 |
} |
| 212 |
|
| 213 |
if ($this->responseFactory instanceof UriFactoryInterface) { |
| 214 |
return $this->responseFactory->createUri($uri); |
| 215 |
} |
| 216 |
|
| 217 |
if (class_exists(Uri::class)) { |
| 218 |
return new Uri($uri); |
| 219 |
} |
| 220 |
|
| 221 |
if (class_exists(Psr17FactoryDiscovery::class)) { |
| 222 |
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri); |
| 223 |
} |
| 224 |
|
| 225 |
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
| 226 |
} |
| 227 |
|
| 228 |
public function __sleep(): array |
| 229 |
{ |
| 230 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
| 231 |
} |
| 232 |
|
| 233 |
public function __wakeup() |
| 234 |
{ |
| 235 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
| 236 |
} |
| 237 |
|
| 238 |
public function __destruct() |
| 239 |
{ |
| 240 |
$this->wait(); |
| 241 |
} |
| 242 |
|
| 243 |
public function reset() |
| 244 |
{ |
| 245 |
if ($this->client instanceof ResetInterface) { |
| 246 |
$this->client->reset(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
private function sendPsr7Request(RequestInterface $request, bool $buffer = null): ResponseInterface |
| 251 |
{ |
| 252 |
try { |
| 253 |
$body = $request->getBody(); |
| 254 |
|
| 255 |
if ($body->isSeekable()) { |
| 256 |
$body->seek(0); |
| 257 |
} |
| 258 |
|
| 259 |
$options = [ |
| 260 |
'headers' => $request->getHeaders(), |
| 261 |
'body' => $body->getContents(), |
| 262 |
'buffer' => $buffer, |
| 263 |
]; |
| 264 |
|
| 265 |
if ('1.0' === $request->getProtocolVersion()) { |
| 266 |
$options['http_version'] = '1.0'; |
| 267 |
} |
| 268 |
|
| 269 |
return $this->client->request($request->getMethod(), (string) $request->getUri(), $options); |
| 270 |
} catch (\InvalidArgumentException $e) { |
| 271 |
throw new RequestException($e->getMessage(), $request, $e); |
| 272 |
} catch (TransportExceptionInterface $e) { |
| 273 |
throw new NetworkException($e->getMessage(), $request, $e); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|