|
| 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 Psr\Http\Client\ClientInterface; |
| 15 | +use Psr\Http\Client\NetworkExceptionInterface; |
| 16 | +use Psr\Http\Client\RequestExceptionInterface; |
| 17 | +use Psr\Http\Message\RequestInterface; |
| 18 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 19 | +use Psr\Http\Message\ResponseInterface; |
| 20 | +use Psr\Http\Message\StreamFactoryInterface; |
| 21 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * An adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface. |
| 26 | + * |
| 27 | + * Run "composer require psr/http-client" to install the base ClientInterface. Run |
| 28 | + * "composer require nyholm/psr7" to install an efficient implementation of response |
| 29 | + * and stream factories with flex-provided autowiring aliases. |
| 30 | + * |
| 31 | + * @author Nicolas Grekas <p@tchwork.com> |
| 32 | + * |
| 33 | + * @experimental in 4.3 |
| 34 | + */ |
| 35 | +final class Psr18Client implements ClientInterface |
| 36 | +{ |
| 37 | + private $client; |
| 38 | + private $responseFactory; |
| 39 | + private $streamFactory; |
| 40 | + |
| 41 | + public function __construct(HttpClientInterface $client, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) |
| 42 | + { |
| 43 | + $this->client = $client; |
| 44 | + $this->responseFactory = $responseFactory; |
| 45 | + $this->streamFactory = $streamFactory; |
| 46 | + } |
| 47 | + |
| 48 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 49 | + { |
| 50 | + try { |
| 51 | + $response = $this->client->request($request->getMethod(), (string) $request->getUri(), [ |
| 52 | + 'headers' => $request->getHeaders(), |
| 53 | + 'body' => (string) $request->getBody(), |
| 54 | + 'http_version' => '1.0' === $request->getProtocolVersion() ? '1.0' : null, |
| 55 | + ]); |
| 56 | + |
| 57 | + $psrResponse = $this->responseFactory->createResponse($response->getStatusCode()); |
| 58 | + |
| 59 | + foreach ($response->getHeaders() as $name => $values) { |
| 60 | + foreach ($values as $value) { |
| 61 | + $psrResponse = $psrResponse->withAddedHeader($name, $value); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $psrResponse->withBody($this->streamFactory->createStream($response->getContent())); |
| 66 | + } catch (TransportExceptionInterface $e) { |
| 67 | + if ($e instanceof \InvalidArgumentException) { |
| 68 | + throw new Psr18RequestException($e, $request); |
| 69 | + } |
| 70 | + |
| 71 | + throw new Psr18NetworkException($e, $request); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * @internal |
| 78 | + */ |
| 79 | +trait Psr18ExceptionTrait |
| 80 | +{ |
| 81 | + private $request; |
| 82 | + |
| 83 | + public function __construct(TransportExceptionInterface $e, RequestInterface $request) |
| 84 | + { |
| 85 | + parent::__construct($e->getMessage(), 0, $e); |
| 86 | + $this->request = $request; |
| 87 | + } |
| 88 | + |
| 89 | + public function getRequest(): RequestInterface |
| 90 | + { |
| 91 | + return $this->request; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * @internal |
| 97 | + */ |
| 98 | +class Psr18NetworkException extends \RuntimeException implements NetworkExceptionInterface |
| 99 | +{ |
| 100 | + use Psr18ExceptionTrait; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @internal |
| 105 | + */ |
| 106 | +class Psr18RequestException extends \InvalidArgumentException implements RequestExceptionInterface |
| 107 | +{ |
| 108 | + use Psr18ExceptionTrait; |
| 109 | +} |
0 commit comments