mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Fix #18394: Add support for setting yii\web\Response::$stream to a callable
				
					
				
			This commit is contained in:
		@ -4,7 +4,7 @@ Yii Framework 2 Change Log
 | 
			
		||||
2.0.40 under development
 | 
			
		||||
------------------------
 | 
			
		||||
 | 
			
		||||
- no changes in this release.
 | 
			
		||||
- Enh #18394: Add support for setting `yii\web\Response::$stream` to a callable (brandonkelly)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
2.0.39.3 November 23, 2020
 | 
			
		||||
 | 
			
		||||
@ -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)) {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user