Passed
Push — master ( 184b95...3a4432 )
by 世昌
01:52
created

ReadLineIterator::getIterator()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 10
nop 0
dl 0
loc 23
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace nebula\route\collection;
3
4
use Iterator;
5
use IteratorAggregate;
6
7
/**
8
 * 逐行读文件迭代器
9
 *
10
 */
11
class ReadLineIterator implements IteratorAggregate
12
{
13
    protected $path;
14
    protected $bufferSize;
15
    protected $offset;
16
17
    /**
18
     * 构建迭代器
19
     *
20
     * @param string $path 路径
21
     * @param integer|null $offset 起始偏移
22
     * @param integer $bufferSize
23
     */
24
    public function __construct(string $path, ?int $offset = 0, int $bufferSize = 1024)
25
    {
26
        $this->path = $path;
27
        $this->offset = $offset;
28
        $this->bufferSize = $bufferSize;
29
    }
30
31
    /**
32
     * 获取迭代器
33
     *
34
     * @return Iterator
35
     */
36
    public function getIterator():Iterator
37
    {
38
        if (file_exists($this->path) && ($fp = fopen($this->path, 'rb')) !== false) {
39
            $offset = 0;
40
            while (!feof($fp)) {
41
                fseek($fp, $offset);
42
                $content = fread($fp, $this->bufferSize);
43
                $content.=(feof($fp))? "\n":'';
44
                $size = strpos($content, "\n");
45
                $offset += $size;
46
                if ($content[$size - 1] === "\r") {
47
                    $content = substr($content, 0, $size -1);
48
                } else {
49
                    $content = substr($content, 0, $size);
50
                }
51
                if (is_null($this->offset)) {
52
                    yield $content;
53
                } else {
54
                    yield $offset => $content;
55
                }
56
            }
57
        } else {
58
            throw new \Exception(sprintf('readline iterator: file note exists %s', $this->path));
59
        }
60
    }
61
}
62