-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathFileChunks.php
43 lines (37 loc) · 863 Bytes
/
FileChunks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace JsonMachine;
/**
* @implements \IteratorAggregate<int, string>
*/
class FileChunks implements \IteratorAggregate
{
/** @var string */
private $fileName;
/** @var int */
private $chunkSize;
/**
* @param string $fileName
* @param int $chunkSize
*/
public function __construct($fileName, $chunkSize = 1024 * 8)
{
$this->fileName = $fileName;
$this->chunkSize = $chunkSize;
}
/**
* @return \Generator
*
* @throws Exception\InvalidArgumentException
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
$fileHandle = fopen($this->fileName, 'r');
try {
yield from new StreamChunks($fileHandle, $this->chunkSize);
} finally {
fclose($fileHandle);
}
}
}