Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -13,8 +13,8 @@ namespace Symfony\Component\Console\Style;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Decorates output to add console style guide helpers.
@@ -25,9 +25,6 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
{
private $output;
/**
* @param OutputInterface $output
*/
public function __construct(OutputInterface $output)
{
$this->output = $output;
@@ -36,17 +33,12 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function newLine($count = 1)
public function newLine(int $count = 1)
{
$this->output->write(str_repeat(PHP_EOL, $count));
$this->output->write(str_repeat(\PHP_EOL, $count));
}
/**
* @param int $max
*
* @return ProgressBar
*/
public function createProgressBar($max = 0)
public function createProgressBar(int $max = 0): ProgressBar
{
return new ProgressBar($this->output, $max);
}
@@ -54,7 +46,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
public function write(string|iterable $messages, bool $newline = false, int $type = self::OUTPUT_NORMAL)
{
$this->output->write($messages, $newline, $type);
}
@@ -62,7 +54,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function writeln($messages, $type = self::OUTPUT_NORMAL)
public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL)
{
$this->output->writeln($messages, $type);
}
@@ -70,7 +62,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function setVerbosity($level)
public function setVerbosity(int $level)
{
$this->output->setVerbosity($level);
}
@@ -78,7 +70,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function getVerbosity()
public function getVerbosity(): int
{
return $this->output->getVerbosity();
}
@@ -86,7 +78,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function setDecorated($decorated)
public function setDecorated(bool $decorated)
{
$this->output->setDecorated($decorated);
}
@@ -94,7 +86,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function isDecorated()
public function isDecorated(): bool
{
return $this->output->isDecorated();
}
@@ -110,7 +102,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function getFormatter()
public function getFormatter(): OutputFormatterInterface
{
return $this->output->getFormatter();
}
@@ -118,7 +110,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function isQuiet()
public function isQuiet(): bool
{
return $this->output->isQuiet();
}
@@ -126,7 +118,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function isVerbose()
public function isVerbose(): bool
{
return $this->output->isVerbose();
}
@@ -134,7 +126,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function isVeryVerbose()
public function isVeryVerbose(): bool
{
return $this->output->isVeryVerbose();
}
@@ -142,7 +134,7 @@ abstract class OutputStyle implements OutputInterface, StyleInterface
/**
* {@inheritdoc}
*/
public function isDebug()
public function isDebug(): bool
{
return $this->output->isDebug();
}

View File

@@ -20,137 +20,88 @@ interface StyleInterface
{
/**
* Formats a command title.
*
* @param string $message
*/
public function title($message);
public function title(string $message);
/**
* Formats a section title.
*
* @param string $message
*/
public function section($message);
public function section(string $message);
/**
* Formats a list.
*
* @param array $elements
*/
public function listing(array $elements);
/**
* Formats informational text.
*
* @param string|array $message
*/
public function text($message);
public function text(string|array $message);
/**
* Formats a success result bar.
*
* @param string|array $message
*/
public function success($message);
public function success(string|array $message);
/**
* Formats an error result bar.
*
* @param string|array $message
*/
public function error($message);
public function error(string|array $message);
/**
* Formats an warning result bar.
*
* @param string|array $message
*/
public function warning($message);
public function warning(string|array $message);
/**
* Formats a note admonition.
*
* @param string|array $message
*/
public function note($message);
public function note(string|array $message);
/**
* Formats a caution admonition.
*
* @param string|array $message
*/
public function caution($message);
public function caution(string|array $message);
/**
* Formats a table.
*
* @param array $headers
* @param array $rows
*/
public function table(array $headers, array $rows);
/**
* Asks a question.
*
* @param string $question
* @param string|null $default
* @param callable|null $validator
*
* @return string
*/
public function ask($question, $default = null, $validator = null);
public function ask(string $question, string $default = null, callable $validator = null): mixed;
/**
* Asks a question with the user input hidden.
*
* @param string $question
* @param callable|null $validator
*
* @return string
*/
public function askHidden($question, $validator = null);
public function askHidden(string $question, callable $validator = null): mixed;
/**
* Asks for confirmation.
*
* @param string $question
* @param bool $default
*
* @return bool
*/
public function confirm($question, $default = true);
public function confirm(string $question, bool $default = true): bool;
/**
* Asks a choice question.
*
* @param string $question
* @param array $choices
* @param string|int|null $default
*
* @return string
*/
public function choice($question, array $choices, $default = null);
public function choice(string $question, array $choices, mixed $default = null): mixed;
/**
* Add newline(s).
*
* @param int $count The number of newlines
*/
public function newLine($count = 1);
public function newLine(int $count = 1);
/**
* Starts the progress output.
*
* @param int $max Maximum steps (0 if unknown)
*/
public function progressStart($max = 0);
public function progressStart(int $max = 0);
/**
* Advances the progress output X steps.
*
* @param int $step Number of steps to advance
*/
public function progressAdvance($step = 1);
public function progressAdvance(int $step = 1);
/**
* Finishes the progress output.

View File

@@ -11,15 +11,19 @@
namespace Symfony\Component\Console\Style;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\TrimmedBufferOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
@@ -32,42 +36,32 @@ use Symfony\Component\Console\Terminal;
*/
class SymfonyStyle extends OutputStyle
{
const MAX_LINE_LENGTH = 120;
public const MAX_LINE_LENGTH = 120;
private $input;
private $output;
private $questionHelper;
private $progressBar;
private $lineLength;
private int $lineLength;
private $bufferedOutput;
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
$this->bufferedOutput = new TrimmedBufferOutput(\DIRECTORY_SEPARATOR === '\\' ? 4 : 2, $output->getVerbosity(), false, clone $output->getFormatter());
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
$this->lineLength = min($width - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
$this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
parent::__construct($output);
parent::__construct($this->output = $output);
}
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string|null $type The block type (added in [] on first line)
* @param string|null $style The style to apply to the whole block
* @param string $prefix The prefix for the block
* @param bool $padding Whether to add vertical padding
* @param bool $escape Whether to escape the message
*/
public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = true)
public function block(string|array $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = true)
{
$messages = is_array($messages) ? array_values($messages) : array($messages);
$messages = \is_array($messages) ? array_values($messages) : [$messages];
$this->autoPrependBlock();
$this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, $escape));
@@ -77,26 +71,26 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function title($message)
public function title(string $message)
{
$this->autoPrependBlock();
$this->writeln(array(
$this->writeln([
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
));
sprintf('<comment>%s</>', str_repeat('=', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))),
]);
$this->newLine();
}
/**
* {@inheritdoc}
*/
public function section($message)
public function section(string $message)
{
$this->autoPrependBlock();
$this->writeln(array(
$this->writeln([
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
));
sprintf('<comment>%s</>', str_repeat('-', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))),
]);
$this->newLine();
}
@@ -117,11 +111,11 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function text($message)
public function text(string|array $message)
{
$this->autoPrependText();
$messages = is_array($message) ? array_values($message) : array($message);
$messages = \is_array($message) ? array_values($message) : [$message];
foreach ($messages as $message) {
$this->writeln(sprintf(' %s', $message));
}
@@ -129,10 +123,8 @@ class SymfonyStyle extends OutputStyle
/**
* Formats a command comment.
*
* @param string|array $message
*/
public function comment($message)
public function comment(string|array $message)
{
$this->block($message, null, null, '<fg=default;bg=default> // </>', false, false);
}
@@ -140,7 +132,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function success($message)
public function success(string|array $message)
{
$this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
}
@@ -148,7 +140,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function error($message)
public function error(string|array $message)
{
$this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
}
@@ -156,23 +148,31 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function warning($message)
public function warning(string|array $message)
{
$this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true);
$this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ', true);
}
/**
* {@inheritdoc}
*/
public function note($message)
public function note(string|array $message)
{
$this->block($message, 'NOTE', 'fg=yellow', ' ! ');
}
/**
* Formats an info message.
*/
public function info(string|array $message)
{
$this->block($message, 'INFO', 'fg=green', ' ', true);
}
/**
* {@inheritdoc}
*/
public function caution($message)
public function caution(string|array $message)
{
$this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
}
@@ -182,22 +182,67 @@ class SymfonyStyle extends OutputStyle
*/
public function table(array $headers, array $rows)
{
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');
$this->createTable()
->setHeaders($headers)
->setRows($rows)
->render()
;
$table = new Table($this);
$table->setHeaders($headers);
$table->setRows($rows);
$table->setStyle($style);
$table->render();
$this->newLine();
}
/**
* Formats a horizontal table.
*/
public function horizontalTable(array $headers, array $rows)
{
$this->createTable()
->setHorizontal(true)
->setHeaders($headers)
->setRows($rows)
->render()
;
$this->newLine();
}
/**
* Formats a list of key/value horizontally.
*
* Each row can be one of:
* * 'A title'
* * ['key' => 'value']
* * new TableSeparator()
*/
public function definitionList(string|array|TableSeparator ...$list)
{
$headers = [];
$row = [];
foreach ($list as $value) {
if ($value instanceof TableSeparator) {
$headers[] = $value;
$row[] = $value;
continue;
}
if (\is_string($value)) {
$headers[] = new TableCell($value, ['colspan' => 2]);
$row[] = null;
continue;
}
if (!\is_array($value)) {
throw new InvalidArgumentException('Value should be an array, string, or an instance of TableSeparator.');
}
$headers[] = key($value);
$row[] = current($value);
}
$this->horizontalTable($headers, [$row]);
}
/**
* {@inheritdoc}
*/
public function ask($question, $default = null, $validator = null)
public function ask(string $question, string $default = null, callable $validator = null): mixed
{
$question = new Question($question, $default);
$question->setValidator($validator);
@@ -208,7 +253,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function askHidden($question, $validator = null)
public function askHidden(string $question, callable $validator = null): mixed
{
$question = new Question($question);
@@ -221,7 +266,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function confirm($question, $default = true)
public function confirm(string $question, bool $default = true): bool
{
return $this->askQuestion(new ConfirmationQuestion($question, $default));
}
@@ -229,11 +274,11 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function choice($question, array $choices, $default = null)
public function choice(string $question, array $choices, mixed $default = null): mixed
{
if (null !== $default) {
$values = array_flip($choices);
$default = $values[$default];
$default = $values[$default] ?? $default;
}
return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
@@ -242,7 +287,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function progressStart($max = 0)
public function progressStart(int $max = 0)
{
$this->progressBar = $this->createProgressBar($max);
$this->progressBar->start();
@@ -251,7 +296,7 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function progressAdvance($step = 1)
public function progressAdvance(int $step = 1)
{
$this->getProgressBar()->advance($step);
}
@@ -263,17 +308,17 @@ class SymfonyStyle extends OutputStyle
{
$this->getProgressBar()->finish();
$this->newLine(2);
$this->progressBar = null;
unset($this->progressBar);
}
/**
* {@inheritdoc}
*/
public function createProgressBar($max = 0)
public function createProgressBar(int $max = 0): ProgressBar
{
$progressBar = parent::createProgressBar($max);
if ('\\' !== DIRECTORY_SEPARATOR) {
if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
$progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
$progressBar->setProgressCharacter('');
$progressBar->setBarCharacter('▓'); // dark shade character \u2593
@@ -283,19 +328,22 @@ class SymfonyStyle extends OutputStyle
}
/**
* @param Question $question
*
* @return string
* @see ProgressBar::iterate()
*/
public function askQuestion(Question $question)
public function progressIterate(iterable $iterable, int $max = null): iterable
{
yield from $this->createProgressBar()->iterate($iterable, $max);
$this->newLine(2);
}
public function askQuestion(Question $question): mixed
{
if ($this->input->isInteractive()) {
$this->autoPrependBlock();
}
if (!$this->questionHelper) {
$this->questionHelper = new SymfonyQuestionHelper();
}
$this->questionHelper ??= new SymfonyQuestionHelper();
$answer = $this->questionHelper->ask($this->input, $this, $question);
@@ -310,25 +358,37 @@ class SymfonyStyle extends OutputStyle
/**
* {@inheritdoc}
*/
public function writeln($messages, $type = self::OUTPUT_NORMAL)
public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL)
{
parent::writeln($messages, $type);
$this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
if (!is_iterable($messages)) {
$messages = [$messages];
}
foreach ($messages as $message) {
parent::writeln($message, $type);
$this->writeBuffer($message, true, $type);
}
}
/**
* {@inheritdoc}
*/
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
public function write(string|iterable $messages, bool $newline = false, int $type = self::OUTPUT_NORMAL)
{
parent::write($messages, $newline, $type);
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
if (!is_iterable($messages)) {
$messages = [$messages];
}
foreach ($messages as $message) {
parent::write($message, $newline, $type);
$this->writeBuffer($message, $newline, $type);
}
}
/**
* {@inheritdoc}
*/
public function newLine($count = 1)
public function newLine(int $count = 1)
{
parent::newLine($count);
$this->bufferedOutput->write(str_repeat("\n", $count));
@@ -336,64 +396,64 @@ class SymfonyStyle extends OutputStyle
/**
* Returns a new instance which makes use of stderr if available.
*
* @return self
*/
public function getErrorStyle()
public function getErrorStyle(): self
{
return new self($this->input, $this->getErrorOutput());
}
/**
* @return ProgressBar
*/
private function getProgressBar()
public function createTable(): Table
{
if (!$this->progressBar) {
throw new RuntimeException('The ProgressBar is not started.');
}
$output = $this->output instanceof ConsoleOutputInterface ? $this->output->section() : $this->output;
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');
return $this->progressBar;
return (new Table($output))->setStyle($style);
}
private function autoPrependBlock()
private function getProgressBar(): ProgressBar
{
$chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
return $this->progressBar
?? throw new RuntimeException('The ProgressBar is not started.');
}
private function autoPrependBlock(): void
{
$chars = substr(str_replace(\PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
if (!isset($chars[0])) {
return $this->newLine(); //empty history, so we should start with a new line.
$this->newLine(); // empty history, so we should start with a new line.
return;
}
//Prepend new line for each non LF chars (This means no blank line was output before)
// Prepend new line for each non LF chars (This means no blank line was output before)
$this->newLine(2 - substr_count($chars, "\n"));
}
private function autoPrependText()
private function autoPrependText(): void
{
$fetched = $this->bufferedOutput->fetch();
//Prepend new line if last char isn't EOL:
if ("\n" !== substr($fetched, -1)) {
// Prepend new line if last char isn't EOL:
if (!str_ends_with($fetched, "\n")) {
$this->newLine();
}
}
private function reduceBuffer($messages)
private function writeBuffer(string $message, bool $newLine, int $type): void
{
// We need to know if the two last chars are PHP_EOL
// Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
return array_map(function ($value) {
return substr($value, -4);
}, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
// We need to know if the last chars are PHP_EOL
$this->bufferedOutput->write($message, $newLine, $type);
}
private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array
{
$indentLength = 0;
$prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
$lines = array();
$prefixLength = Helper::width(Helper::removeDecoration($this->getFormatter(), $prefix));
$lines = [];
if (null !== $type) {
$type = sprintf('[%s] ', $type);
$indentLength = strlen($type);
$indentLength = \strlen($type);
$lineIndentation = str_repeat(' ', $indentLength);
}
@@ -403,9 +463,14 @@ class SymfonyStyle extends OutputStyle
$message = OutputFormatter::escape($message);
}
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
$decorationLength = Helper::width($message) - Helper::width(Helper::removeDecoration($this->getFormatter(), $message));
$messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength);
$messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true));
foreach ($messageLines as $messageLine) {
$lines[] = $messageLine;
}
if (count($messages) > 1 && $key < count($messages) - 1) {
if (\count($messages) > 1 && $key < \count($messages) - 1) {
$lines[] = '';
}
}
@@ -423,7 +488,7 @@ class SymfonyStyle extends OutputStyle
}
$line = $prefix.$line;
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
$line .= str_repeat(' ', max($this->lineLength - Helper::width(Helper::removeDecoration($this->getFormatter(), $line)), 0));
if ($style) {
$line = sprintf('<%s>%s</>', $style, $line);