octane: Response doesn't support stream-based output

  • Octane Version: 0.1.0
  • Laravel Version: 8.36.1
  • PHP Version: 8.0.3
  • Database Driver & Version:

Description:

Most of PHP’s output usages like echo, var_dump output its content to buffer. In Swoole’s case the result will print on console instead of web.

It seems this partial code in SwooleClient.php refers from here https://github.com/laravel/octane/blob/08537a7ed6d2c463f76278081ac7af9a6dd1dead/src/Swoole/SwooleClient.php#L153-L178 because the output property doesn’t exist in symfony’s response object. The output property is set from here in swooletw/laravel-swoole.

Symfony\Component\HttpFoundation\StreamedResponse doesn’t support getContent and setContent for setting output content, so I fetch buffer output and set the result to response’s output property manually.

If Octane is going to support stream output, it needs to use ob_ functions to catch stream content as well.

Many thanks to the official team for integrating Swoole into Laravel!

Steps To Reproduce:

echo 'hello world';

var_dump('Swoole is awesome');

response()->streamDownload(function () {
    echo file_get_contents("https://github.com/laravel/laravel/blob/8.x/README.md");
    }, 'laravel-readme.md');
});

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Had to look into it - https://github.com/php/php-src/commit/888f376e4cc2c87b0e0c4214e3a5af52f4160b28 Another one of those 21 year old features I’ve never noticed ; )

Thanks @barryvdh !

It would be ideal if there were a way to access the current Swoole\Http\Response, so that I could call write() on it.

My use case is streaming large JSON objects, which currently is done using a JSON generator calling echo within the StreamResponse::sendContent callback. These objects are too large to hold in memory, so a way of doing this that doesn’t involve output buffering would be great!

I’d be happy to assist with a PR, but I wasn’t sure where architecturally in Octane this would go.

Do any of:

  • via the Octane facade
  • the dependency injection container
  • by extending StreamResponse
  • via a new interface on an application class that extended StreamResponse

make sense?