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

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($this->createPsr7Response($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 function createPsr7Response(ResponseInterface $responsebool $buffer false): Psr7ResponseInterface
  98.     {
  99.         $psrResponse $this->responseFactory->createResponse($response->getStatusCode());
  100.         foreach ($response->getHeaders(false) as $name => $values) {
  101.             foreach ($values as $value) {
  102.                 $psrResponse $psrResponse->withAddedHeader($name$value);
  103.             }
  104.         }
  105.         if ($response instanceof StreamableInterface) {
  106.             $body $this->streamFactory->createStreamFromResource($response->toStream(false));
  107.         } elseif (!$buffer) {
  108.             $body $this->streamFactory->createStreamFromResource(StreamWrapper::createResource($response$this->client));
  109.         } else {
  110.             $body $this->streamFactory->createStream($response->getContent(false));
  111.         }
  112.         if ($body->isSeekable()) {
  113.             $body->seek(0);
  114.         }
  115.         return $psrResponse->withBody($body);
  116.     }
  117. }