diff --git a/src/Command/ImportHeidelbergCommand.php b/src/Command/ImportHeidelbergCommand.php index b17b6ef..c9a5216 100644 --- a/src/Command/ImportHeidelbergCommand.php +++ b/src/Command/ImportHeidelbergCommand.php @@ -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,23 +36,34 @@ 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) { $basename = basename($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));