From 8152164c816a4c6dcd91280e775b99875c8fcd73 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 26 Aug 2024 09:08:04 +0200 Subject: [PATCH] Remove ConsoleLogger --- phpstan.neon.dist | 3 - src/Tools/Console/Command/DoctrineCommand.php | 14 +- src/Tools/Console/ConsoleLogger.php | 134 ------------------ tests/Tools/Console/ConsoleLoggerTest.php | 74 ---------- 4 files changed, 12 insertions(+), 213 deletions(-) delete mode 100644 src/Tools/Console/ConsoleLogger.php delete mode 100644 tests/Tools/Console/ConsoleLoggerTest.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6d86b7af3..a455f7dff 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -17,9 +17,6 @@ parameters: - message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~' path: src/InlineParameterFormatter.php - - - message: '~^Call to an undefined method Symfony\\Component\\Console\\Output\\OutputInterface\:\:getErrorOutput\(\)\.$~' - path: src/Tools/Console/ConsoleLogger.php # https://github.com/phpstan/phpstan/issues/5982 - diff --git a/src/Tools/Console/Command/DoctrineCommand.php b/src/Tools/Console/Command/DoctrineCommand.php index 661f30d54..c1003bce7 100644 --- a/src/Tools/Console/Command/DoctrineCommand.php +++ b/src/Tools/Console/Command/DoctrineCommand.php @@ -7,14 +7,15 @@ use Doctrine\Migrations\Configuration\Connection\ConfigurationFile; use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback; use Doctrine\Migrations\DependencyFactory; -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 Psr\Log\LogLevel; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Style\StyleInterface; @@ -104,7 +105,16 @@ protected function initialize(InputInterface $input, OutputInterface $output): v return; } - $logger = new ConsoleLogger($output); + $logger = new ConsoleLogger($output, [ + LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL, + LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL, + LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL, + LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL, + LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, + LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, + LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, + LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE, + ]); $dependencyFactory->setService(LoggerInterface::class, $logger); $dependencyFactory->freeze(); } diff --git a/src/Tools/Console/ConsoleLogger.php b/src/Tools/Console/ConsoleLogger.php deleted file mode 100644 index 800c20c14..000000000 --- a/src/Tools/Console/ConsoleLogger.php +++ /dev/null @@ -1,134 +0,0 @@ - */ - private array $verbosityLevelMap = [ - LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL, - LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL, - LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL, - LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL, - LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, - LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, - LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, - LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE, - ]; - /** @var array */ - private array $formatLevelMap = [ - LogLevel::EMERGENCY => self::ERROR, - LogLevel::ALERT => self::ERROR, - LogLevel::CRITICAL => self::ERROR, - LogLevel::ERROR => self::ERROR, - LogLevel::WARNING => self::INFO, - LogLevel::NOTICE => self::INFO, - LogLevel::INFO => self::INFO, - LogLevel::DEBUG => self::INFO, - ]; - - /** - * @param array $verbosityLevelMap - * @param array $formatLevelMap - */ - public function __construct( - private readonly OutputInterface $output, - array $verbosityLevelMap = [], - array $formatLevelMap = [], - ) { - $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap; - $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap; - } - - /** - * {@inheritDoc} - * - * @param string|Stringable $message - * @param mixed[] $context - */ - public function log($level, $message, array $context = []): void - { - if (! isset($this->verbosityLevelMap[$level])) { - throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level)); - } - - $output = $this->output; - - // Write to the error output if necessary and available - if ($this->formatLevelMap[$level] === self::ERROR) { - if ($this->output instanceof ConsoleOutputInterface) { - $output = $output->getErrorOutput(); - } - } - - // the if condition check isn't necessary -- it's the same one that $output will do internally anyway. - // We only do it for efficiency here as the message formatting is relatively expensive. - if ($output->getVerbosity() < $this->verbosityLevelMap[$level]) { - return; - } - - $output->writeln(sprintf('<%1$s>[%2$s] %3$s', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]); - } - - /** - * Interpolates context values into the message placeholders. - * - * @param mixed[] $context - */ - private function interpolate(string|Stringable $message, array $context): string - { - $message = (string) $message; - if (! str_contains($message, '{')) { - return $message; - } - - $replacements = []; - foreach ($context as $key => $val) { - if ($val === null || is_scalar($val) || $val instanceof Stringable) { - $replacements["{{$key}}"] = $val; - } elseif ($val instanceof DateTimeInterface) { - $replacements["{{$key}}"] = $val->format(DateTime::RFC3339); - } elseif (is_object($val)) { - $replacements["{{$key}}"] = '[object ' . $val::class . ']'; - } else { - $replacements["{{$key}}"] = '[' . gettype($val) . ']'; - } - - if (! isset($replacements["{{$key}}"])) { - continue; - } - - $replacements["{{$key}}"] = '' . $replacements["{{$key}}"] . ''; - } - - return strtr($message, $replacements); - } -} diff --git a/tests/Tools/Console/ConsoleLoggerTest.php b/tests/Tools/Console/ConsoleLoggerTest.php deleted file mode 100644 index b8aac2d91..000000000 --- a/tests/Tools/Console/ConsoleLoggerTest.php +++ /dev/null @@ -1,74 +0,0 @@ -output = new BufferedOutput(); - } - - public function testNoInfoAndDebugAsDefault(): void - { - $logger = new ConsoleLogger($this->output); - $logger->info('foo'); - $logger->debug('bar'); - - self::assertSame('', $this->output->fetch()); - } - - public function testLevelCanBeChanged(): void - { - $logger = new ConsoleLogger($this->output, [LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL]); - $logger->info('foo'); - $logger->debug('bar'); - - self::assertSame('[info] foo' . PHP_EOL, $this->output->fetch()); - } - - public function testVerbosityIsREspected(): void - { - $this->output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); - - $logger = new ConsoleLogger($this->output); - $logger->info('foo'); - $logger->debug('bar'); - - self::assertSame( - '[info] foo' . PHP_EOL . '[debug] bar' . PHP_EOL, - $this->output->fetch(), - ); - } - - public function testInterpolation(): void - { - $logger = new ConsoleLogger($this->output); - $logger->error('foo {number} {date} {object} {resource} {missing} bar', [ - 'number' => 1, - 'date' => new DateTime('2010-01-01 00:08:09+00:00'), - 'object' => new stdClass(), - 'resource' => fopen('php://output', 'w'), - ]); - self::assertSame( - '[error] foo 1 2010-01-01T00:08:09+00:00 [object stdClass] [resource] {missing} bar' . PHP_EOL, - $this->output->fetch(), - ); - } -}