0 follower

Final Class Yiisoft\ResponseDownload\ByteRangeStream

InheritanceYiisoft\ResponseDownload\ByteRangeStream
ImplementsPsr\Http\Message\StreamInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Http\Message\StreamInterface $stream, integer $start, integer $end )
$stream \Psr\Http\Message\StreamInterface
$start integer
$end integer

                public function __construct(
    private readonly StreamInterface $stream,
    private readonly int $start,
    int $end,
) {
    if ($start < 0 || $end < $start) {
        throw new RuntimeException('Invalid byte range.');
    }
    if (!$stream->isSeekable()) {
        throw new RuntimeException('Stream is not seekable.');
    }
    if (!$stream->isReadable()) {
        throw new RuntimeException('Stream is not readable.');
    }
    $this->size = $end - $start + 1;
    $this->stream->seek($start);
}

            
__toString() public method

public string __toString ( )

                public function __toString(): string
{
    try {
        $this->rewind();
        return $this->getContents();
    } catch (Throwable) {
        return '';
    }
}

            
close() public method

public void close ( )

                public function close(): void
{
    $this->stream->close();
}

            
detach() public method

public mixed detach ( )

                public function detach()
{
    return $this->stream->detach();
}

            
eof() public method

public boolean eof ( )

                public function eof(): bool
{
    return $this->position >= $this->size || $this->stream->eof();
}

            
getContents() public method

public string getContents ( )

                public function getContents(): string
{
    $contents = [];
    while (!$this->eof()) {
        $chunk = $this->read(8192);
        if ($chunk === '') {
            break;
        }
        $contents[] = $chunk;
    }
    return implode('', $contents);
}

            
getMetadata() public method

public mixed getMetadata ( ?string $key null )
$key ?string

                public function getMetadata(?string $key = null)
{
    return $this->stream->getMetadata($key);
}

            
getSize() public method

public ?int getSize ( )

                public function getSize(): ?int
{
    return $this->size;
}

            
isReadable() public method

public boolean isReadable ( )

                public function isReadable(): bool
{
    return true;
}

            
isSeekable() public method

public boolean isSeekable ( )

                public function isSeekable(): bool
{
    return true;
}

            
isWritable() public method

public boolean isWritable ( )

                public function isWritable(): bool
{
    return false;
}

            
read() public method

public string read ( integer $length )
$length integer

                public function read(int $length): string
{
    if ($length < 0) {
        throw new RuntimeException('Length parameter cannot be negative.');
    }
    if ($length === 0 || $this->eof()) {
        return '';
    }
    $remaining = $this->size - $this->position;
    $contents = $this->stream->read(min($length, $remaining));
    $this->position += strlen($contents);
    return $contents;
}

            
rewind() public method

public void rewind ( )

                public function rewind(): void
{
    $this->seek(0);
}

            
seek() public method

public void seek ( integer $offset, integer $whence SEEK_SET )
$offset integer
$whence integer

                public function seek(int $offset, int $whence = SEEK_SET): void
{
    $position = match ($whence) {
        SEEK_SET => $offset,
        SEEK_CUR => $this->position + $offset,
        SEEK_END => $this->size + $offset,
        default => throw new RuntimeException('Invalid seek mode.'),
    };
    if ($position < 0 || $position > $this->size) {
        throw new RuntimeException('Invalid seek offset.');
    }
    $this->stream->seek($this->start + $position);
    $this->position = $position;
}

            
tell() public method

public integer tell ( )

                public function tell(): int
{
    return $this->position;
}

            
write() public method

public integer write ( string $string )
$string string

                public function write(string $string): int
{
    throw new RuntimeException('Stream is not writable.');
}