Files
logview/app/common/Implement/Parser.php
2023-05-19 11:16:50 -04:00

62 lines
1.6 KiB
PHP

<?php
namespace ProVM\Common\Implement;
use Exception;
use ProVM\Common\Define\Log;
use ProVM\Logview\Exception\Parse\EmptyException;
use ProVM\Logview\Exception\Parse\EmptyLineException;
use ProVM\Logview\Log as LogContent;
use ProVM\Common\Define\Parser as Definition;
use function Safe\{fopen, fclose};
abstract class Parser implements Definition
{
public function isMultiline(string $filename): bool
{
return false;
}
public function total(string $filename): int
{
try {
$fh = fopen($filename, 'r');
$cnt = 0;
while(!feof($fh)) {
$line = fgets($fh);
$cnt ++;
}
fclose($fh);
return $cnt;
} catch (Exception $e) {
return 0;
}
}
public function parse(mixed &$file_handler): Log
{
$content = fgets($file_handler);
if (!$content) {
$meta_data = stream_get_meta_data($file_handler);
throw new EmptyException($meta_data['uri'], ftell($file_handler));
}
if (trim($content) === '') {
$meta_data = stream_get_meta_data($file_handler);
throw new EmptyLineException($meta_data['uri'], ftell($file_handler));
}
return new LogContent($content);
}
public function advance(mixed &$file_handler, int $offset): void
{
if ($offset === 0) {
return;
}
$cnt = 0;
while(!feof($file_handler)) {
fgets($file_handler);
$cnt ++;
if ($cnt >= $offset) {
break;
}
}
}
}