Passed
Push — master ( 7c8a03...35bd44 )
by 世昌
02:27
created

DataStream::getStreamName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\framework\http\stream;
3
4
use SplFileObject;
5
use suda\framework\http\Stream;
6
7
/**
8
 * 数据流
9
 */
10
class DataStream implements Stream
11
{
12
    /**
13
     * 设置偏移
14
     *
15
     * @var int
16
     */
17
    protected $offset = 0;
18
19
    /**
20
     * 设置长度
21
     *
22
     * @var int|null
23
     */
24
    protected $length = null;
25
26
    /**
27
     * 读取快大小
28
     *
29
     * @var integer
30
     */
31
    protected $blockSize = 8192;
32
33
    /**
34
     * 流描述
35
     *
36
     * @var SplFileObject|string
37
     */
38
    protected $stream;
39
40
    /**
41
     * 输入文件流
42
     *
43
     * @param SplFileObject|string $stream
44
     * @param integer $offset
45
     * @param integer|null $length
46
     * @param integer $blockSize
47
     */
48
    public function __construct($stream, int $offset = 0, ?int $length = null, int $blockSize = 8192)
49
    {
50
        $this->stream = $stream;
51
        $this->offset = $offset;
52
        $this->length = $length;
53
        $this->blockSize = $blockSize;
54
    }
55
56
    /**
57
     * 获取全部内容
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        \ob_start();
64
        $this->echo();
65
        return \ob_get_clean();
66
    }
67
68
    /**
69
     * 输出
70
     *
71
     * @return void
72
     */
73
    public function echo()
74
    {
75
        $remain = $this->length();
76
        $stream = $this->openStream();
77
        if ($stream->fseek($this->offset) === 0) {
78
            // 持续链接则继续发送内容
79
            while ($stream->eof() === false && $remain > 0) {
80
                $readLength = $remain >= $this->blockSize ? $this->blockSize : $remain;
81
                $remain -= $readLength;
82
                $data = $stream->fread($readLength);
83
                echo $data;
84
            }
85
        }
86
    }
87
88
    /**
89
     * 获取流长度
90
     *
91
     * @return integer
92
     */
93
    public function length():int
94
    {
95
        if ($this->length === null) {
96
            $stream = $this->openStream();
97
            $stream->fseek(0, SEEK_END);
98
            $this->length = $stream->ftell();
99
        }
100
        return $this->length;
101
    }
102
103
    /**
104
     * 获取流名称
105
     *
106
     * @return string
107
     */
108
    public function getStreamName() {
109
        if (is_string($this->stream)) {
110
            return $this->stream;
111
        } else {
112
            return $this->stream->getPathname();
113
        }
114
    }
115
116
    protected function openStream(): SplFileObject
117
    {
118
        if (is_string($this->stream)) {
119
            return new SplFileObject($this->stream);
120
        } else {
121
            return $this->stream;
122
        }
123
    }
124
}
125