Skip to content

Commit d0749d6

Browse files
committed
onProgress callback
1 parent 21bd122 commit d0749d6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/LajosBencz/StreamParseSql/StreamParseSql.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class StreamParseSql
1212

1313
const DEFAULT_CHUNK_SIZE = 4096;
1414

15+
/** @var callable[] */
16+
private $_onProgress = [];
17+
1518
/** @var string */
1619
protected $_filePath = '';
1720

@@ -77,15 +80,18 @@ public function parse(): Generator
7780
$tokenizer = new Tokenizer($this->getDelimiter());
7881
$tokens = [];
7982
$filePath = $this->getFilePath();
83+
$size = filesize($filePath);
8084
$fh = fopen($filePath, 'r');
8185
if (!is_resource($fh)) {
8286
throw new Exception\FileReadException($filePath);
8387
}
8488
if (!flock($fh, LOCK_SH)) {
8589
throw new Exception\FileLockException($filePath);
8690
}
91+
$pos = 0;
8792
while (!feof($fh)) {
8893
$line = fread($fh, $this->_chunkSize);
94+
$pos += min(strlen($line), $this->_chunkSize);
8995
foreach($tokenizer->append($line, feof($fh)) as $token) {
9096
switch($token->type) {
9197
case 'COMMENT_SINGLE':
@@ -101,11 +107,19 @@ public function parse(): Generator
101107
break;
102108
}
103109
}
110+
foreach($this->_onProgress as $callback) {
111+
$callback($pos, $size);
112+
}
104113
}
105114
flock($fh, LOCK_UN);
106115
fclose($fh);
107116
if(count($tokens) > 0) {
108117
yield Token::Assemble(...$tokens);
109118
}
110119
}
120+
121+
public function onProgress(callable $callback): void
122+
{
123+
$this->_onProgress[] = $callback;
124+
}
111125
}

0 commit comments

Comments
 (0)