vendor/symfony/http-client/Internal/HttplugWaitLoop.php line 96

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpClient\Internal;
  11. use Http\Client\Exception\NetworkException;
  12. use Http\Promise\Promise;
  13. use Psr\Http\Message\RequestInterface as Psr7RequestInterface;
  14. use Psr\Http\Message\ResponseFactoryInterface;
  15. use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
  16. use Psr\Http\Message\StreamFactoryInterface;
  17. use Symfony\Component\HttpClient\Response\StreamableInterface;
  18. use Symfony\Component\HttpClient\Response\StreamWrapper;
  19. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  20. use Symfony\Contracts\HttpClient\HttpClientInterface;
  21. use Symfony\Contracts\HttpClient\ResponseInterface;
  22. /**
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  *
  25.  * @internal
  26.  */
  27. final class HttplugWaitLoop
  28. {
  29.     private $client;
  30.     private $promisePool;
  31.     private $responseFactory;
  32.     private $streamFactory;
  33.     /**
  34.      * @param \SplObjectStorage<ResponseInterface, array{Psr7RequestInterface, Promise}>|null $promisePool
  35.      */
  36.     public function __construct(HttpClientInterface $client, ?\SplObjectStorage $promisePoolResponseFactoryInterface $responseFactoryStreamFactoryInterface $streamFactory)
  37.     {
  38.         $this->client $client;
  39.         $this->promisePool $promisePool;
  40.         $this->responseFactory $responseFactory;
  41.         $this->streamFactory $streamFactory;
  42.     }
  43.     public function wait(?ResponseInterface $pendingResponsefloat $maxDuration nullfloat $idleTimeout null): int
  44.     {
  45.         if (!$this->promisePool) {
  46.             return 0;
  47.         }
  48.         $guzzleQueue \GuzzleHttp\Promise\Utils::queue();
  49.         if (0.0 === $remainingDuration $maxDuration) {
  50.             $idleTimeout 0.0;
  51.         } elseif (null !== $maxDuration) {
  52.             $startTime microtime(true);
  53.             $idleTimeout max(0.0min($maxDuration 5$idleTimeout ?? $maxDuration));
  54.         }
  55.         do {
  56.             foreach ($this->client->stream($this->promisePool$idleTimeout) as $response => $chunk) {
  57.                 try {
  58.                     if (null !== $maxDuration && $chunk->isTimeout()) {
  59.                         goto check_duration;
  60.                     }
  61.                     if ($chunk->isFirst()) {
  62.                         // Deactivate throwing on 3/4/5xx
  63.                         $response->getStatusCode();
  64.                     }
  65.                     if (!$chunk->isLast()) {
  66.                         goto check_duration;
  67.                     }
  68.                     if ([, $promise] = $this->promisePool[$response] ?? null) {
  69.                         unset($this->promisePool[$response]);
  70.                         $promise->resolve(self::createPsr7Response($this->responseFactory$this->streamFactory$this->client$responsetrue));
  71.                     }
  72.                 } catch (\Exception $e) {
  73.                     if ([$request$promise] = $this->promisePool[$response] ?? null) {
  74.                         unset($this->promisePool[$response]);
  75.                         if ($e instanceof TransportExceptionInterface) {
  76.                             $e = new NetworkException($e->getMessage(), $request$e);
  77.                         }
  78.                         $promise->reject($e);
  79.                     }
  80.                 }
  81.                 $guzzleQueue->run();
  82.                 if ($pendingResponse === $response) {
  83.                     return $this->promisePool->count();
  84.                 }
  85.                 check_duration:
  86.                 if (null !== $maxDuration && $idleTimeout && $idleTimeout $remainingDuration max(0.0$maxDuration microtime(true) + $startTime)) {
  87.                     $idleTimeout $remainingDuration 5;
  88.                     break;
  89.                 }
  90.             }
  91.             if (!$count $this->promisePool->count()) {
  92.                 return 0;
  93.             }
  94.         } while (null === $maxDuration || $remainingDuration);
  95.         return $count;
  96.     }
  97.     public static function createPsr7Response(ResponseFactoryInterface $responseFactoryStreamFactoryInterface $streamFactoryHttpClientInterface $clientResponseInterface $responsebool $buffer): Psr7ResponseInterface
  98.     {
  99.         $responseParameters = [$response->getStatusCode()];
  100.         foreach ($response->getInfo('response_headers') as $h) {
  101.             if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? (?:\d\d\d) (.+)#'$h$m)) {
  102.                 $responseParameters[1] = $m[1];
  103.             }
  104.         }
  105.         $psrResponse $responseFactory->createResponse(...$responseParameters);
  106.         foreach ($response->getHeaders(false) as $name => $values) {
  107.             foreach ($values as $value) {
  108.                 try {
  109.                     $psrResponse $psrResponse->withAddedHeader($name$value);
  110.                 } catch (\InvalidArgumentException $e) {
  111.                     // ignore invalid header
  112.                 }
  113.             }
  114.         }
  115.         if ($response instanceof StreamableInterface) {
  116.             $body $streamFactory->createStreamFromResource($response->toStream(false));
  117.         } elseif (!$buffer) {
  118.             $body $streamFactory->createStreamFromResource(StreamWrapper::createResource($response$client));
  119.         } else {
  120.             $body $streamFactory->createStream($response->getContent(false));
  121.         }
  122.         if ($body->isSeekable()) {
  123.             $body->seek(0);
  124.         }
  125.         return $psrResponse->withBody($body);
  126.     }
  127. }