|
| 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 Symfony\Contracts\HttpClient\Exception\ExceptionInterface; |
| 15 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 16 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Monitors a set of responses and triggers callbacks as they complete. |
| 20 | + * |
| 21 | + * @author Nicolas Grekas <p@tchwork.com> |
| 22 | + */ |
| 23 | +final class ResponseSetMonitor |
| 24 | +{ |
| 25 | + private $client; |
| 26 | + private $responses; |
| 27 | + |
| 28 | + public function __construct(HttpClientInterface $client) |
| 29 | + { |
| 30 | + $this->client = $client; |
| 31 | + $this->responses = new \SplObjectStorage(); |
| 32 | + } |
| 33 | + |
| 34 | + public function add(ResponseInterface $response, callable $onHeaders = null, callable $onBody = null, callable $onError = null): void |
| 35 | + { |
| 36 | + $this->responses[$response] = [$onHeaders, $onBody, $onError]; |
| 37 | + $this->tick(); |
| 38 | + } |
| 39 | + |
| 40 | + public function tick(?float $timeout = 0.0, bool $errorOnTimeout = false): int |
| 41 | + { |
| 42 | + foreach ($this->client->stream($this->responses, $timeout) as $response => $chunk) { |
| 43 | + try { |
| 44 | + if ($chunk->isTimeout() && !$errorOnTimeout) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + if (!$chunk->isFirst() && !$chunk->isLast()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + [$onHeaders, $onBody] = $this->responses[$response]; |
| 53 | + |
| 54 | + if (null !== $onHeaders && $chunk->isFirst()) { |
| 55 | + $onHeaders($response); |
| 56 | + } |
| 57 | + |
| 58 | + if (null !== $onBody && $chunk->isLast()) { |
| 59 | + $onBody($response); |
| 60 | + } |
| 61 | + |
| 62 | + if (null === $onBody || $chunk->isLast()) { |
| 63 | + unset($this->responses[$response]); |
| 64 | + } |
| 65 | + } catch (ExceptionInterface $e) { |
| 66 | + [, , $onError] = $this->responses[$response]; |
| 67 | + unset($this->responses[$response]); |
| 68 | + |
| 69 | + if (null === $onError) { |
| 70 | + throw $e; |
| 71 | + } |
| 72 | + |
| 73 | + $onError($e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return \count($this->responses); |
| 78 | + } |
| 79 | + |
| 80 | + public function __destruct() |
| 81 | + { |
| 82 | + $this->tick(null, true); |
| 83 | + } |
| 84 | +} |
0 commit comments