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 static function add(array|Item $item) { $file = fopen($GLOBALS['path_mediadb'], 'a+'); if ($file === false) Json::error('Failed opening media DB for adding new Item')->die(); $line = $item->getHash() . ' ' . implode(' ', $item->getTags()); if (fwrite($file, $line) == false) Json::error('Write operation failed on Item adding')->die(); fclose($file); // The $this->list will be updated on trying to find new items } 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; } } }