Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Stream;
4
 
5
use React\Promise\Deferred;
6
use React\Promise\PromisorInterface;
7
 
8
class BufferedSink extends WritableStream implements PromisorInterface
9
{
10
    private $buffer = '';
11
    private $deferred;
12
 
13
    public function __construct()
14
    {
15
        $this->deferred = new Deferred();
16
 
17
        $this->on('pipe', array($this, 'handlePipeEvent'));
18
        $this->on('error', array($this, 'handleErrorEvent'));
19
    }
20
 
21
    public function handlePipeEvent($source)
22
    {
23
        Util::forwardEvents($source, $this, array('error'));
24
    }
25
 
26
    public function handleErrorEvent($e)
27
    {
28
        $this->deferred->reject($e);
29
    }
30
 
31
    public function write($data)
32
    {
33
        $this->buffer .= $data;
34
        $this->deferred->progress($data);
35
    }
36
 
37
    public function close()
38
    {
39
        if ($this->closed) {
40
            return;
41
        }
42
 
43
        parent::close();
44
        $this->deferred->resolve($this->buffer);
45
    }
46
 
47
    public function promise()
48
    {
49
        return $this->deferred->promise();
50
    }
51
 
52
    public static function createPromise(ReadableStreamInterface $stream)
53
    {
54
        $sink = new static();
55
        $stream->pipe($sink);
56
 
57
        return $sink->promise();
58
    }
59
}