mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Decouple database commands from AbstractDatabaseCommand
This commit is contained in:
@@ -6,31 +6,17 @@ namespace Shlinkio\Shlink\CLI\Command\Db;
|
||||
|
||||
use Shlinkio\Shlink\CLI\Command\Util\CommandUtils;
|
||||
use Shlinkio\Shlink\CLI\Command\Util\LockConfig;
|
||||
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Lock\LockFactory;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
|
||||
abstract class AbstractDatabaseCommand extends Command
|
||||
{
|
||||
private string $phpBinary;
|
||||
|
||||
public function __construct(
|
||||
private readonly LockFactory $locker,
|
||||
private readonly ProcessRunnerInterface $processRunner,
|
||||
PhpExecutableFinder $phpFinder,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->phpBinary = $phpFinder->find(false) ?: 'php';
|
||||
}
|
||||
|
||||
protected function runPhpCommand(OutputInterface $output, array $command): void
|
||||
public function __construct(private readonly LockFactory $locker)
|
||||
{
|
||||
$command = [$this->phpBinary, ...$command, '--no-interaction'];
|
||||
$this->processRunner->run($output, $command);
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
final protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
|
||||
@@ -12,7 +12,6 @@ use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Lock\LockFactory;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
use Throwable;
|
||||
|
||||
use function array_map;
|
||||
@@ -24,18 +23,17 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
|
||||
private readonly Connection $regularConn;
|
||||
|
||||
public const string NAME = 'db:create';
|
||||
public const string DOCTRINE_SCRIPT = 'bin/doctrine';
|
||||
public const string DOCTRINE_CREATE_SCHEMA_COMMAND = 'orm:schema-tool:create';
|
||||
public const string SCRIPT = 'bin/doctrine';
|
||||
public const string COMMAND = 'orm:schema-tool:create';
|
||||
|
||||
public function __construct(
|
||||
LockFactory $locker,
|
||||
ProcessRunnerInterface $processRunner,
|
||||
PhpExecutableFinder $phpFinder,
|
||||
private readonly ProcessRunnerInterface $processRunner,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Connection $noDbNameConn,
|
||||
) {
|
||||
$this->regularConn = $this->em->getConnection();
|
||||
parent::__construct($locker, $processRunner, $phpFinder);
|
||||
parent::__construct($locker);
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
@@ -59,7 +57,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
|
||||
|
||||
// Create database
|
||||
$io->writeln('<fg=blue>Creating database tables...</>');
|
||||
$this->runPhpCommand($output, [self::DOCTRINE_SCRIPT, self::DOCTRINE_CREATE_SCHEMA_COMMAND]);
|
||||
$this->processRunner->run($output, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
||||
$io->success('Database properly created!');
|
||||
|
||||
return self::SUCCESS;
|
||||
|
||||
@@ -4,15 +4,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Command\Db;
|
||||
|
||||
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Lock\LockFactory;
|
||||
|
||||
class MigrateDatabaseCommand extends AbstractDatabaseCommand
|
||||
{
|
||||
public const string NAME = 'db:migrate';
|
||||
public const string DOCTRINE_MIGRATIONS_SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
|
||||
public const string DOCTRINE_MIGRATE_COMMAND = 'migrations:migrate';
|
||||
public const string SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
|
||||
public const string COMMAND = 'migrations:migrate';
|
||||
|
||||
public function __construct(
|
||||
LockFactory $locker,
|
||||
private readonly ProcessRunnerInterface $processRunner,
|
||||
) {
|
||||
parent::__construct($locker);
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
@@ -27,7 +36,7 @@ class MigrateDatabaseCommand extends AbstractDatabaseCommand
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$io->writeln('<fg=blue>Migrating database...</>');
|
||||
$this->runPhpCommand($output, [self::DOCTRINE_MIGRATIONS_SCRIPT, self::DOCTRINE_MIGRATE_COMMAND]);
|
||||
$this->processRunner->run($output, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
||||
$io->success('Database properly migrated!');
|
||||
|
||||
return self::SUCCESS;
|
||||
|
||||
26
module/CLI/src/Util/PhpProcessRunner.php
Normal file
26
module/CLI/src/Util/PhpProcessRunner.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Util;
|
||||
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
|
||||
/**
|
||||
* Wraps another process manager prefixing any command run with current PHP binary
|
||||
*/
|
||||
readonly class PhpProcessRunner implements ProcessRunnerInterface
|
||||
{
|
||||
private string $phpBinary;
|
||||
|
||||
public function __construct(private ProcessRunnerInterface $wrappedProcessRunner, PhpExecutableFinder $phpFinder)
|
||||
{
|
||||
$this->phpBinary = $phpFinder->find(includeArgs: false) ?: 'php';
|
||||
}
|
||||
|
||||
public function run(OutputInterface $output, array $cmd): void
|
||||
{
|
||||
$this->wrappedProcessRunner->run($output, [$this->phpBinary, ...$cmd]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user