| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Internal; |
| 13 |
|
| 14 |
use Amp\ByteStream\ReadableBuffer; |
| 15 |
use Amp\ByteStream\ReadableIterableStream; |
| 16 |
use Amp\ByteStream\ReadableResourceStream; |
| 17 |
use Amp\ByteStream\ReadableStream; |
| 18 |
use Amp\Cancellation; |
| 19 |
use Amp\Http\Client\HttpContent; |
| 20 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class AmpBodyV5 implements HttpContent, ReadableStream, \IteratorAggregate |
| 28 |
{ |
| 29 |
private ReadableStream $body; |
| 30 |
private ?string $content; |
| 31 |
private array $info; |
| 32 |
private ?int $offset = 0; |
| 33 |
private int $length = -1; |
| 34 |
private ?int $uploaded = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
$body, |
| 41 |
&$info, |
| 42 |
private \Closure $onProgress, |
| 43 |
) { |
| 44 |
$this->info = &$info; |
| 45 |
|
| 46 |
if (\is_resource($body)) { |
| 47 |
$this->offset = ftell($body); |
| 48 |
$this->length = fstat($body)['size']; |
| 49 |
$this->body = new ReadableResourceStream($body); |
| 50 |
} elseif (\is_string($body)) { |
| 51 |
$this->length = \strlen($body); |
| 52 |
$this->body = new ReadableBuffer($body); |
| 53 |
$this->content = $body; |
| 54 |
} else { |
| 55 |
$this->body = new ReadableIterableStream((static function () use ($body) { |
| 56 |
while ('' !== $data = ($body)(16372)) { |
| 57 |
if (!\is_string($data)) { |
| 58 |
throw new TransportException(\sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data))); |
| 59 |
} |
| 60 |
|
| 61 |
yield $data; |
| 62 |
} |
| 63 |
})()); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function getContent(): ReadableStream |
| 68 |
{ |
| 69 |
if (null !== $this->uploaded) { |
| 70 |
$this->uploaded = null; |
| 71 |
|
| 72 |
if (\is_string($this->body)) { |
| 73 |
$this->offset = 0; |
| 74 |
} elseif ($this->body instanceof ReadableResourceStream) { |
| 75 |
fseek($this->body->getResource(), $this->offset); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
public function getContentType(): ?string |
| 83 |
{ |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
public function getContentLength(): ?int |
| 88 |
{ |
| 89 |
return 0 <= $this->length ? $this->length - $this->offset : null; |
| 90 |
} |
| 91 |
|
| 92 |
public function read(?Cancellation $cancellation = null): ?string |
| 93 |
{ |
| 94 |
$this->info['size_upload'] += $this->uploaded; |
| 95 |
$this->uploaded = 0; |
| 96 |
($this->onProgress)(); |
| 97 |
|
| 98 |
if (null !== $data = $this->body->read($cancellation)) { |
| 99 |
$this->uploaded = \strlen($data); |
| 100 |
} else { |
| 101 |
$this->info['upload_content_length'] = $this->info['size_upload']; |
| 102 |
} |
| 103 |
|
| 104 |
return $data; |
| 105 |
} |
| 106 |
|
| 107 |
public function isReadable(): bool |
| 108 |
{ |
| 109 |
return $this->body->isReadable(); |
| 110 |
} |
| 111 |
|
| 112 |
public function close(): void |
| 113 |
{ |
| 114 |
$this->body->close(); |
| 115 |
} |
| 116 |
|
| 117 |
public function isClosed(): bool |
| 118 |
{ |
| 119 |
return $this->body->isClosed(); |
| 120 |
} |
| 121 |
|
| 122 |
public function onClose(\Closure $onClose): void |
| 123 |
{ |
| 124 |
$this->body->onClose($onClose); |
| 125 |
} |
| 126 |
|
| 127 |
public function getIterator(): \Traversable |
| 128 |
{ |
| 129 |
return $this->body; |
| 130 |
} |
| 131 |
|
| 132 |
public static function rewind(HttpContent $body): HttpContent |
| 133 |
{ |
| 134 |
if (!$body instanceof self) { |
| 135 |
return $body; |
| 136 |
} |
| 137 |
|
| 138 |
$body->uploaded = null; |
| 139 |
|
| 140 |
if ($body->body instanceof ReadableResourceStream && !$body->body->isClosed()) { |
| 141 |
fseek($body->body->getResource(), $body->offset); |
| 142 |
} |
| 143 |
|
| 144 |
if ($body->body instanceof ReadableBuffer) { |
| 145 |
return new $body($body->content, $body->info, $body->onProgress); |
| 146 |
} |
| 147 |
|
| 148 |
return $body; |
| 149 |
} |
| 150 |
} |