Nyholm / psr7

A super lightweight PSR-7 implementation
MIT License
1.16k stars 75 forks source link

feature request for a non-buffered stream (NonBufferedStream) #201

Closed manueldimmler closed 1 year ago

manueldimmler commented 2 years ago

At the moment, it's not possible to use server-sent events with this implementation.

When calling StreamInterface::write, the content should be echoed and flushed immediately. In addition, the header would have to be emitted immediately in the Response class if the NonBufferedStream is used as the body.

Possible implementation of StreamInterface::write in a new NonBufferedStream class

    public function write($string): int
    {
        $buffered = '';
        while (0 < \ob_get_level()) {
            $buffered = \ob_get_clean() . $buffered;
        }

        echo $buffered . $string;

        \flush();

        return \strlen($string) + \strlen($buffered);
    }

Additional lines needed in MessageTrait::withHeader

        if ($this->stream instanceof NonBufferedStream) {
            header($normalized . ': ' . \implode(', ', $value));
        }
Nyholm commented 2 years ago

Hm.. I dont really understand. Why would you hold the full response in memory? How would that help you with the article you linked?

Also, what is the issue using the normal Stream?

zonuexe commented 2 years ago

@manueldimmler What about Laminas' SapiStreamEmitter.php?

nicolas-grekas commented 1 year ago

Closing as stalled.