Skip to content

Commit

Permalink
Fix: Don't initialize db inside configure methods (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Feb 28, 2025
1 parent 4924049 commit b9ec636
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
5 changes: 0 additions & 5 deletions src/Command/DownloadCloudSavesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,11 @@ public function __construct(

protected function configure(): void
{
$defaultDirectory = $_ENV['DOWNLOAD_DIRECTORY']
?? $this->persistence->getSetting(Setting::DownloadPath)
?? getcwd();

$this
->addArgument(
'directory',
InputArgument::OPTIONAL,
'The target directory.',
$defaultDirectory,
)
->addOption(
'no-verify',
Expand Down
4 changes: 0 additions & 4 deletions src/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ public function __construct(

protected function configure()
{
$defaultDirectory = $_ENV['DOWNLOAD_DIRECTORY']
?? $this->persistence->getSetting(Setting::DownloadPath)
?? getcwd();
$this
->setDescription('Downloads all files from the local database (see update command). Can resume downloads unless --no-verify is specified.')
->addArgument(
'directory',
InputArgument::OPTIONAL,
'The target directory.',
$defaultDirectory,
)
->addOption(
'no-verify',
Expand Down
6 changes: 6 additions & 0 deletions src/Command/GamesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Exception\ExitException;
use App\Service\DownloadManager;
use App\Service\OwnedItemsManager;
use App\Service\Persistence\PersistenceManager;
use App\Trait\MigrationCheckerTrait;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -17,9 +19,12 @@
#[AsCommand('games', description: 'Gets details about a game you own, or lists your games.')]
final class GamesCommand extends Command
{
use MigrationCheckerTrait;

public function __construct(
private readonly OwnedItemsManager $ownedItemsManager,
private readonly DownloadManager $downloadManager,
private readonly PersistenceManager $persistence,
) {
parent::__construct();
}
Expand All @@ -38,6 +43,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->showInfoIfMigrationsAreNeeded($io, $this->persistence);

try {
$gameName = $input->getArgument('name') ?? $this->selectGame($input, $io);
Expand Down
15 changes: 8 additions & 7 deletions src/Command/MigrateNamingSchemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ public function __construct(

protected function configure(): void
{
$defaultDirectory = $_ENV['DOWNLOAD_DIRECTORY']
?? $this->persistence->getSetting(Setting::DownloadPath)
?? null;

$this->setHidden($this->persistence->getSetting(Setting::NamingConvention) !== NamingConvention::Custom->value);
$this->setHidden(
!$this->persistence->needsMigrating()
&& $this->persistence->getSetting(Setting::NamingConvention) !== NamingConvention::Custom->value
);

$this->addArgument(
'directory',
InputArgument::OPTIONAL,
'The target directory.',
$defaultDirectory,
);
}

Expand All @@ -50,7 +48,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$this->showInfoIfMigrationsAreNeeded($io, $this->persistence);

$configuredDirectory = $input->getArgument('directory');
$configuredDirectory = $input->getArgument('directory')
?? $_ENV['DOWNLOAD_DIRECTORY']
?? $this->persistence->getSetting(Setting::DownloadPath)
;

if (!$configuredDirectory) {
if (!$input->isInteractive()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Trait/MigrationCheckerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ private function showInfoIfMigrationsAreNeeded(OutputInterface $output, Persiste
return;
}

$output->writeln("<warning>> The database needs migrating after an update, this command might take more time than usual, please don't cancel it in the middle of a migration.</warning>");
$output->writeln("<info>> The database needs migrating after an update, this command might take more time than usual, please don't cancel it in the middle of a migration.</info>");
}
}
7 changes: 6 additions & 1 deletion src/Trait/TargetDirectoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ trait TargetDirectoryTrait
{
private function getTargetDir(InputInterface $input, GameDetail $game, ?string $subdirectory = null, ?NamingConvention $namingScheme = null): string
{
$dir = $input->getArgument('directory');
$dir = $input->getArgument('directory')
?? $_ENV['DOWNLOAD_DIRECTORY']
?? $this->persistence->getSetting(Setting::DownloadPath)
?? getcwd()
;

if (PHP_OS_FAMILY === 'Windows') {
if (!preg_match('/^[a-zA-Z]:\\\\/', $dir)) {
$dir = getcwd() . '/' . $dir;
Expand Down

0 comments on commit b9ec636

Please sign in to comment.