Skip to content

Commit

Permalink
Merge pull request #968 from emodric/symfony_style
Browse files Browse the repository at this point in the history
Use SymfonyStyle to output text in console
  • Loading branch information
goetas authored Apr 29, 2020
2 parents 79ff97d + 362ca53 commit 9bd1bc3
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 194 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cache:

before_install:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- stty cols 120
- export COLUMNS=120

install:
- rm composer.lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
}
}

$output->writeln(sprintf(
'<info>%s</info>%s',
$this->io->text(sprintf(
"<info>%s</info>%s\n",
(string) $version,
$description !== '' ? ' - ' . $description : ''
));
Expand Down
17 changes: 9 additions & 8 deletions lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function execute(
$newMigrations = $statusCalculator->getNewMigrations();

if (! $this->checkNewMigrationsOrExecutedUnavailable($newMigrations, $executedUnavailableMigrations, $input, $output)) {
$output->writeln('<error>Migration cancelled!</error>');
$this->io->error('Migration cancelled!');

return 3;
}
Expand All @@ -149,15 +149,15 @@ protected function execute(
);
} catch (NoChangesDetected $exception) {
if ($allowEmptyDiff) {
$output->writeln($exception->getMessage());
$this->io->error($exception->getMessage());

return 0;
}

throw $exception;
}

$output->writeln([
$this->io->text([
sprintf('Generated new migration class to "<info>%s</info>"', $path),
'',
sprintf(
Expand All @@ -169,6 +169,7 @@ protected function execute(
'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
addslashes($fqcn)
),
'',
]);

return 0;
Expand All @@ -185,19 +186,19 @@ private function checkNewMigrationsOrExecutedUnavailable(
}

if (count($newMigrations) !== 0) {
$output->writeln(sprintf(
'<error>WARNING! You have %d available migrations to execute.</error>',
$this->io->warning(sprintf(
'You have %d available migrations to execute.',
count($newMigrations)
));
}

if (count($executedUnavailableMigrations) !== 0) {
$output->writeln(sprintf(
'<error>WARNING! You have %d previously executed migrations in the database that are not registered migrations.</error>',
$this->io->warning(sprintf(
'You have %d previously executed migrations in the database that are not registered migrations.',
count($executedUnavailableMigrations)
));
}

return $this->canExecute('Are you sure you wish to continue? (y/n)', $input, $output);
return $this->canExecute('Are you sure you wish to continue?', $input);
}
}
29 changes: 10 additions & 19 deletions lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function is_string;

/**
Expand All @@ -25,6 +26,9 @@ abstract class DoctrineCommand extends Command
/** @var DependencyFactory|null */
private $dependencyFactory;

/** @var StyleInterface */
protected $io;

public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
{
parent::__construct($name);
Expand Down Expand Up @@ -55,6 +59,8 @@ protected function configure() : void

protected function initialize(InputInterface $input, OutputInterface $output) : void
{
$this->io = new SymfonyStyle($input, $output);

$configurationParameter = $input->getOption('configuration');
if ($this->dependencyFactory === null) {
$configurationLoader = new ConfigurationFileWithFallback(
Expand Down Expand Up @@ -87,23 +93,8 @@ protected function getDependencyFactory() : DependencyFactory
return $this->dependencyFactory;
}

protected function askConfirmation(
string $question,
InputInterface $input,
OutputInterface $output
) : bool {
return $this->getHelper('question')->ask(
$input,
$output,
new ConfirmationQuestion($question)
);
}

protected function canExecute(
string $question,
InputInterface $input,
OutputInterface $output
) : bool {
return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
protected function canExecute(string $question, InputInterface $input) : bool
{
return ! $input->isInteractive() || $this->io->confirm($question);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function execute(
$lineLength
);

$output->writeln([
$this->io->text([
sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path),
'',
sprintf(
Expand All @@ -125,6 +125,7 @@ public function execute(
),
'',
'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.',
'',
]);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$path = is_string($path) ? $path : getcwd();

if (! is_string($path) || ! is_writable($path)) {
$output->writeln('<error>Path not writeable!</error>');
$this->io->error('Path not writeable!');

return 1;
}
Expand All @@ -134,10 +134,10 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
return 0;
}

$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)';
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue?';

if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) {
$output->writeln('<error>Migration cancelled!</error>');
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');

return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int

$path = $migrationGenerator->generateMigration($fqcn);

$output->writeln([
$this->io->text([
sprintf('Generated new migration class to "<info>%s</info>"', $path),
'',
sprintf(
Expand All @@ -79,6 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
$fqcn
),
'',
]);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$description = '';
}

$output->writeln(sprintf(
'<info>%s</info>%s',
$this->io->text(sprintf(
"<info>%s</info>%s\n",
$version,
$description !== '' ? ' - ' . $description : ''
));
Expand Down
51 changes: 25 additions & 26 deletions lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
try {
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($versionAlias);
} catch (UnknownMigrationVersion|NoMigrationsFoundWithCriteria $e) {
$this->getVersionNameFromAlias($versionAlias, $output);
$this->getVersionNameFromAlias($versionAlias);

return 1;
}
Expand All @@ -132,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();

if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input, $output) === false) {
if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input) === false) {
return 3;
}

Expand All @@ -142,13 +142,13 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$plan = $planCalculator->getPlanUntilVersion($version);

if (count($plan) === 0 && ! $allowNoMigration) {
$output->writeln('Could not find any migrations to execute.');
$this->io->warning('Could not find any migrations to execute.');

return 1;
}

if (count($plan) === 0) {
$this->getVersionNameFromAlias($versionAlias, $output);
$this->getVersionNameFromAlias($versionAlias);

return 0;
}
Expand All @@ -161,7 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$path = is_string($path) ? $path : getcwd();

if (! is_string($path) || ! is_writable($path)) {
$output->writeln('<error>Path not writeable!</error>');
$this->io->error('Path not writeable!');

return 1;
}
Expand All @@ -172,10 +172,10 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
return 0;
}

$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)';
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?';

if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) {
$output->writeln('<error>Migration cancelled!</error>');
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');

return 3;
}
Expand All @@ -192,34 +192,35 @@ protected function execute(InputInterface $input, OutputInterface $output) : int

$migrator->migrate($plan, $migratorConfiguration);

$this->io->newLine();

return 0;
}

private function checkExecutedUnavailableMigrations(
ExecutedMigrationsSet $executedUnavailableMigrations,
InputInterface $input,
OutputInterface $output
InputInterface $input
) : bool {
if (count($executedUnavailableMigrations) !== 0) {
$output->writeln(sprintf(
'<error>WARNING! You have %s previously executed migrations in the database that are not registered migrations.</error>',
$this->io->warning(sprintf(
'You have %s previously executed migrations in the database that are not registered migrations.',
count($executedUnavailableMigrations)
));

foreach ($executedUnavailableMigrations->getItems() as $executedUnavailableMigration) {
$output->writeln(sprintf(
' <comment>>></comment> %s (<comment>%s</comment>)',
$this->io->text(sprintf(
'<comment>>></comment> %s (<comment>%s</comment>)',
$executedUnavailableMigration->getExecutedAt() !== null
? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s')
: null,
$executedUnavailableMigration->getVersion()
));
}

$question = 'Are you sure you wish to continue? (y/n)';
$question = 'Are you sure you wish to continue?';

if (! $this->canExecute($question, $input, $output)) {
$output->writeln('<error>Migration cancelled!</error>');
if (! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');

return false;
}
Expand All @@ -228,30 +229,28 @@ private function checkExecutedUnavailableMigrations(
return true;
}

private function getVersionNameFromAlias(
string $versionAlias,
OutputInterface $output
) : void {
private function getVersionNameFromAlias(string $versionAlias) : void
{
if ($versionAlias === 'first') {
$output->writeln('<error>Already at first version.</error>');
$this->io->error('Already at first version.');

return;
}

if ($versionAlias === 'next' || $versionAlias === 'latest') {
$output->writeln('<error>Already at latest version.</error>');
$this->io->error('Already at latest version.');

return;
}

if (substr($versionAlias, 0, 7) === 'current') {
$output->writeln('<error>The delta couldn\'t be reached.</error>');
$this->io->error('The delta couldn\'t be reached.');

return;
}

$output->writeln(sprintf(
'<error>Unknown version: %s</error>',
$this->io->error(sprintf(
'Unknown version: %s',
OutputFormatter::escape($versionAlias)
));
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/Migrations/Tools/Console/Command/RollupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ protected function configure() : void

protected function execute(InputInterface $input, OutputInterface $output) : int
{
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)';
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?';

if (! $this->canExecute($question, $input, $output)) {
$output->writeln('<error>Migration cancelled!</error>');
if (! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');

return 3;
}

$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();
$version = $this->getDependencyFactory()->getRollup()->rollup();

$output->writeln(sprintf(
'Rolled up migrations to version <info>%s</info>',
$this->io->success(sprintf(
'Rolled up migrations to version %s',
(string) $version
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function execute(
) : int {
$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();

$output->writeln('Metadata storage synchronized');
$this->io->success('Metadata storage synchronized');

return 0;
}
Expand Down
Loading

0 comments on commit 9bd1bc3

Please sign in to comment.