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 Doctrine\ORM\EntityManagerInterface;
use Pdo\Sqlite;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@@ -35,22 +36,33 @@ class ImportHeidelbergCommand extends Command
/**
* @param EntityManagerInterface $emi
*/
public function __construct(EntityManagerInterface $emi)
{
public function __construct(
EntityManagerInterface $emi,
private string $dir = ''
) {
parent::__construct();
$this->emi = $emi;
}
protected function configure(): void
{
$this
->addArgument('directory', InputArgument::REQUIRED, 'Directory to crawl')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
$this->dir = $input->getArgument('directory');
$files = glob('references/Heidelberg Catechism/*.md');
natsort($files);
if (!$this->dir || !file_exists($this->dir)) {
$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));
foreach ($files as $file) {
@@ -64,6 +76,18 @@ class ImportHeidelbergCommand extends Command
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
{
$md = trim(file_get_contents($file));