upd: prepared for cli execution

This commit is contained in:
2025-12-05 10:50:09 -05:00
parent 8b44329e09
commit d58721ca5b

View File

@@ -5,6 +5,7 @@ namespace App\Command;
use App\Entity\Reference; use App\Entity\Reference;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Pdo\Sqlite;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@@ -35,23 +36,34 @@ class ImportHeidelbergCommand extends Command
/** /**
* @param EntityManagerInterface $emi * @param EntityManagerInterface $emi
*/ */
public function __construct(EntityManagerInterface $emi) public function __construct(
{ EntityManagerInterface $emi,
private string $dir = ''
) {
parent::__construct(); parent::__construct();
$this->emi = $emi; $this->emi = $emi;
} }
protected function configure(): void protected function configure(): void
{ {
$this
->addArgument('directory', InputArgument::REQUIRED, 'Directory to crawl')
;
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$this->io = new SymfonyStyle($input, $output); $this->io = new SymfonyStyle($input, $output);
$this->dir = $input->getArgument('directory');
$files = glob('references/Heidelberg Catechism/*.md'); if (!$this->dir || !file_exists($this->dir)) {
natsort($files); $this->io->error('Directory not specified or does not exist');
return Command::FAILURE;
}
$this->io->note("Crawling {$this->dir} for Heidelberg Catechism Q&A's");
$files = $this->getFiles();
$this->io->progressStart(count($files)); $this->io->progressStart(count($files));
foreach ($files as $file) { foreach ($files as $file) {
$basename = basename($file); $basename = basename($file);
@@ -64,6 +76,18 @@ class ImportHeidelbergCommand extends Command
return Command::SUCCESS; return Command::SUCCESS;
} }
private function getFiles(): array
{
$files = glob($this->dir . "/*.md");
if (!$this->files || count($this->files) === 0) {
$this->io->warning("No files found in this directory\n{$this->dir}");
$files = [];
}
natsort($files);
return $files;
}
private function processFile(string $file): Reference|bool private function processFile(string $file): Reference|bool
{ {
$md = trim(file_get_contents($file)); $md = trim(file_get_contents($file));