|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Client\Exception\NetworkException; |
| 15 | +use Http\Client\Exception\RequestException; |
| 16 | +use Http\Client\HttpClient; |
| 17 | +use Http\Message\RequestFactory; |
| 18 | +use Http\Message\StreamFactory; |
| 19 | +use Http\Message\UriFactory; |
| 20 | +use Psr\Http\Client\ClientInterface; |
| 21 | +use Psr\Http\Client\NetworkExceptionInterface; |
| 22 | +use Psr\Http\Client\RequestExceptionInterface; |
| 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\UriInterface; |
| 29 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 30 | + |
| 31 | +if (!interface_exists(HttpClient::class)) { |
| 32 | + 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".'); |
| 33 | +} |
| 34 | + |
| 35 | +if (!interface_exists(ClientInterface::class)) { |
| 36 | + throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "psr/http-client" package is not installed. Try running "composer require psr/http-client".'); |
| 37 | +} |
| 38 | + |
| 39 | +if (!interface_exists(RequestFactory::class)) { |
| 40 | + throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/message-factory" package is not installed. Try running "composer require nyholm/psr7".'); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * An adapter to turn a Symfony HttpClientInterface into an Httplug client. |
| 45 | + * |
| 46 | + * Run "composer require psr/http-client" to install the base ClientInterface. Run |
| 47 | + * "composer require nyholm/psr7" to install an efficient implementation of response |
| 48 | + * and stream factories with flex-provided autowiring aliases. |
| 49 | + * |
| 50 | + * @author Nicolas Grekas <p@tchwork.com> |
| 51 | + */ |
| 52 | +final class HttplugClient implements HttpClient, RequestFactory, StreamFactory, UriFactory |
| 53 | +{ |
| 54 | + private $client; |
| 55 | + |
| 56 | + public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null) |
| 57 | + { |
| 58 | + $this->client = new Psr18Client($client, $responseFactory, $streamFactory); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + */ |
| 64 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 65 | + { |
| 66 | + try { |
| 67 | + return $this->client->sendRequest($request); |
| 68 | + } catch (RequestExceptionInterface $e) { |
| 69 | + throw new RequestException($e->getMessage(), $request, $e); |
| 70 | + } catch (NetworkExceptionInterface $e) { |
| 71 | + throw new NetworkException($e->getMessage(), $request, $e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface |
| 79 | + { |
| 80 | + $request = $this->client |
| 81 | + ->createRequest($method, $uri) |
| 82 | + ->withProtocolVersion($protocolVersion) |
| 83 | + ->withBody($this->createStream($body)) |
| 84 | + ; |
| 85 | + |
| 86 | + foreach ($headers as $name => $value) { |
| 87 | + $request = $request->withAddedHeader($name, $value); |
| 88 | + } |
| 89 | + |
| 90 | + return $request; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * {@inheritdoc} |
| 95 | + */ |
| 96 | + public function createStream($body = null): StreamInterface |
| 97 | + { |
| 98 | + if ($body instanceof StreamInterface) { |
| 99 | + return $body; |
| 100 | + } |
| 101 | + |
| 102 | + if (\is_string($body ?? '')) { |
| 103 | + return $this->client->createStream($body ?? ''); |
| 104 | + } |
| 105 | + |
| 106 | + if (\is_resource($body)) { |
| 107 | + return $this->client->createStreamFromResource($body); |
| 108 | + } |
| 109 | + |
| 110 | + throw new \InvalidArgumentException(sprintf('%s() expects string, resource or StreamInterface, %s given.', __METHOD__, \gettype($body))); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {@inheritdoc} |
| 115 | + */ |
| 116 | + public function createUri($uri = ''): UriInterface |
| 117 | + { |
| 118 | + return $uri instanceof UriInterface ? $uri : $this->client->createUri($uri); |
| 119 | + } |
| 120 | +} |
0 commit comments