Fix #18394: Add support for setting yii\web\Response::$stream to a callable

This commit is contained in:
Brandon Kelly
2020-11-23 11:56:59 -08:00
committed by GitHub
parent f72718ebbe
commit 279f59a3d4
2 changed files with 16 additions and 4 deletions

View File

@ -138,9 +138,12 @@ class Response extends \yii\base\Response
*/
public $content;
/**
* @var resource|array the stream to be sent. This can be a stream handle or an array of stream handle,
* the begin position and the end position. Note that when this property is set, the [[data]] and [[content]]
* properties will be ignored by [[send()]].
* @var resource|array|callable the stream to be sent. This can be a stream handle or an array of stream handle,
* the begin position and the end position. Alternatively it can be set to a callable, which returns
* (or [yields](https://www.php.net/manual/en/language.generators.syntax.php)) an array of strings that should
* be echoed and flushed out one by one.
*
* Note that when this property is set, the [[data]] and [[content]] properties will be ignored by [[send()]].
*/
public $stream;
/**
@ -441,6 +444,15 @@ class Response extends \yii\base\Response
Yii::warning('set_time_limit() is not available', __METHOD__);
}
if (is_callable($this->stream)) {
$data = call_user_func($this->stream);
foreach ($data as $datum) {
echo $datum;
flush();
}
return;
}
$chunkSize = 8 * 1024 * 1024; // 8MB per chunk
if (is_array($this->stream)) {