41 lines
945 B
PHP
41 lines
945 B
PHP
<?php
|
|
|
|
class MediaDB
|
|
{
|
|
private $handler;
|
|
private array $lines = [];
|
|
|
|
public function __construct()
|
|
{
|
|
// Just initialize the file handler
|
|
$this->handler = fopen($GLOBALS['path_mediadb'], 'r');
|
|
if ($this->handler === false)
|
|
Json::error('Error opening media DB');
|
|
}
|
|
|
|
private function getLine(): ?array
|
|
{
|
|
$ret = fgetcsv($this->handler, 0, ' ');
|
|
if ($ret === false)
|
|
return null;
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function map(callable $func)
|
|
{
|
|
// First read from what's already in memory
|
|
foreach ($this->lines as $i_line) {
|
|
if ($func($i_line) === true) return;
|
|
}
|
|
|
|
// If we run out, read from the file and append to array so the next lookup
|
|
// is fast
|
|
while ($line = $this->getLine()) {
|
|
$this->lines[] = $line;
|
|
|
|
if ($func($line) === true) return;
|
|
}
|
|
}
|
|
}
|