Skip to content

Commit

Permalink
Extract get namespace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Myks92 committed Apr 22, 2024
1 parent 18f1ded commit 7a8f2ba
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 89 deletions.
32 changes: 1 addition & 31 deletions src/Tools/Console/Command/DiffCommand.php
Expand Up @@ -9,21 +9,15 @@
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Doctrine\SqlFormatter\SqlFormatter;
use OutOfBoundsException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

use function addslashes;
use function array_keys;
use function assert;
use function class_exists;
use function count;
use function filter_var;
use function is_string;
use function key;
use function sprintf;

use const FILTER_VALIDATE_BOOLEAN;
Expand Down Expand Up @@ -112,10 +106,6 @@ protected function execute(
$allowEmptyDiff = $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$fromEmptySchema = $input->getOption('from-empty-schema');
$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

if ($formatted) {
if (! class_exists(SqlFormatter::class)) {
Expand All @@ -125,27 +115,7 @@ protected function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

if (! isset($dirs[$namespace])) {
throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();
Expand Down
38 changes: 38 additions & 0 deletions src/Tools/Console/Command/DoctrineCommand.php
Expand Up @@ -10,16 +10,22 @@
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function array_keys;
use function assert;
use function count;
use function is_string;
use function key;
use function sprintf;

/**
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
Expand Down Expand Up @@ -138,4 +144,36 @@ private function setNamedEmOrConnection(InputInterface $input): void
return;
}
}

protected function getNamespace(InputInterface $input, OutputInterface $output): string
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

if (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace "%s"', $namespace));
}

assert(is_string($namespace));

return $namespace;
}
}
26 changes: 1 addition & 25 deletions src/Tools/Console/Command/DumpSchemaCommand.php
Expand Up @@ -11,15 +11,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

use function addslashes;
use function array_keys;
use function assert;
use function class_exists;
use function count;
use function is_string;
use function key;
use function sprintf;
use function str_contains;

Expand Down Expand Up @@ -94,25 +88,7 @@ public function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$this->checkNoPreviousDumpExistsForNamespace($namespace);

Expand Down
34 changes: 1 addition & 33 deletions src/Tools/Console/Command/GenerateCommand.php
Expand Up @@ -4,18 +4,11 @@

namespace Doctrine\Migrations\Tools\Console\Command;

use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

use function array_keys;
use function assert;
use function count;
use function is_string;
use function key;
use function sprintf;

/**
Expand Down Expand Up @@ -50,34 +43,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

if (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);

Expand Down

0 comments on commit 7a8f2ba

Please sign in to comment.