File: FileIterator53.php Lines 7 to 48 |
|
7 | * php-5.5以降であればGeneratorを使った方がパフォーマンスがよい。 |
|
8 | * 5.5.11以降であればSplFileObjectもバイナリファイルに対応されている。 |
|
9 | * |
|
10 | * @uses >= php-5.3 |
|
11 | * @version 0.1.0 |
|
12 | */ |
|
13 | abstract class FileIterator53 implements \Iterator |
|
14 | { |
|
15 | /** @var resource */ |
| | protected $fp; |
|
17 | /** @var mixed scalar */ |
|
18 | protected $_key; |
|
19 | /** @var mixed */ |
|
20 | protected $buf; |
|
21 | |
|
22 | /** |
|
23 | * @param resource $fp |
|
24 | */ |
| | public function __construct($fp) { |
|
26 | if(!is_resource($fp)) { |
|
27 | throw new \InvalidArgumentException(); |
|
28 | } |
|
29 | $this->fp = $fp; |
|
30 | } |
|
31 | |
|
32 | /** |
|
33 | * @param string $fullpath |
|
34 | * @param string $mode |
|
35 | * @return static |
|
36 | */ |
|
37 | public static function createByPath($fullpath, $mode='rb') { |
| | $fp = @fopen($fullpath, $mode); |
|
39 | if(!is_resource($fp)) { |
|
40 | throw new \RuntimeException("cannot open stream: $fullpath"); |
|
41 | } |
|
42 | return new static($fp); |
|
43 | } |
|
44 | |
|
45 | public function __destruct() { |
|
46 | @fclose($this->fp); |
|
47 | } |
|
48 | |
|