bear/streamer

BEAR.Sunday HTTP stream responder

1.2.2 2021-03-07 00:25 UTC

This package is auto-updated.

Last update: 2024-04-11 02:09:23 UTC


README

A HTTP stream responder

Scrutinizer Code Quality codecov Type Coverage Continuous Integration

Assign stream resource to resource-body.

class Image extends ResourceObject
{
    use StreamTransferInject;

    public function onGet(string $name = 'inline image') : ResourceObject
    {
        $fp = fopen(__DIR__ . '/BEAR.jpg', 'r');
        stream_filter_append($fp, 'convert.base64-encode'); // image base64 format
        $this->body = [
            'name' => $name,
            'image' => $fp
        ];

        return $this;
    }
}

Or assign entire body.

class Download extends ResourceObject
{
    use StreamTransferInject;

    public $headers = [
        'Content-Type' => 'image/jpeg',
        'Content-Disposition' => 'attachment; filename="image.jpg"'
    ];

    public function onGet() : ResourceObject
    {
        $fp = fopen(__DIR__ . '/BEAR.jpg', 'r');
        $this->body = $fp;

        return $this;
    }
}

Http body will not be output at once with "echo", Instead streamed with low memory consumption.