Skip to content

Commit

Permalink
Use STDERR for errors (if available)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhatlak committed Oct 16, 2024
1 parent 31d837c commit 3b65e72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
16 changes: 14 additions & 2 deletions src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use UnexpectedValueException;

Expand Down Expand Up @@ -439,7 +440,7 @@ protected function writeEnvironmentOutput(?string &$environment, OutputInterface
}

if (!$this->getConfig()->hasEnvironment($environment)) {
$output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
self::getErrorOutput($output)->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));

return false;
}
Expand Down Expand Up @@ -478,7 +479,7 @@ protected function writeInformationOutput(?string &$environment, OutputInterface
}
$output->writeln('<info>using database</info> ' . $name, $this->verbosityLevel);
} else {
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
self::getErrorOutput($output)->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');

return false;
}
Expand All @@ -492,4 +493,15 @@ protected function writeInformationOutput(?string &$environment, OutputInterface

return true;
}

/**
* Returns the error output to use
*
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
* @return \Symfony\Component\Console\Output\OutputInterface
*/
protected static function getErrorOutput(OutputInterface $output): OutputInterface
{
return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
}
}
18 changes: 10 additions & 8 deletions src/Phinx/Console/Command/ListAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ function ($alias, $class) use ($maxAliasLength, $maxClassLength) {
),
$this->verbosityLevel
);
} else {
$output->writeln(
sprintf(
'<error>No aliases defined in %s</error>',
Util::relativePath($this->config->getConfigFilePath())
)
);

return self::CODE_SUCCESS;
}

return self::CODE_SUCCESS;
self::getErrorOutput($output)->writeln(
sprintf(
'<error>No aliases defined in %s</error>',
Util::relativePath($this->config->getConfigFilePath())
)
);

return self::CODE_ERROR;
}
}
7 changes: 1 addition & 6 deletions src/Phinx/Console/Command/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Phinx\Console\Command;

use DateTime;
use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -92,12 +91,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->getManager()->migrate($environment, $version, $fake);
}
$end = microtime(true);
} catch (Exception $e) {
$output->writeln('<error>' . $e->__toString() . '</error>');

return self::CODE_ERROR;
} catch (Throwable $e) {
$output->writeln('<error>' . $e->__toString() . '</error>');
self::getErrorOutput($output)->writeln('<error>' . $e->__toString() . '</error>');

return self::CODE_ERROR;
}
Expand Down

0 comments on commit 3b65e72

Please sign in to comment.