File: LineGenerator.php Lines 3 to 22 |
|
3 | |
|
4 | class LineGenerator |
|
5 | { |
|
6 | /** |
|
7 | * ファイルを1行ずつ返すgenerator |
|
8 | * @param $fullpath |
|
9 | * @return \Generator |
|
10 | */ |
|
11 | public static function eachLine($fullpath) { |
| | $fp = @fopen($fullpath, 'rb'); |
|
13 | if($fp === false) { |
|
14 | throw new \RuntimeException("cannot open stream: $fullpath"); |
|
15 | } |
|
16 | try { |
|
17 | while (false !== $line = fgets($fp)) { |
|
18 | yield $line; |
|
19 | } |
|
20 | } finally { |
|
21 | @fclose($fp); |
|
22 | } |
|