Skip to content

Commit 9b8bb84

Browse files
committed
Add importer class
1 parent 51e65ae commit 9b8bb84

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/Importer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Xhgui\Profiler;
4+
5+
use Exception;
6+
use Xhgui\Profiler\Saver\SaverInterface;
7+
8+
final class Importer
9+
{
10+
/** @var SaverInterface */
11+
private $saver;
12+
13+
public function __construct(SaverInterface $saver)
14+
{
15+
$this->saver = $saver;
16+
}
17+
18+
public function import($stream)
19+
{
20+
$saver = $this->saver;
21+
$lines = 0;
22+
while (!feof($stream)) {
23+
$line = trim(fgets($stream));
24+
if (!$line) {
25+
continue;
26+
}
27+
$data = json_decode($line, true);
28+
if (!$data) {
29+
error_log("Ignoring malformed JSON line: $line");
30+
continue;
31+
}
32+
33+
try {
34+
$saver->save($data);
35+
$lines++;
36+
} catch (Exception $e) {
37+
error_log($e);
38+
}
39+
}
40+
error_log("Imported {$lines} lines");
41+
}
42+
}

0 commit comments

Comments
 (0)