en_btr_backend/app/Handlers/LexiconEntryImportHandler.php

66 lines
1.6 KiB
PHP

<?php
namespace App\Handlers;
use App\LexicalEntry;
/**
* LexiconEntryImportHandler.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 10/6/20
* Time: 8:28 PM
*/
class LexiconEntryImportHandler
{
use LexiconEntryTrait;
const REPO_FOLDER = '/gwt/';
public function run() : void
{
// Iterate over each folder in the repo
$handle = opendir($this->getFolder());
while (($dir = readdir($handle)) !== false) {
if (is_dir($this->getFolder() . $dir)) {
$this->scanLexiconDirectory($this->getFolder() . $dir);
}
// Silently ignoring files at this level
}
closedir($handle);
}
protected function scanLexiconDirectory(string $dir) : void
{
$handle = opendir($dir);
while (($item = readdir($handle)) !== false) {
if ((substr($item, 0, 1) === 'g') && is_file($dir . '/' . $item)) {
$parts = explode('.', substr($item, 1));
$this->importEntryFile($dir . '/' . $item, $parts[0]);
}
}
closedir($handle);
}
protected function importEntryFile(string $file, string $stongsNumber) : LexicalEntry
{
$contents = file_get_contents($file);
list($lemma, $commentary) = $this->parseEntry($contents);
$entry = LexicalEntry::create([
'id' => $stongsNumber,
'lemma' => $lemma,
'commentary' => $commentary,
]);
return $entry;
}
protected function getFolder() : string
{
return storage_path() . self::REPO_FOLDER;
}
}