|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Http Adapter package. |
| 5 | + * |
| 6 | + * (c) Eric GELOEN <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please read the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Http\Adapter; |
| 13 | + |
| 14 | +use GuzzleHttp\Client; |
| 15 | +use GuzzleHttp\ClientInterface; |
| 16 | +use GuzzleHttp\Event\CompleteEvent; |
| 17 | +use GuzzleHttp\Event\ErrorEvent; |
| 18 | +use GuzzleHttp\Message\RequestInterface; |
| 19 | +use GuzzleHttp\Pool; |
| 20 | +use Http\Adapter\Message\InternalRequestInterface; |
| 21 | +use Http\Adapter\Normalizer\BodyNormalizer; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author GeLo <[email protected]> |
| 25 | + */ |
| 26 | +class Guzzle5HttpAdapter extends CurlHttpAdapter |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var ClientInterface |
| 30 | + */ |
| 31 | + private $client; |
| 32 | + |
| 33 | + /** |
| 34 | + * |
| 35 | + * @param ClientInterface|null $client |
| 36 | + * @param ConfigurationInterface|null $configuration |
| 37 | + */ |
| 38 | + public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null) |
| 39 | + { |
| 40 | + parent::__construct($configuration); |
| 41 | + |
| 42 | + $this->client = $client ?: new Client(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function getName() |
| 49 | + { |
| 50 | + return 'guzzle5'; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
| 57 | + { |
| 58 | + try { |
| 59 | + $response = $this->client->send($this->createRequest($internalRequest)); |
| 60 | + } catch (\Exception $e) { |
| 61 | + var_dump($e); exit; |
| 62 | + throw HttpAdapterException::cannotFetchUri( |
| 63 | + $e->getRequest()->getUrl(), |
| 64 | + $this->getName(), |
| 65 | + $e->getMessage() |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->getConfiguration()->getMessageFactory()->createResponse( |
| 70 | + (integer) $response->getStatusCode(), |
| 71 | + $response->getProtocolVersion(), |
| 72 | + $response->getHeaders(), |
| 73 | + BodyNormalizer::normalize( |
| 74 | + function () use ($response) { |
| 75 | + return $response->getBody()->detach(); |
| 76 | + }, |
| 77 | + $internalRequest->getMethod() |
| 78 | + ) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritdoc} |
| 84 | + */ |
| 85 | + protected function sendInternalRequests(array $internalRequests, $success, $error) |
| 86 | + { |
| 87 | + $requests = []; |
| 88 | + foreach ($internalRequests as $internalRequest) { |
| 89 | + $requests[] = $this->createRequest($internalRequest, $success, $error); |
| 90 | + } |
| 91 | + |
| 92 | + Pool::batch($this->client, $requests); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + protected function createFile($file) |
| 99 | + { |
| 100 | + return fopen($file, 'r'); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates a request. |
| 105 | + * |
| 106 | + * @param InternalRequestInterface $internalRequest |
| 107 | + * @param callable|null $success |
| 108 | + * @param callable|null $error |
| 109 | + * |
| 110 | + * @return RequestInterface |
| 111 | + */ |
| 112 | + private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null) |
| 113 | + { |
| 114 | + $request = $this->client->createRequest( |
| 115 | + $internalRequest->getMethod(), |
| 116 | + (string) $internalRequest->getUri(), |
| 117 | + [ |
| 118 | + 'exceptions' => false, |
| 119 | + 'allow_redirects' => false, |
| 120 | + 'timeout' => $this->getConfiguration()->getTimeout(), |
| 121 | + 'connect_timeout' => $this->getConfiguration()->getTimeout(), |
| 122 | + 'version' => $internalRequest->getProtocolVersion(), |
| 123 | + 'headers' => $this->prepareHeaders($internalRequest), |
| 124 | + 'body' => $this->prepareContent($internalRequest), |
| 125 | + ] |
| 126 | + ); |
| 127 | + |
| 128 | + if (isset($success)) { |
| 129 | + $messageFactory = $this->getConfiguration()->getMessageFactory(); |
| 130 | + |
| 131 | + $request->getEmitter()->on( |
| 132 | + 'complete', |
| 133 | + function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) { |
| 134 | + $response = $messageFactory->createResponse( |
| 135 | + (integer) $event->getResponse()->getStatusCode(), |
| 136 | + $event->getResponse()->getProtocolVersion(), |
| 137 | + $event->getResponse()->getHeaders(), |
| 138 | + BodyNormalizer::normalize( |
| 139 | + function () use ($event) { |
| 140 | + return $event->getResponse()->getBody()->detach(); |
| 141 | + }, |
| 142 | + $internalRequest->getMethod() |
| 143 | + ) |
| 144 | + ); |
| 145 | + |
| 146 | + $response = $response->withParameter('request', $internalRequest); |
| 147 | + call_user_func($success, $response); |
| 148 | + } |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + if (isset($error)) { |
| 153 | + $httpAdapterName = $this->getName(); |
| 154 | + |
| 155 | + $request->getEmitter()->on( |
| 156 | + 'error', |
| 157 | + function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) { |
| 158 | + $exception = HttpAdapterException::cannotFetchUri( |
| 159 | + $event->getException()->getRequest()->getUrl(), |
| 160 | + $httpAdapterName, |
| 161 | + $event->getException()->getMessage() |
| 162 | + ); |
| 163 | + $exception->setRequest($internalRequest); |
| 164 | + call_user_func($error, $exception); |
| 165 | + } |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + return $request; |
| 170 | + } |
| 171 | +} |
0 commit comments