From 0c653a0c4826f9977001541f53b29b317eff163d Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 07:39:49 -0400 Subject: [PATCH 01/34] added console style guide helpers --- .../Console/Helper/FormatterHelper.php | 156 +++++++++++++++++- 1 file changed, 151 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index ac736f982e5c1..cac3a0ace529b 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -37,13 +37,14 @@ public function formatSection($section, $message, $style = 'info') /** * Formats a message as a block of text. * - * @param string|array $messages The message to write in the block - * @param string $style The style to apply to the whole block - * @param bool $large Whether to return a large block + * @param string|array $messages The message to write in the block + * @param string $style The style to apply to the whole block + * @param bool $large Whether to return a large block + * @param int $padLength ength to pad the messages * * @return string The formatter message */ - public function formatBlock($messages, $style, $large = false) + public function formatBlock($messages, $style, $large = false, $padLength = 0) { if (!is_array($messages)) { $messages = array($messages); @@ -66,12 +67,129 @@ public function formatBlock($messages, $style, $large = false) } for ($i = 0; isset($messages[$i]); ++$i) { - $messages[$i] = sprintf('<%s>%s', $style, $messages[$i], $style); + $messages[$i] = sprintf('<%s>%s', $style, str_pad($messages[$i], $padLength), $style); } return implode("\n", $messages); } + /** + * Formats a command title + * + * @param string $message + * + * @return array + */ + public function formatTitle($message) + { + return array( + '', + sprintf('%s', $message), + sprintf('%s', str_repeat('=', strlen($message))), + '' + ); + } + + /** + * Formats a section title + * + * @param string $message + * + * @return array + */ + public function formatSectionTitle($message) + { + return array( + sprintf('%s', $message), + sprintf('%s', str_repeat('-', strlen($message))), + '' + ); + } + + /** + * Formats a list element + * + * @param string|array $messages + * + * @return array + */ + public function formatListElement($messages) + { + $messages = array_values((array) $messages); + + $messages[0] = sprintf(' * %s', $messages[0]); + + foreach ($messages as $key => &$message) { + if (0 === $key) { + continue; + } + + $message = sprintf(' %s', $message); + } + + return array_merge($messages, array('')); + } + + /** + * Formats informational or debug text + * + * @param string $message + * + * @return string + */ + public function formatText($message) + { + return sprintf(' // %s', $message); + } + + /** + * Formats a success result bar + * + * @param string|array $messages + * + * @return array + */ + public function formatSuccessResultBar($messages) + { + return $this->formatStyledBlock($messages, 'OK', 'fg=white;bg=green'); + } + + /** + * Formats an error result bar + * + * @param string|array $messages + * + * @return array + */ + public function formatErrorResultBar($messages) + { + return $this->formatStyledBlock($messages, 'ERROR', 'fg=white;bg=red'); + } + + /** + * Formats a note admonition + * + * @param string|array $messages + * + * @return array + */ + public function formatNoteBlock($messages) + { + return $this->formatStyledBlock($messages, 'NOTE', 'fg=white', '! '); + } + + /** + * Formats a caution admonition + * + * @param string|array $messages + * + * @return array + */ + public function formatCautionBlock($messages) + { + return $this->formatStyledBlock($messages, 'CAUTION', 'fg=white;bg=red', '! '); + } + /** * {@inheritdoc} */ @@ -79,4 +197,32 @@ public function getName() { return 'formatter'; } + + /** + * Formats a styled block + * + * @param string|array $messages + * @param string $type + * @param string $style + * @param string $prefix + * + * @return array + */ + protected function formatStyledBlock($messages, $type, $style, $prefix = '') + { + $messages = array_values((array) $messages); + + $messages[0] = sprintf('[%s] %s', $type, $messages[0]); + + $messages = array_map(function ($value) use ($prefix) { + return sprintf('%s%s', $prefix, $value); + }, + $messages + ); + + return array( + $this->formatBlock($messages, $style, false, 120), + '' + ); + } } From b2bd4306658e64210e5e4fae174bc3774a37c85b Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 08:46:25 -0400 Subject: [PATCH 02/34] added FormatterInterface and default formatters --- .../Helper/Formatter/BlockFormatter.php | 63 ++++++++++ .../Helper/Formatter/FormatterInterface.php | 14 +++ .../Helper/Formatter/ListElementFormatter.php | 39 ++++++ .../Helper/Formatter/SectionFormatter.php | 35 ++++++ .../Formatter/SectionTitleFormatter.php | 31 +++++ .../Helper/Formatter/StyledBlockFormatter.php | 52 ++++++++ .../Helper/Formatter/TextFormatter.php | 33 +++++ .../Helper/Formatter/TitleFormatter.php | 34 +++++ .../Console/Helper/FormatterHelper.php | 118 +++++------------- 9 files changed, 332 insertions(+), 87 deletions(-) create mode 100644 src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php create mode 100644 src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php diff --git a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php new file mode 100644 index 0000000000000..fd5bfa98d7ca3 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php @@ -0,0 +1,63 @@ + + */ +class BlockFormatter implements FormatterInterface +{ + protected $messages; + protected $style; + protected $large; + protected $padLength; + + /** + * @param string|array $messages The message to write in the block + * @param string $style The style to apply to the whole block + * @param bool $large Whether to return a large block + * @param int $padLength Length to pad the messages + */ + public function __construct($messages, $style, $large = false, $padLength = 0) + { + $this->messages = $messages; + $this->style = $style; + $this->large = $large; + $this->padLength = $padLength; + } + + /** + * {@inheritdoc} + */ + public function format() + { + $messages = (array) $this->messages; + + $len = 0; + $lines = array(); + foreach ($messages as $message) { + $message = OutputFormatter::escape($message); + $lines[] = sprintf($this->large ? ' %s ' : ' %s ', $message); + $len = max(Helper::strlen($message) + ($this->large ? 4 : 2), $len); + } + + $messages = $this->large ? array(str_repeat(' ', $len)) : array(); + foreach ($lines as $line) { + $messages[] = $line.str_repeat(' ', $len - Helper::strlen($line)); + } + if ($this->large) { + $messages[] = str_repeat(' ', $len); + } + + foreach ($messages as &$message) { + $message = sprintf('<%s>%s', $this->style, str_pad($message, $this->padLength), $this->style); + } + + return implode("\n", $messages); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php new file mode 100644 index 0000000000000..7de41b517f05c --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php @@ -0,0 +1,14 @@ + + */ +interface FormatterInterface +{ + /** + * @return array|string + */ + public function format(); +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php new file mode 100644 index 0000000000000..ad25b2a3878d0 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php @@ -0,0 +1,39 @@ + + */ +class ListElementFormatter implements FormatterInterface +{ + protected $messages; + + /** + * @param string|array $messages + */ + public function __construct($messages) + { + $this->messages = $messages; + } + + /** + * {@inheritdoc} + */ + public function format() + { + $messages = array_values((array) $this->messages); + + $messages[0] = sprintf(' * %s', $messages[0]); + + foreach ($messages as $key => &$message) { + if (0 === $key) { + continue; + } + + $message = sprintf(' %s', $message); + } + + return array_merge($messages, array('')); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php new file mode 100644 index 0000000000000..03e81bc6b5e1b --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php @@ -0,0 +1,35 @@ + + */ +class SectionFormatter implements FormatterInterface +{ + protected $section; + protected $message; + protected $style; + + /** + * Formats a message within a section. + * + * @param string $section + * @param string $message + * @param string $style + */ + public function __construct($section, $message, $style = 'info') + { + $this->section = $section; + $this->message = $message; + $this->style = $style; + } + + /** + * {@inheritdoc} + */ + public function format() + { + return sprintf('<%s>[%s] %s', $this->style, $this->section, $this->style, $this->message); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php new file mode 100644 index 0000000000000..d92a6e27acc50 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php @@ -0,0 +1,31 @@ + + */ +class SectionTitleFormatter implements FormatterInterface +{ + protected $title; + + /** + * @param string $title + */ + public function __construct($title) + { + $this->title = $title; + } + + /** + * {@inheritdoc} + */ + public function format() + { + return array( + sprintf('%s', $this->title), + sprintf('%s', str_repeat('-', strlen($this->title))), + '' + ); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php new file mode 100644 index 0000000000000..a0b84739b6c5f --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php @@ -0,0 +1,52 @@ + + */ +class StyledBlockFormatter extends BlockFormatter +{ + protected $type; + protected $prefix; + + /** + * @param array|string $messages + * @param string $type + * @param bool $style + * @param string $prefix + */ + public function __construct($messages, $type, $style, $prefix = '') + { + $this->type = $type; + $this->prefix = $prefix; + + parent::__construct($messages, $style, false, 120); + } + + /** + * {@inheritdoc} + */ + public function format() + { + $messages = array_values((array) $this->messages); + $prefix = $this->prefix; + + $messages[0] = sprintf('[%s] %s', $this->type, $messages[0]); + + $messages = array_map(function ($value) use ($prefix) { + return sprintf('%s%s', $prefix, $value); + }, + $messages + ); + + $this->messages = $messages; + + return array( + parent::format(), + '' + ); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php new file mode 100644 index 0000000000000..ea276c4888b15 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php @@ -0,0 +1,33 @@ + + */ +class TextFormatter implements FormatterInterface +{ + protected $messages; + + /** + * @param string|array $messages + */ + public function __construct($messages) + { + $this->messages = $messages; + } + + /** + * {@inheritdoc} + */ + public function format() + { + return array_map(function ($value) { + return sprintf(' // %s', $value); + }, + (array) $this->messages + ); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php new file mode 100644 index 0000000000000..95986a779ac26 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php @@ -0,0 +1,34 @@ + + */ +class TitleFormatter implements FormatterInterface +{ + protected $title; + + /** + * @param string $title + */ + public function __construct($title) + { + $this->title = $title; + } + + /** + * {@inheritdoc} + */ + public function format() + { + return array( + '', + sprintf('%s', $this->title), + sprintf('%s', str_repeat('=', strlen($this->title))), + '' + ); + } +} diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index cac3a0ace529b..322fbf29f923c 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -11,7 +11,14 @@ namespace Symfony\Component\Console\Helper; -use Symfony\Component\Console\Formatter\OutputFormatter; +use Symfony\Component\Console\Helper\Formatter\BlockFormatter; +use Symfony\Component\Console\Helper\Formatter\FormatterInterface; +use Symfony\Component\Console\Helper\Formatter\ListElementFormatter; +use Symfony\Component\Console\Helper\Formatter\SectionFormatter; +use Symfony\Component\Console\Helper\Formatter\SectionTitleFormatter; +use Symfony\Component\Console\Helper\Formatter\StyledBlockFormatter; +use Symfony\Component\Console\Helper\Formatter\TextFormatter; +use Symfony\Component\Console\Helper\Formatter\TitleFormatter; /** * The Formatter class provides helpers to format messages. @@ -20,6 +27,16 @@ */ class FormatterHelper extends Helper { + /** + * @param FormatterInterface $formatter + * + * @return array|string + */ + public function format(FormatterInterface $formatter) + { + return $formatter->format(); + } + /** * Formats a message within a section. * @@ -31,7 +48,7 @@ class FormatterHelper extends Helper */ public function formatSection($section, $message, $style = 'info') { - return sprintf('<%s>[%s] %s', $style, $section, $style, $message); + return $this->format(new SectionFormatter($section, $message, $style)); } /** @@ -40,37 +57,13 @@ public function formatSection($section, $message, $style = 'info') * @param string|array $messages The message to write in the block * @param string $style The style to apply to the whole block * @param bool $large Whether to return a large block - * @param int $padLength ength to pad the messages + * @param int $padLength Length to pad the messages * * @return string The formatter message */ public function formatBlock($messages, $style, $large = false, $padLength = 0) { - if (!is_array($messages)) { - $messages = array($messages); - } - - $len = 0; - $lines = array(); - foreach ($messages as $message) { - $message = OutputFormatter::escape($message); - $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); - $len = max($this->strlen($message) + ($large ? 4 : 2), $len); - } - - $messages = $large ? array(str_repeat(' ', $len)) : array(); - for ($i = 0; isset($lines[$i]); ++$i) { - $messages[] = $lines[$i].str_repeat(' ', $len - $this->strlen($lines[$i])); - } - if ($large) { - $messages[] = str_repeat(' ', $len); - } - - for ($i = 0; isset($messages[$i]); ++$i) { - $messages[$i] = sprintf('<%s>%s', $style, str_pad($messages[$i], $padLength), $style); - } - - return implode("\n", $messages); + return $this->format(new BlockFormatter($messages, $style, $large, $padLength)); } /** @@ -82,12 +75,7 @@ public function formatBlock($messages, $style, $large = false, $padLength = 0) */ public function formatTitle($message) { - return array( - '', - sprintf('%s', $message), - sprintf('%s', str_repeat('=', strlen($message))), - '' - ); + return $this->format(new TitleFormatter($message)); } /** @@ -99,11 +87,7 @@ public function formatTitle($message) */ public function formatSectionTitle($message) { - return array( - sprintf('%s', $message), - sprintf('%s', str_repeat('-', strlen($message))), - '' - ); + return $this->format(new SectionTitleFormatter($message)); } /** @@ -115,31 +99,19 @@ public function formatSectionTitle($message) */ public function formatListElement($messages) { - $messages = array_values((array) $messages); - - $messages[0] = sprintf(' * %s', $messages[0]); - - foreach ($messages as $key => &$message) { - if (0 === $key) { - continue; - } - - $message = sprintf(' %s', $message); - } - - return array_merge($messages, array('')); + return $this->format(new ListElementFormatter($messages)); } /** * Formats informational or debug text * - * @param string $message + * @param string|array $messages * * @return string */ - public function formatText($message) + public function formatText($messages) { - return sprintf(' // %s', $message); + return $this->format(new TextFormatter($messages)); } /** @@ -151,7 +123,7 @@ public function formatText($message) */ public function formatSuccessResultBar($messages) { - return $this->formatStyledBlock($messages, 'OK', 'fg=white;bg=green'); + return $this->format(new StyledBlockFormatter($messages, 'OK', 'fg=white;bg=green')); } /** @@ -163,7 +135,7 @@ public function formatSuccessResultBar($messages) */ public function formatErrorResultBar($messages) { - return $this->formatStyledBlock($messages, 'ERROR', 'fg=white;bg=red'); + return $this->format(new StyledBlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); } /** @@ -175,7 +147,7 @@ public function formatErrorResultBar($messages) */ public function formatNoteBlock($messages) { - return $this->formatStyledBlock($messages, 'NOTE', 'fg=white', '! '); + return $this->format(new StyledBlockFormatter($messages, 'NOTE', 'fg=white', '! ')); } /** @@ -187,7 +159,7 @@ public function formatNoteBlock($messages) */ public function formatCautionBlock($messages) { - return $this->formatStyledBlock($messages, 'CAUTION', 'fg=white;bg=red', '! '); + return $this->format(new StyledBlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', '! ')); } /** @@ -197,32 +169,4 @@ public function getName() { return 'formatter'; } - - /** - * Formats a styled block - * - * @param string|array $messages - * @param string $type - * @param string $style - * @param string $prefix - * - * @return array - */ - protected function formatStyledBlock($messages, $type, $style, $prefix = '') - { - $messages = array_values((array) $messages); - - $messages[0] = sprintf('[%s] %s', $type, $messages[0]); - - $messages = array_map(function ($value) use ($prefix) { - return sprintf('%s%s', $prefix, $value); - }, - $messages - ); - - return array( - $this->formatBlock($messages, $style, false, 120), - '' - ); - } } From c71222fc57ef83a0f310346580efa9cdbbf2a954 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 09:33:43 -0400 Subject: [PATCH 03/34] added warning result bar --- .../Component/Console/Helper/FormatterHelper.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index 322fbf29f923c..b2825d54825c6 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -138,6 +138,18 @@ public function formatErrorResultBar($messages) return $this->format(new StyledBlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); } + /** + * Formats an warning result bar + * + * @param string|array $messages + * + * @return array + */ + public function formatWarningResultBar($messages) + { + return $this->format(new StyledBlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); + } + /** * Formats a note admonition * From e20283f8dc2bc46d40bb76e234bfdbeefb8615f5 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 10:24:36 -0400 Subject: [PATCH 04/34] added OutputDecorator --- .../Console/Helper/FormatterHelper.php | 113 --------- .../Console/Output/OutputDecorator.php | 216 ++++++++++++++++++ 2 files changed, 216 insertions(+), 113 deletions(-) create mode 100644 src/Symfony/Component/Console/Output/OutputDecorator.php diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index b2825d54825c6..e8f3784aef124 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -13,12 +13,7 @@ use Symfony\Component\Console\Helper\Formatter\BlockFormatter; use Symfony\Component\Console\Helper\Formatter\FormatterInterface; -use Symfony\Component\Console\Helper\Formatter\ListElementFormatter; use Symfony\Component\Console\Helper\Formatter\SectionFormatter; -use Symfony\Component\Console\Helper\Formatter\SectionTitleFormatter; -use Symfony\Component\Console\Helper\Formatter\StyledBlockFormatter; -use Symfony\Component\Console\Helper\Formatter\TextFormatter; -use Symfony\Component\Console\Helper\Formatter\TitleFormatter; /** * The Formatter class provides helpers to format messages. @@ -66,114 +61,6 @@ public function formatBlock($messages, $style, $large = false, $padLength = 0) return $this->format(new BlockFormatter($messages, $style, $large, $padLength)); } - /** - * Formats a command title - * - * @param string $message - * - * @return array - */ - public function formatTitle($message) - { - return $this->format(new TitleFormatter($message)); - } - - /** - * Formats a section title - * - * @param string $message - * - * @return array - */ - public function formatSectionTitle($message) - { - return $this->format(new SectionTitleFormatter($message)); - } - - /** - * Formats a list element - * - * @param string|array $messages - * - * @return array - */ - public function formatListElement($messages) - { - return $this->format(new ListElementFormatter($messages)); - } - - /** - * Formats informational or debug text - * - * @param string|array $messages - * - * @return string - */ - public function formatText($messages) - { - return $this->format(new TextFormatter($messages)); - } - - /** - * Formats a success result bar - * - * @param string|array $messages - * - * @return array - */ - public function formatSuccessResultBar($messages) - { - return $this->format(new StyledBlockFormatter($messages, 'OK', 'fg=white;bg=green')); - } - - /** - * Formats an error result bar - * - * @param string|array $messages - * - * @return array - */ - public function formatErrorResultBar($messages) - { - return $this->format(new StyledBlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); - } - - /** - * Formats an warning result bar - * - * @param string|array $messages - * - * @return array - */ - public function formatWarningResultBar($messages) - { - return $this->format(new StyledBlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); - } - - /** - * Formats a note admonition - * - * @param string|array $messages - * - * @return array - */ - public function formatNoteBlock($messages) - { - return $this->format(new StyledBlockFormatter($messages, 'NOTE', 'fg=white', '! ')); - } - - /** - * Formats a caution admonition - * - * @param string|array $messages - * - * @return array - */ - public function formatCautionBlock($messages) - { - return $this->format(new StyledBlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', '! ')); - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php new file mode 100644 index 0000000000000..3fb9983d133f3 --- /dev/null +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -0,0 +1,216 @@ + + */ +class OutputDecorator implements OutputInterface +{ + private $output; + + /** + * @param OutputInterface $output + */ + public function __construct(OutputInterface $output) + { + $this->output = $output; + } + + /** + * @param FormatterInterface $formatter + */ + public function format(FormatterInterface $formatter) + { + $this->writeln($formatter->format()); + } + + /** + * Formats a message within a section. + * + * @param string $section The section name + * @param string $message The message + * @param string $style The style to apply to the section + */ + public function section($section, $message, $style = 'info') + { + $this->format(new SectionFormatter($section, $message, $style)); + } + + /** + * Formats a message as a block of text. + * + * @param string|array $messages The message to write in the block + * @param string $style The style to apply to the whole block + * @param bool $large Whether to return a large block + * @param int $padLength Length to pad the messages + */ + public function block($messages, $style, $large = false, $padLength = 0) + { + $this->format(new BlockFormatter($messages, $style, $large, $padLength)); + } + + /** + * Formats a command title + * + * @param string $message + */ + public function title($message) + { + $this->format(new TitleFormatter($message)); + } + + /** + * Formats a section title + * + * @param string $message + */ + public function subtitle($message) + { + $this->format(new SectionTitleFormatter($message)); + } + + /** + * Formats a list element + * + * @param string|array $messages + */ + public function listElement($messages) + { + $this->format(new ListElementFormatter($messages)); + } + + /** + * Formats informational or debug text + * + * @param string|array $messages + */ + public function text($messages) + { + $this->format(new TextFormatter($messages)); + } + + /** + * Formats a success result bar + * + * @param string|array $messages + */ + public function success($messages) + { + $this->format(new StyledBlockFormatter($messages, 'OK', 'fg=white;bg=green')); + } + + /** + * Formats an error result bar + * + * @param string|array $messages + */ + public function error($messages) + { + $this->format(new StyledBlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); + } + + /** + * Formats an warning result bar + * + * @param string|array $messages + */ + public function warning($messages) + { + $this->format(new StyledBlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); + } + + /** + * Formats a note admonition + * + * @param string|array $messages + */ + public function note($messages) + { + $this->format(new StyledBlockFormatter($messages, 'NOTE', 'fg=white', '! ')); + } + + /** + * Formats a caution admonition + * + * @param string|array $messages + */ + public function caution($messages) + { + $this->format(new StyledBlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', '! ')); + } + + /** + * {@inheritdoc} + */ + public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) + { + $this->output->write($messages, $newline, $type); + } + + /** + * {@inheritdoc} + */ + public function writeln($messages, $type = self::OUTPUT_NORMAL) + { + $this->output->writeln($messages, $type); + } + + /** + * {@inheritdoc} + */ + public function setVerbosity($level) + { + $this->output->setVerbosity($level); + } + + /** + * {@inheritdoc} + */ + public function getVerbosity() + { + return $this->output->getVerbosity(); + } + + /** + * {@inheritdoc} + */ + public function setDecorated($decorated) + { + $this->output->setDecorated($decorated); + } + + /** + * {@inheritdoc} + */ + public function isDecorated() + { + return $this->output->isDecorated(); + } + + /** + * {@inheritdoc} + */ + public function setFormatter(OutputFormatterInterface $formatter) + { + $this->output->setFormatter($formatter); + } + + /** + * {@inheritdoc} + */ + public function getFormatter() + { + return $this->output->getFormatter(); + } +} From 797a485e4c8619b6c851888f024908965b17aa91 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 10:35:00 -0400 Subject: [PATCH 05/34] added comments --- .../Console/Helper/Formatter/BlockFormatter.php | 9 +++++++++ .../Console/Helper/Formatter/FormatterInterface.php | 9 +++++++++ .../Console/Helper/Formatter/ListElementFormatter.php | 11 +++++++++++ .../Console/Helper/Formatter/SectionFormatter.php | 9 +++++++++ .../Helper/Formatter/SectionTitleFormatter.php | 9 +++++++++ .../Console/Helper/Formatter/StyledBlockFormatter.php | 9 +++++++++ .../Console/Helper/Formatter/TextFormatter.php | 9 +++++++++ .../Console/Helper/Formatter/TitleFormatter.php | 9 +++++++++ .../Component/Console/Output/OutputDecorator.php | 11 +++++++++++ 9 files changed, 85 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php index fd5bfa98d7ca3..2a8903d52e636 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; use Symfony\Component\Console\Formatter\OutputFormatter; diff --git a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php index 7de41b517f05c..44f2b966747a8 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php +++ b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php index ad25b2a3878d0..83b760c6299f5 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php @@ -1,8 +1,19 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** + * Formats a list element + * * @author Kevin Bond */ class ListElementFormatter implements FormatterInterface diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php index 03e81bc6b5e1b..8cd4dcb86ef6d 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php index d92a6e27acc50..33e36bb2b4be7 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php index a0b84739b6c5f..6cc26599f5fb2 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php index ea276c4888b15..f3672f0208962 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php index 95986a779ac26..2bf91aeda2992 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Helper\Formatter; /** diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php index 3fb9983d133f3..be7c1280d9d05 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Output; use Symfony\Component\Console\Formatter\OutputFormatterInterface; @@ -13,6 +22,8 @@ use Symfony\Component\Console\Helper\Formatter\TitleFormatter; /** + * Decorates output to add console style guide helper methods + * * @author Kevin Bond */ class OutputDecorator implements OutputInterface From c1c49c86ff46a0b03f21ff454cfeb7bf46dcef45 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 10:59:29 -0400 Subject: [PATCH 06/34] adjusted some formats --- ...stElementFormatter.php => ListFormatter.php} | 17 ++++++----------- .../Helper/Formatter/StyledBlockFormatter.php | 15 +++++++++------ .../Console/Output/OutputDecorator.php | 10 +++++----- 3 files changed, 20 insertions(+), 22 deletions(-) rename src/Symfony/Component/Console/Helper/Formatter/{ListElementFormatter.php => ListFormatter.php} (61%) diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php similarity index 61% rename from src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php rename to src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php index 83b760c6299f5..c8501f8b009c9 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/ListElementFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php @@ -16,7 +16,7 @@ * * @author Kevin Bond */ -class ListElementFormatter implements FormatterInterface +class ListFormatter implements FormatterInterface { protected $messages; @@ -33,18 +33,13 @@ public function __construct($messages) */ public function format() { - $messages = array_values((array) $this->messages); + $ret = array(); - $messages[0] = sprintf(' * %s', $messages[0]); - - foreach ($messages as $key => &$message) { - if (0 === $key) { - continue; - } - - $message = sprintf(' %s', $message); + foreach ((array) $this->messages as $message) { + $ret[] = sprintf(' * %s', $message); + $ret[] = ''; } - return array_merge($messages, array('')); + return $ret; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php index 6cc26599f5fb2..49f32216e7676 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php @@ -42,16 +42,19 @@ public function format() { $messages = array_values((array) $this->messages); $prefix = $this->prefix; + $ret = array(); $messages[0] = sprintf('[%s] %s', $this->type, $messages[0]); - $messages = array_map(function ($value) use ($prefix) { - return sprintf('%s%s', $prefix, $value); - }, - $messages - ); + foreach ($messages as $key => &$message) { + $ret[] = sprintf('%s%s', $prefix, $message); + + if (count($messages) > 1 && $key < count($message)) { + $ret[] = $prefix; + } + } - $this->messages = $messages; + $this->messages = $ret; return array( parent::format(), diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php index be7c1280d9d05..88c8f57c9905b 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -14,7 +14,7 @@ use Symfony\Component\Console\Formatter\OutputFormatterInterface; use Symfony\Component\Console\Helper\Formatter\BlockFormatter; use Symfony\Component\Console\Helper\Formatter\FormatterInterface; -use Symfony\Component\Console\Helper\Formatter\ListElementFormatter; +use Symfony\Component\Console\Helper\Formatter\ListFormatter; use Symfony\Component\Console\Helper\Formatter\SectionFormatter; use Symfony\Component\Console\Helper\Formatter\SectionTitleFormatter; use Symfony\Component\Console\Helper\Formatter\StyledBlockFormatter; @@ -92,13 +92,13 @@ public function subtitle($message) } /** - * Formats a list element + * Formats a list * - * @param string|array $messages + * @param array $elements */ - public function listElement($messages) + public function listing(array $elements) { - $this->format(new ListElementFormatter($messages)); + $this->format(new ListFormatter($elements)); } /** From bdb37040596c38cda04b61833941c1f7b9ba3c1a Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 11:21:44 -0400 Subject: [PATCH 07/34] added newline helper --- .../Component/Console/Output/OutputDecorator.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php index 88c8f57c9905b..000b49044ab27 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -161,6 +161,16 @@ public function caution($messages) $this->format(new StyledBlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', '! ')); } + /** + * Add newline(s) + * + * @param int $count The number of newlines + */ + public function ln($count = 1) + { + $this->output->write(str_repeat(PHP_EOL, $count)); + } + /** * {@inheritdoc} */ From 7110316096771ce94aa8242e84c470a65ec2cdd8 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 13:21:52 -0400 Subject: [PATCH 08/34] FormatterInterface::format returns string only --- .../Console/Helper/Formatter/FormatterInterface.php | 2 +- .../Console/Helper/Formatter/ListFormatter.php | 13 ++++++------- .../Helper/Formatter/SectionTitleFormatter.php | 4 ++-- .../Helper/Formatter/StyledBlockFormatter.php | 5 +---- .../Console/Helper/Formatter/TextFormatter.php | 4 ++-- .../Console/Helper/Formatter/TitleFormatter.php | 4 ++-- 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php index 44f2b966747a8..7244223279886 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php +++ b/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php @@ -17,7 +17,7 @@ interface FormatterInterface { /** - * @return array|string + * @return string */ public function format(); } diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php index c8501f8b009c9..9002288df564c 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php @@ -33,13 +33,12 @@ public function __construct($messages) */ public function format() { - $ret = array(); + $messages = array_map(function ($message) { + return sprintf(' * %s', $message); + }, + $this->messages + ); - foreach ((array) $this->messages as $message) { - $ret[] = sprintf(' * %s', $message); - $ret[] = ''; - } - - return $ret; + return implode("\n\n", $messages) . "\n"; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php index 33e36bb2b4be7..6676e17dd75b1 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php @@ -31,10 +31,10 @@ public function __construct($title) */ public function format() { - return array( + return implode("\n", array( sprintf('%s', $this->title), sprintf('%s', str_repeat('-', strlen($this->title))), '' - ); + )); } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php index 49f32216e7676..9d52d0d581d44 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php @@ -56,9 +56,6 @@ public function format() $this->messages = $ret; - return array( - parent::format(), - '' - ); + return parent::format() . "\n"; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php index f3672f0208962..3353b2b0f5610 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php @@ -33,10 +33,10 @@ public function __construct($messages) */ public function format() { - return array_map(function ($value) { + return implode("\n", array_map(function ($value) { return sprintf(' // %s', $value); }, (array) $this->messages - ); + )); } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php index 2bf91aeda2992..2fc7b856e9dca 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php @@ -33,11 +33,11 @@ public function __construct($title) */ public function format() { - return array( + return implode("\n", array( '', sprintf('%s', $this->title), sprintf('%s', str_repeat('=', strlen($this->title))), '' - ); + )); } } From 4b30f14139c6c8b6b66284322ed32c79b8f625ee Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 13:24:06 -0400 Subject: [PATCH 09/34] fix cs --- .../Component/Console/Helper/Formatter/ListFormatter.php | 2 +- .../Console/Helper/Formatter/SectionTitleFormatter.php | 2 +- .../Component/Console/Helper/Formatter/StyledBlockFormatter.php | 2 +- .../Component/Console/Helper/Formatter/TitleFormatter.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php index 9002288df564c..dd03fa7e24228 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php @@ -39,6 +39,6 @@ public function format() $this->messages ); - return implode("\n\n", $messages) . "\n"; + return implode("\n\n", $messages)."\n"; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php index 6676e17dd75b1..dce798e5b459f 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php @@ -34,7 +34,7 @@ public function format() return implode("\n", array( sprintf('%s', $this->title), sprintf('%s', str_repeat('-', strlen($this->title))), - '' + '', )); } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php index 9d52d0d581d44..7095fd6e316c8 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php @@ -56,6 +56,6 @@ public function format() $this->messages = $ret; - return parent::format() . "\n"; + return parent::format()."\n"; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php index 2fc7b856e9dca..2396292954d3f 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php @@ -37,7 +37,7 @@ public function format() '', sprintf('%s', $this->title), sprintf('%s', str_repeat('=', strlen($this->title))), - '' + '', )); } } From a358431738cc21993be71d75541f1be1fc827601 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 25 Sep 2014 15:32:36 -0400 Subject: [PATCH 10/34] fix string padding --- .../Component/Console/Helper/Formatter/BlockFormatter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php index 2a8903d52e636..a21a1d6cbc996 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php @@ -64,7 +64,8 @@ public function format() } foreach ($messages as &$message) { - $message = sprintf('<%s>%s', $this->style, str_pad($message, $this->padLength), $this->style); + $padding = $this->padLength - Helper::strlen($message); + $message = sprintf('<%s>%s', $this->style, $message.str_repeat(' ', $padding > 0 ? $padding : 0), $this->style); } return implode("\n", $messages); From 99ebf0ed9dd095ba365f97ce0e8130df62f2b0eb Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 26 Sep 2014 09:31:27 -0400 Subject: [PATCH 11/34] refactored and simplified --- .../Helper/Formatter/BlockFormatter.php | 56 +++++++++-------- .../Helper/Formatter/SectionFormatter.php | 44 ------------- .../Formatter/SectionTitleFormatter.php | 40 ------------ .../Helper/Formatter/StyledBlockFormatter.php | 61 ------------------- .../Helper/Formatter/TitleFormatter.php | 25 +++++--- .../Console/Helper/FormatterHelper.php | 49 +++++++++------ .../Console/Output/OutputDecorator.php | 39 ++++-------- 7 files changed, 90 insertions(+), 224 deletions(-) delete mode 100644 src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php delete mode 100644 src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php delete mode 100644 src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php diff --git a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php index a21a1d6cbc996..8155cc5b1fa56 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php @@ -21,23 +21,25 @@ */ class BlockFormatter implements FormatterInterface { + const MAX_LENGTH = 120; + protected $messages; + protected $type; protected $style; - protected $large; - protected $padLength; + protected $prefix; /** * @param string|array $messages The message to write in the block - * @param string $style The style to apply to the whole block - * @param bool $large Whether to return a large block - * @param int $padLength Length to pad the messages + * @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 */ - public function __construct($messages, $style, $large = false, $padLength = 0) + public function __construct($messages, $type = null, $style = null, $prefix = ' ') { $this->messages = $messages; + $this->type = $type; $this->style = $style; - $this->large = $large; - $this->padLength = $padLength; + $this->prefix = $prefix; } /** @@ -45,29 +47,33 @@ public function __construct($messages, $style, $large = false, $padLength = 0) */ public function format() { - $messages = (array) $this->messages; - - $len = 0; + $messages = array_values((array) $this->messages); $lines = array(); - foreach ($messages as $message) { - $message = OutputFormatter::escape($message); - $lines[] = sprintf($this->large ? ' %s ' : ' %s ', $message); - $len = max(Helper::strlen($message) + ($this->large ? 4 : 2), $len); - } - $messages = $this->large ? array(str_repeat(' ', $len)) : array(); - foreach ($lines as $line) { - $messages[] = $line.str_repeat(' ', $len - Helper::strlen($line)); + // add type + if (null !== $this->type) { + $messages[0] = sprintf('[%s] %s', $this->type, $messages[0]); } - if ($this->large) { - $messages[] = str_repeat(' ', $len); + + // wrap and add newlines for each element + foreach ($messages as $key => $message) { + $message = OutputFormatter::escape($message); + $lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LENGTH - Helper::strlen($this->prefix)))); + + if (count($messages) > 1 && $key < count($message)) { + $lines[] = ''; + } } - foreach ($messages as &$message) { - $padding = $this->padLength - Helper::strlen($message); - $message = sprintf('<%s>%s', $this->style, $message.str_repeat(' ', $padding > 0 ? $padding : 0), $this->style); + foreach ($lines as &$line) { + $line = sprintf('%s%s', $this->prefix, $line); + $line .= str_repeat(' ', self::MAX_LENGTH - Helper::strlen($line)); + + if ($this->style) { + $line = sprintf('<%s>%s', $this->style, $line, $this->style); + } } - return implode("\n", $messages); + return implode("\n", $lines)."\n"; } } diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php deleted file mode 100644 index 8cd4dcb86ef6d..0000000000000 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionFormatter.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Helper\Formatter; - -/** - * @author Kevin Bond - */ -class SectionFormatter implements FormatterInterface -{ - protected $section; - protected $message; - protected $style; - - /** - * Formats a message within a section. - * - * @param string $section - * @param string $message - * @param string $style - */ - public function __construct($section, $message, $style = 'info') - { - $this->section = $section; - $this->message = $message; - $this->style = $style; - } - - /** - * {@inheritdoc} - */ - public function format() - { - return sprintf('<%s>[%s] %s', $this->style, $this->section, $this->style, $this->message); - } -} diff --git a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php deleted file mode 100644 index dce798e5b459f..0000000000000 --- a/src/Symfony/Component/Console/Helper/Formatter/SectionTitleFormatter.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Helper\Formatter; - -/** - * @author Kevin Bond - */ -class SectionTitleFormatter implements FormatterInterface -{ - protected $title; - - /** - * @param string $title - */ - public function __construct($title) - { - $this->title = $title; - } - - /** - * {@inheritdoc} - */ - public function format() - { - return implode("\n", array( - sprintf('%s', $this->title), - sprintf('%s', str_repeat('-', strlen($this->title))), - '', - )); - } -} diff --git a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php deleted file mode 100644 index 7095fd6e316c8..0000000000000 --- a/src/Symfony/Component/Console/Helper/Formatter/StyledBlockFormatter.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Helper\Formatter; - -/** - * Formats a styled block - * - * @author Kevin Bond - */ -class StyledBlockFormatter extends BlockFormatter -{ - protected $type; - protected $prefix; - - /** - * @param array|string $messages - * @param string $type - * @param bool $style - * @param string $prefix - */ - public function __construct($messages, $type, $style, $prefix = '') - { - $this->type = $type; - $this->prefix = $prefix; - - parent::__construct($messages, $style, false, 120); - } - - /** - * {@inheritdoc} - */ - public function format() - { - $messages = array_values((array) $this->messages); - $prefix = $this->prefix; - $ret = array(); - - $messages[0] = sprintf('[%s] %s', $this->type, $messages[0]); - - foreach ($messages as $key => &$message) { - $ret[] = sprintf('%s%s', $prefix, $message); - - if (count($messages) > 1 && $key < count($message)) { - $ret[] = $prefix; - } - } - - $this->messages = $ret; - - return parent::format()."\n"; - } -} diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php index 2396292954d3f..40d32be2e5689 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php @@ -19,13 +19,19 @@ class TitleFormatter implements FormatterInterface { protected $title; + protected $underlineChar; + protected $newLineBefore; /** * @param string $title + * @param string $underlineChar + * @param bool $newLineBefore */ - public function __construct($title) + public function __construct($title, $underlineChar = '=', $newLineBefore = false) { $this->title = $title; + $this->underlineChar = $underlineChar; + $this->newLineBefore = $newLineBefore; } /** @@ -33,11 +39,16 @@ public function __construct($title) */ public function format() { - return implode("\n", array( - '', - sprintf('%s', $this->title), - sprintf('%s', str_repeat('=', strlen($this->title))), - '', - )); + $ret = array(); + + if ($this->newLineBefore) { + $ret[] = ''; + } + + $ret[] = sprintf('%s', $this->title); + $ret[] = sprintf('%s', str_repeat($this->underlineChar, strlen($this->title))); + $ret[] = ''; + + return implode("\n", $ret); } } diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index e8f3784aef124..b94294345e38d 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -11,9 +11,7 @@ namespace Symfony\Component\Console\Helper; -use Symfony\Component\Console\Helper\Formatter\BlockFormatter; -use Symfony\Component\Console\Helper\Formatter\FormatterInterface; -use Symfony\Component\Console\Helper\Formatter\SectionFormatter; +use Symfony\Component\Console\Formatter\OutputFormatter; /** * The Formatter class provides helpers to format messages. @@ -22,16 +20,6 @@ */ class FormatterHelper extends Helper { - /** - * @param FormatterInterface $formatter - * - * @return array|string - */ - public function format(FormatterInterface $formatter) - { - return $formatter->format(); - } - /** * Formats a message within a section. * @@ -43,22 +31,43 @@ public function format(FormatterInterface $formatter) */ public function formatSection($section, $message, $style = 'info') { - return $this->format(new SectionFormatter($section, $message, $style)); + return sprintf('<%s>[%s] %s', $style, $section, $style, $message); } /** * Formats a message as a block of text. * - * @param string|array $messages The message to write in the block - * @param string $style The style to apply to the whole block - * @param bool $large Whether to return a large block - * @param int $padLength Length to pad the messages + * @param string|array $messages The message to write in the block + * @param string $style The style to apply to the whole block + * @param bool $large Whether to return a large block * * @return string The formatter message */ - public function formatBlock($messages, $style, $large = false, $padLength = 0) + public function formatBlock($messages, $style, $large = false) { - return $this->format(new BlockFormatter($messages, $style, $large, $padLength)); + $messages = (array) $messages; + + $len = 0; + $lines = array(); + foreach ($messages as $message) { + $message = OutputFormatter::escape($message); + $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); + $len = max($this->strlen($message) + ($large ? 4 : 2), $len); + } + + $messages = $large ? array(str_repeat(' ', $len)) : array(); + foreach ($lines as $line) { + $messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); + } + if ($large) { + $messages[] = str_repeat(' ', $len); + } + + foreach ($messages as &$message) { + $message = sprintf('<%s>%s', $style, $message, $style); + } + + return implode("\n", $messages); } /** diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php index 000b49044ab27..21cb0f150f08c 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -15,9 +15,6 @@ use Symfony\Component\Console\Helper\Formatter\BlockFormatter; use Symfony\Component\Console\Helper\Formatter\FormatterInterface; use Symfony\Component\Console\Helper\Formatter\ListFormatter; -use Symfony\Component\Console\Helper\Formatter\SectionFormatter; -use Symfony\Component\Console\Helper\Formatter\SectionTitleFormatter; -use Symfony\Component\Console\Helper\Formatter\StyledBlockFormatter; use Symfony\Component\Console\Helper\Formatter\TextFormatter; use Symfony\Component\Console\Helper\Formatter\TitleFormatter; @@ -46,29 +43,17 @@ public function format(FormatterInterface $formatter) $this->writeln($formatter->format()); } - /** - * Formats a message within a section. - * - * @param string $section The section name - * @param string $message The message - * @param string $style The style to apply to the section - */ - public function section($section, $message, $style = 'info') - { - $this->format(new SectionFormatter($section, $message, $style)); - } - /** * Formats a message as a block of text. * * @param string|array $messages The message to write in the block - * @param string $style The style to apply to the whole block - * @param bool $large Whether to return a large block - * @param int $padLength Length to pad the messages + * @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 */ - public function block($messages, $style, $large = false, $padLength = 0) + public function block($messages, $type = null, $style = null, $prefix = ' ') { - $this->format(new BlockFormatter($messages, $style, $large, $padLength)); + $this->format(new BlockFormatter($messages, $type, $style, $prefix)); } /** @@ -78,7 +63,7 @@ public function block($messages, $style, $large = false, $padLength = 0) */ public function title($message) { - $this->format(new TitleFormatter($message)); + $this->format(new TitleFormatter($message, '=', true)); } /** @@ -88,7 +73,7 @@ public function title($message) */ public function subtitle($message) { - $this->format(new SectionTitleFormatter($message)); + $this->format(new TitleFormatter($message, '-')); } /** @@ -118,7 +103,7 @@ public function text($messages) */ public function success($messages) { - $this->format(new StyledBlockFormatter($messages, 'OK', 'fg=white;bg=green')); + $this->format(new BlockFormatter($messages, 'OK', 'fg=white;bg=green')); } /** @@ -128,7 +113,7 @@ public function success($messages) */ public function error($messages) { - $this->format(new StyledBlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); + $this->format(new BlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); } /** @@ -138,7 +123,7 @@ public function error($messages) */ public function warning($messages) { - $this->format(new StyledBlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); + $this->format(new BlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); } /** @@ -148,7 +133,7 @@ public function warning($messages) */ public function note($messages) { - $this->format(new StyledBlockFormatter($messages, 'NOTE', 'fg=white', '! ')); + $this->format(new BlockFormatter($messages, 'NOTE', null, ' ! ')); } /** @@ -158,7 +143,7 @@ public function note($messages) */ public function caution($messages) { - $this->format(new StyledBlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', '! ')); + $this->format(new BlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', ' ! ')); } /** From 4d3e0780cc6ae7ffa00d9e20c07abbc2bfe1d49a Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 12:36:37 -0400 Subject: [PATCH 12/34] rename subtitle to section, fix docblocks --- .../Component/Console/Helper/Formatter/TextFormatter.php | 2 +- src/Symfony/Component/Console/Output/OutputDecorator.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php index 3353b2b0f5610..0ad2afd789f2a 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php +++ b/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Console\Helper\Formatter; /** - * Formats informational or debug text + * Formats informational text * * @author Kevin Bond */ diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Output/OutputDecorator.php index 21cb0f150f08c..8a7b3510464c0 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Output/OutputDecorator.php @@ -71,7 +71,7 @@ public function title($message) * * @param string $message */ - public function subtitle($message) + public function section($message) { $this->format(new TitleFormatter($message, '-')); } @@ -87,7 +87,7 @@ public function listing(array $elements) } /** - * Formats informational or debug text + * Formats informational text * * @param string|array $messages */ From 72708d7a86fe733eeb63e164cd668156b9e4eaed Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 12:49:12 -0400 Subject: [PATCH 13/34] refactored --- .../Console/Style/AbstractOutputStyle.php | 115 ++++++++++++++++++ .../FormatterInterface.php | 2 +- .../Standard}/BlockFormatter.php | 3 +- .../Standard}/ListFormatter.php | 4 +- .../Standard/StandardOutputStyle.php} | 106 +--------------- .../Standard}/TextFormatter.php | 4 +- .../Standard}/TitleFormatter.php | 4 +- 7 files changed, 131 insertions(+), 107 deletions(-) create mode 100644 src/Symfony/Component/Console/Style/AbstractOutputStyle.php rename src/Symfony/Component/Console/{Helper/Formatter => Style}/FormatterInterface.php (87%) rename src/Symfony/Component/Console/{Helper/Formatter => Style/Standard}/BlockFormatter.php (95%) rename src/Symfony/Component/Console/{Helper/Formatter => Style/Standard}/ListFormatter.php (88%) rename src/Symfony/Component/Console/{Output/OutputDecorator.php => Style/Standard/StandardOutputStyle.php} (54%) rename src/Symfony/Component/Console/{Helper/Formatter => Style/Standard}/TextFormatter.php (88%) rename src/Symfony/Component/Console/{Helper/Formatter => Style/Standard}/TitleFormatter.php (91%) diff --git a/src/Symfony/Component/Console/Style/AbstractOutputStyle.php b/src/Symfony/Component/Console/Style/AbstractOutputStyle.php new file mode 100644 index 0000000000000..5ee2b48e7347b --- /dev/null +++ b/src/Symfony/Component/Console/Style/AbstractOutputStyle.php @@ -0,0 +1,115 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Style; + +use Symfony\Component\Console\Formatter\OutputFormatterInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Decorates output to add console style guide helper methods + * + * @author Kevin Bond + */ +abstract class AbstractOutputStyle implements OutputInterface +{ + private $output; + + /** + * @param OutputInterface $output + */ + public function __construct(OutputInterface $output) + { + $this->output = $output; + } + + /** + * @param FormatterInterface $formatter + */ + public function format(FormatterInterface $formatter) + { + $this->writeln($formatter->format()); + } + + /** + * Add newline(s) + * + * @param int $count The number of newlines + */ + public function ln($count = 1) + { + $this->output->write(str_repeat(PHP_EOL, $count)); + } + + /** + * {@inheritdoc} + */ + public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) + { + $this->output->write($messages, $newline, $type); + } + + /** + * {@inheritdoc} + */ + public function writeln($messages, $type = self::OUTPUT_NORMAL) + { + $this->output->writeln($messages, $type); + } + + /** + * {@inheritdoc} + */ + public function setVerbosity($level) + { + $this->output->setVerbosity($level); + } + + /** + * {@inheritdoc} + */ + public function getVerbosity() + { + return $this->output->getVerbosity(); + } + + /** + * {@inheritdoc} + */ + public function setDecorated($decorated) + { + $this->output->setDecorated($decorated); + } + + /** + * {@inheritdoc} + */ + public function isDecorated() + { + return $this->output->isDecorated(); + } + + /** + * {@inheritdoc} + */ + public function setFormatter(OutputFormatterInterface $formatter) + { + $this->output->setFormatter($formatter); + } + + /** + * {@inheritdoc} + */ + public function getFormatter() + { + return $this->output->getFormatter(); + } +} diff --git a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php b/src/Symfony/Component/Console/Style/FormatterInterface.php similarity index 87% rename from src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php rename to src/Symfony/Component/Console/Style/FormatterInterface.php index 7244223279886..47dcfdc3f39ea 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/FormatterInterface.php +++ b/src/Symfony/Component/Console/Style/FormatterInterface.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Helper\Formatter; +namespace Symfony\Component\Console\Style; /** * @author Kevin Bond diff --git a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php b/src/Symfony/Component/Console/Style/Standard/BlockFormatter.php similarity index 95% rename from src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php rename to src/Symfony/Component/Console/Style/Standard/BlockFormatter.php index 8155cc5b1fa56..d365809b1b17a 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/BlockFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/BlockFormatter.php @@ -9,10 +9,11 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Helper\Formatter; +namespace Symfony\Component\Console\Style\Standard; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Helper\Helper; +use Symfony\Component\Console\Style\FormatterInterface; /** * Formats a message as a block of text. diff --git a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php b/src/Symfony/Component/Console/Style/Standard/ListFormatter.php similarity index 88% rename from src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php rename to src/Symfony/Component/Console/Style/Standard/ListFormatter.php index dd03fa7e24228..f2731f9a5bab0 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/ListFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/ListFormatter.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Helper\Formatter; +namespace Symfony\Component\Console\Style\Standard; + +use Symfony\Component\Console\Style\FormatterInterface; /** * Formats a list element diff --git a/src/Symfony/Component/Console/Output/OutputDecorator.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php similarity index 54% rename from src/Symfony/Component/Console/Output/OutputDecorator.php rename to src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 8a7b3510464c0..8e3b1942f6bd7 100644 --- a/src/Symfony/Component/Console/Output/OutputDecorator.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -9,40 +9,15 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Output; +namespace Symfony\Component\Console\Style\Standard; -use Symfony\Component\Console\Formatter\OutputFormatterInterface; -use Symfony\Component\Console\Helper\Formatter\BlockFormatter; -use Symfony\Component\Console\Helper\Formatter\FormatterInterface; -use Symfony\Component\Console\Helper\Formatter\ListFormatter; -use Symfony\Component\Console\Helper\Formatter\TextFormatter; -use Symfony\Component\Console\Helper\Formatter\TitleFormatter; +use Symfony\Component\Console\Style\AbstractOutputStyle; /** - * Decorates output to add console style guide helper methods - * * @author Kevin Bond */ -class OutputDecorator implements OutputInterface +class StandardOutputStyle extends AbstractOutputStyle { - private $output; - - /** - * @param OutputInterface $output - */ - public function __construct(OutputInterface $output) - { - $this->output = $output; - } - - /** - * @param FormatterInterface $formatter - */ - public function format(FormatterInterface $formatter) - { - $this->writeln($formatter->format()); - } - /** * Formats a message as a block of text. * @@ -145,78 +120,5 @@ public function caution($messages) { $this->format(new BlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', ' ! ')); } - - /** - * Add newline(s) - * - * @param int $count The number of newlines - */ - public function ln($count = 1) - { - $this->output->write(str_repeat(PHP_EOL, $count)); - } - - /** - * {@inheritdoc} - */ - public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) - { - $this->output->write($messages, $newline, $type); - } - - /** - * {@inheritdoc} - */ - public function writeln($messages, $type = self::OUTPUT_NORMAL) - { - $this->output->writeln($messages, $type); - } - - /** - * {@inheritdoc} - */ - public function setVerbosity($level) - { - $this->output->setVerbosity($level); - } - - /** - * {@inheritdoc} - */ - public function getVerbosity() - { - return $this->output->getVerbosity(); - } - - /** - * {@inheritdoc} - */ - public function setDecorated($decorated) - { - $this->output->setDecorated($decorated); - } - - /** - * {@inheritdoc} - */ - public function isDecorated() - { - return $this->output->isDecorated(); - } - - /** - * {@inheritdoc} - */ - public function setFormatter(OutputFormatterInterface $formatter) - { - $this->output->setFormatter($formatter); - } - - /** - * {@inheritdoc} - */ - public function getFormatter() - { - return $this->output->getFormatter(); - } } + diff --git a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php b/src/Symfony/Component/Console/Style/Standard/TextFormatter.php similarity index 88% rename from src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php rename to src/Symfony/Component/Console/Style/Standard/TextFormatter.php index 0ad2afd789f2a..04a48ea641f4b 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TextFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/TextFormatter.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Helper\Formatter; +namespace Symfony\Component\Console\Style\Standard; + +use Symfony\Component\Console\Style\FormatterInterface; /** * Formats informational text diff --git a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php b/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php similarity index 91% rename from src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php rename to src/Symfony/Component/Console/Style/Standard/TitleFormatter.php index 40d32be2e5689..c63926c15c2e0 100644 --- a/src/Symfony/Component/Console/Helper/Formatter/TitleFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Helper\Formatter; +namespace Symfony\Component\Console\Style\Standard; + +use Symfony\Component\Console\Style\FormatterInterface; /** * Formats a command title From a59fe02360c8197d4cc178e00d6669fe49fdeeb8 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 12:59:01 -0400 Subject: [PATCH 14/34] added table helper --- .../Style/Standard/StandardOutputStyle.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 8e3b1942f6bd7..076ebb966ff03 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Style\Standard; +use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Style\AbstractOutputStyle; /** @@ -120,5 +121,21 @@ public function caution($messages) { $this->format(new BlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', ' ! ')); } + + /** + * Formats a table + * + * @param array $headers + * @param array $rows + */ + public function table(array $headers, array $rows) + { + $table = new Table($this); + $table->setHeaders($headers); + $table->setRows($rows); + + $table->render(); + $this->ln(); + } } From bafefd67f04a6c56cecd77f09c4ca0ac63dce418 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 16:08:50 -0400 Subject: [PATCH 15/34] added question helpers --- .../Style/Standard/StandardOutputStyle.php | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 076ebb966ff03..4398041afe16e 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -12,6 +12,9 @@ namespace Symfony\Component\Console\Style\Standard; use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Question\ChoiceQuestion; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\AbstractOutputStyle; /** @@ -137,5 +140,134 @@ public function table(array $headers, array $rows) $table->render(); $this->ln(); } + + /** + * Asks a question + * + * @param Question|string $question + * @param string|null $default + * @param callable|null $validator + * + * @return string + */ + public function ask($question, $default = null, $validator = null) + { + $question = new Question($question, $default); + $question->setValidator($validator); + + return $this->askQuestion($question, $validator); + } + + /** + * Asks for confirmation + * + * @param string $question + * @param bool $default + * + * @return bool + */ + public function confirm($question, $default = true) + { + return $this->askQuestion(new ConfirmationQuestion($question, $default)); + } + + /** + * 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) + { + if (null !== $default) { + $values = array_flip($choices); + $default = $values[$default]; + } + + return $this->askQuestion(new ChoiceQuestion($question, $choices, $default)); + } + + /** + * @param Question $question + * + * @return string + */ + public function askQuestion(Question $question) + { + $text = $question->getQuestion(); + $default = $question->getDefault(); + + switch (true) { + case null === $default: + $text = sprintf(' %s:', $text); + + break; + + case $question instanceof ConfirmationQuestion: + $text = sprintf(' %s (yes/no) [%s]:', $text, $default ? 'yes' : 'no'); + + break; + + case $question instanceof ChoiceQuestion: + $choices = $question->getChoices(); + $text = sprintf(' %s [%s]:', $text, $choices[$default]); + + break; + + default: + $text = sprintf(' %s [%s]:', $text, $default); + } + + $validator = $question->getValidator(); + $question->setValidator(function ($value) use ($validator) { + if (null !== $validator && is_callable($validator)) { + $value = $validator($value); + } + + // make required + if (!is_bool($value) && 0 === strlen($value)) { + throw new \Exception('A value is required.'); + } + + return $value; + }); + + $ret = null; + + while (null === $ret) { + $this->writeln($text); + + if ($question instanceof ChoiceQuestion) { + $width = max(array_map('strlen', array_keys($question->getChoices()))); + + foreach ($question->getChoices() as $key => $value) { + $this->writeln(sprintf(" [%-${width}s] %s", $key, $value)); + } + } + + $this->write(' > '); + + $input = trim(fgets(STDIN, 4096)); + $input = strlen($input) > 0 ? $input : $default; + + try { + $ret = call_user_func($question->getValidator(), $input); + } catch (\Exception $e) { + $this->ln(); + $this->error($e->getMessage()); + } + } + + $this->ln(); + + if ($normalizer = $question->getNormalizer()) { + return $normalizer($ret); + } + + return $ret; + } } From d767d28b985f91a339ddb05b88ea6e34a2005f76 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 16:29:53 -0400 Subject: [PATCH 16/34] added style guide table style --- .../Style/Standard/StandardOutputStyle.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 4398041afe16e..367994483dc22 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Console\Style\Standard; use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableStyle; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; @@ -22,6 +24,24 @@ */ class StandardOutputStyle extends AbstractOutputStyle { + /** + * {@inheritdoc} + */ + public function __construct(OutputInterface $output) + { + $styleGuideTableStyle = new TableStyle(); + $styleGuideTableStyle + ->setHorizontalBorderChar('-') + ->setVerticalBorderChar(' ') + ->setCrossingChar(' ') + ->setCellHeaderFormat('%s') + ; + + Table::setStyleDefinition('symfony-style-guide', $styleGuideTableStyle); + + parent::__construct($output); + } + /** * Formats a message as a block of text. * @@ -136,6 +156,7 @@ public function table(array $headers, array $rows) $table = new Table($this); $table->setHeaders($headers); $table->setRows($rows); + $table->setStyle('symfony-style-guide'); $table->render(); $this->ln(); From 273c187ad95c88195949ec8c8d482c79684d8fcd Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 17:36:49 -0400 Subject: [PATCH 17/34] added StandardQuestionHelper --- .../Console/Helper/QuestionHelper.php | 65 +++++++---- .../Style/Standard/StandardOutputStyle.php | 80 ++------------ .../Style/Standard/StandardQuestionHelper.php | 104 ++++++++++++++++++ 3 files changed, 156 insertions(+), 93 deletions(-) create mode 100644 src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index caa091492c540..f0d5fd8548f34 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -109,25 +109,11 @@ public function getName() */ public function doAsk(OutputInterface $output, Question $question) { - $inputStream = $this->inputStream ?: STDIN; - - $message = $question->getQuestion(); - if ($question instanceof ChoiceQuestion) { - $width = max(array_map('strlen', array_keys($question->getChoices()))); - - $messages = (array) $question->getQuestion(); - foreach ($question->getChoices() as $key => $value) { - $messages[] = sprintf(" [%-${width}s] %s", $key, $value); - } - - $output->writeln($messages); - - $message = $question->getPrompt(); - } - - $output->write($message); + $this->writePrompt($output, $question); + $inputStream = $this->inputStream ?: STDIN; $autocomplete = $question->getAutocompleterValues(); + if (null === $autocomplete || !$this->hasSttyAvailable()) { $ret = false; if ($question->isHidden()) { @@ -160,6 +146,43 @@ public function doAsk(OutputInterface $output, Question $question) return $ret; } + /** + * Outputs the question prompt. + * + * @param OutputInterface $output + * @param Question $question + */ + protected function writePrompt(OutputInterface $output, Question $question) + { + $message = $question->getQuestion(); + + if ($question instanceof ChoiceQuestion) { + $width = max(array_map('strlen', array_keys($question->getChoices()))); + + $messages = (array) $question->getQuestion(); + foreach ($question->getChoices() as $key => $value) { + $messages[] = sprintf(" [%-${width}s] %s", $key, $value); + } + + $output->writeln($messages); + + $message = $question->getPrompt(); + } + + $output->write($message); + } + + /** + * Outputs an error message. + * + * @param OutputInterface $output + * @param \Exception $error + */ + protected function writeError(OutputInterface $output, \Exception $error) + { + $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); + } + /** * Autocompletes a question. * @@ -355,13 +378,7 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio $attempts = $question->getMaxAttempts(); while (null === $attempts || $attempts--) { if (null !== $error) { - if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { - $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); - } else { - $message = ''.$error->getMessage().''; - } - - $output->writeln($message); + $this->writeError($output, $error); } try { diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 367994483dc22..bf64536146fb4 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableStyle; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; @@ -24,10 +25,14 @@ */ class StandardOutputStyle extends AbstractOutputStyle { + private $input; + private $questionHelper; + /** - * {@inheritdoc} + * @param InputInterface $input + * @param OutputInterface $output */ - public function __construct(OutputInterface $output) + public function __construct(InputInterface $input, OutputInterface $output) { $styleGuideTableStyle = new TableStyle(); $styleGuideTableStyle @@ -39,6 +44,9 @@ public function __construct(OutputInterface $output) Table::setStyleDefinition('symfony-style-guide', $styleGuideTableStyle); + $this->input = $input; + $this->questionHelper = new StandardQuestionHelper(); + parent::__construct($output); } @@ -218,76 +226,10 @@ public function choice($question, array $choices, $default = null) */ public function askQuestion(Question $question) { - $text = $question->getQuestion(); - $default = $question->getDefault(); - - switch (true) { - case null === $default: - $text = sprintf(' %s:', $text); - - break; - - case $question instanceof ConfirmationQuestion: - $text = sprintf(' %s (yes/no) [%s]:', $text, $default ? 'yes' : 'no'); - - break; - - case $question instanceof ChoiceQuestion: - $choices = $question->getChoices(); - $text = sprintf(' %s [%s]:', $text, $choices[$default]); - - break; - - default: - $text = sprintf(' %s [%s]:', $text, $default); - } - - $validator = $question->getValidator(); - $question->setValidator(function ($value) use ($validator) { - if (null !== $validator && is_callable($validator)) { - $value = $validator($value); - } - - // make required - if (!is_bool($value) && 0 === strlen($value)) { - throw new \Exception('A value is required.'); - } - - return $value; - }); - - $ret = null; - - while (null === $ret) { - $this->writeln($text); - - if ($question instanceof ChoiceQuestion) { - $width = max(array_map('strlen', array_keys($question->getChoices()))); - - foreach ($question->getChoices() as $key => $value) { - $this->writeln(sprintf(" [%-${width}s] %s", $key, $value)); - } - } - - $this->write(' > '); - - $input = trim(fgets(STDIN, 4096)); - $input = strlen($input) > 0 ? $input : $default; - - try { - $ret = call_user_func($question->getValidator(), $input); - } catch (\Exception $e) { - $this->ln(); - $this->error($e->getMessage()); - } - } + $ret = $this->questionHelper->ask($this->input, $this, $question); $this->ln(); - if ($normalizer = $question->getNormalizer()) { - return $normalizer($ret); - } - return $ret; } } diff --git a/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php b/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php new file mode 100644 index 0000000000000..2dbfd605c5959 --- /dev/null +++ b/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php @@ -0,0 +1,104 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Style\Standard; + +use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ChoiceQuestion; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; + +/** + * @author Kevin Bond + */ +class StandardQuestionHelper extends QuestionHelper +{ + /** + * {@inheritdoc} + */ + public function ask(InputInterface $input, OutputInterface $output, Question $question) + { + $validator = $question->getValidator(); + $question->setValidator(function ($value) use ($validator) { + if (null !== $validator && is_callable($validator)) { + $value = $validator($value); + } + + // make required + if (!is_bool($value) && 0 === strlen($value)) { + throw new \Exception('A value is required.'); + } + + return $value; + }); + + return parent::ask($input, $output, $question); + } + + /** + * {@inheritdoc} + */ + protected function writePrompt(OutputInterface $output, Question $question) + { + $text = $question->getQuestion(); + $default = $question->getDefault(); + + switch (true) { + case null === $default: + $text = sprintf(' %s:', $text); + + break; + + case $question instanceof ConfirmationQuestion: + $text = sprintf(' %s (yes/no) [%s]:', $text, $default ? 'yes' : 'no'); + + break; + + case $question instanceof ChoiceQuestion: + $choices = $question->getChoices(); + $text = sprintf(' %s [%s]:', $text, $choices[$default]); + + break; + + default: + $text = sprintf(' %s [%s]:', $text, $default); + } + + $output->writeln($text); + + if ($question instanceof ChoiceQuestion) { + $width = max(array_map('strlen', array_keys($question->getChoices()))); + + foreach ($question->getChoices() as $key => $value) { + $output->writeln(sprintf(" [%-${width}s] %s", $key, $value)); + } + } + + $output->write(' > '); + } + + /** + * {@inheritdoc} + */ + protected function writeError(OutputInterface $output, \Exception $error) + { + if ($output instanceof StandardOutputStyle) { + $output->ln(); + $output->error($error->getMessage()); + + return; + } + + parent::writeError($output, $error); + } +} From b0d2e6ab59f1aab6179b068c9065d5c558ccfe87 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 17:39:34 -0400 Subject: [PATCH 18/34] added table style to Table defaults --- src/Symfony/Component/Console/Helper/Table.php | 9 +++++++++ .../Console/Style/Standard/StandardOutputStyle.php | 11 ----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 67cfbb503d06b..0e9612a2f2958 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -401,10 +401,19 @@ private static function initStyles() ->setCellRowContentFormat('%s') ; + $styleGuide = new TableStyle(); + $styleGuide + ->setHorizontalBorderChar('-') + ->setVerticalBorderChar(' ') + ->setCrossingChar(' ') + ->setCellHeaderFormat('%s') + ; + return array( 'default' => new TableStyle(), 'borderless' => $borderless, 'compact' => $compact, + 'symfony-style-guide' => $styleGuide, ); } } diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index bf64536146fb4..e1ce1fac38878 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Console\Style\Standard; use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableStyle; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; @@ -34,16 +33,6 @@ class StandardOutputStyle extends AbstractOutputStyle */ public function __construct(InputInterface $input, OutputInterface $output) { - $styleGuideTableStyle = new TableStyle(); - $styleGuideTableStyle - ->setHorizontalBorderChar('-') - ->setVerticalBorderChar(' ') - ->setCrossingChar(' ') - ->setCellHeaderFormat('%s') - ; - - Table::setStyleDefinition('symfony-style-guide', $styleGuideTableStyle); - $this->input = $input; $this->questionHelper = new StandardQuestionHelper(); From 9d82d6affb67ef62753cc3f0dc8335e9502b08ef Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 17:47:42 -0400 Subject: [PATCH 19/34] added `askHidden` helper --- .../Style/Standard/StandardOutputStyle.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index e1ce1fac38878..3f030cc500d10 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -162,7 +162,7 @@ public function table(array $headers, array $rows) /** * Asks a question * - * @param Question|string $question + * @param string $question * @param string|null $default * @param callable|null $validator * @@ -176,6 +176,22 @@ public function ask($question, $default = null, $validator = null) return $this->askQuestion($question, $validator); } + /** + * Asks a question with the user input hidden + * + * @param string $question + * @param callable|null $validator + * + * @return string + */ + public function askHidden($question, $validator = null) + { + $question = new Question($question); + $question->setHidden(true); + + return $this->askQuestion($question, $validator); + } + /** * Asks for confirmation * From 525be2b1d3464c172a8574d6c7cf970c328496c9 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 2 Oct 2014 18:17:57 -0400 Subject: [PATCH 20/34] fix cs --- .../Console/Style/AbstractOutputStyle.php | 2 +- .../Console/Style/Standard/ListFormatter.php | 2 +- .../Style/Standard/StandardOutputStyle.php | 48 +++++++++++++------ .../Style/Standard/StandardQuestionHelper.php | 4 +- .../Console/Style/Standard/TextFormatter.php | 2 +- .../Console/Style/Standard/TitleFormatter.php | 2 +- 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Component/Console/Style/AbstractOutputStyle.php b/src/Symfony/Component/Console/Style/AbstractOutputStyle.php index 5ee2b48e7347b..fe9a73e387389 100644 --- a/src/Symfony/Component/Console/Style/AbstractOutputStyle.php +++ b/src/Symfony/Component/Console/Style/AbstractOutputStyle.php @@ -15,7 +15,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Decorates output to add console style guide helper methods + * Decorates output to add console style guide helpers * * @author Kevin Bond */ diff --git a/src/Symfony/Component/Console/Style/Standard/ListFormatter.php b/src/Symfony/Component/Console/Style/Standard/ListFormatter.php index f2731f9a5bab0..9a1f25241658c 100644 --- a/src/Symfony/Component/Console/Style/Standard/ListFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/ListFormatter.php @@ -14,7 +14,7 @@ use Symfony\Component\Console\Style\FormatterInterface; /** - * Formats a list element + * Formats a list element. * * @author Kevin Bond */ diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php index 3f030cc500d10..42e77d592db83 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php @@ -20,6 +20,8 @@ use Symfony\Component\Console\Style\AbstractOutputStyle; /** + * Output decorator helpers for the Symfony Style Guide. + * * @author Kevin Bond */ class StandardOutputStyle extends AbstractOutputStyle @@ -53,7 +55,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ') } /** - * Formats a command title + * Formats a command title. * * @param string $message */ @@ -63,7 +65,7 @@ public function title($message) } /** - * Formats a section title + * Formats a section title. * * @param string $message */ @@ -73,7 +75,7 @@ public function section($message) } /** - * Formats a list + * Formats a list. * * @param array $elements */ @@ -83,7 +85,7 @@ public function listing(array $elements) } /** - * Formats informational text + * Formats informational text. * * @param string|array $messages */ @@ -93,7 +95,7 @@ public function text($messages) } /** - * Formats a success result bar + * Formats a success result bar. * * @param string|array $messages */ @@ -103,7 +105,7 @@ public function success($messages) } /** - * Formats an error result bar + * Formats an error result bar. * * @param string|array $messages */ @@ -113,7 +115,7 @@ public function error($messages) } /** - * Formats an warning result bar + * Formats an warning result bar. * * @param string|array $messages */ @@ -123,7 +125,7 @@ public function warning($messages) } /** - * Formats a note admonition + * Formats a note admonition. * * @param string|array $messages */ @@ -133,7 +135,7 @@ public function note($messages) } /** - * Formats a caution admonition + * Formats a caution admonition. * * @param string|array $messages */ @@ -143,7 +145,7 @@ public function caution($messages) } /** - * Formats a table + * Formats a table. * * @param array $headers * @param array $rows @@ -160,7 +162,7 @@ public function table(array $headers, array $rows) } /** - * Asks a question + * Asks a question. * * @param string $question * @param string|null $default @@ -177,7 +179,7 @@ public function ask($question, $default = null, $validator = null) } /** - * Asks a question with the user input hidden + * Asks a question with the user input hidden. * * @param string $question * @param callable|null $validator @@ -193,7 +195,7 @@ public function askHidden($question, $validator = null) } /** - * Asks for confirmation + * Asks for confirmation. * * @param string $question * @param bool $default @@ -206,7 +208,7 @@ public function confirm($question, $default = true) } /** - * Asks a choice question + * Asks a choice question. * * @param string $question * @param array $choices @@ -224,6 +226,23 @@ public function choice($question, array $choices, $default = null) return $this->askQuestion(new ChoiceQuestion($question, $choices, $default)); } + /** + * Asks a choice question. + * + * @param string $question + * @param array $choices + * @param array $default + * + * @return string + */ + public function multipleChoice($question, array $choices, array $default = array()) + { + $question = new ChoiceQuestion($question, $choices, $default); + $question->setMultiselect(true); + + return $this->askQuestion($question); + } + /** * @param Question $question * @@ -238,4 +257,3 @@ public function askQuestion(Question $question) return $ret; } } - diff --git a/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php b/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php index 2dbfd605c5959..3ddf9c5fe8c5e 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php +++ b/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php @@ -19,6 +19,8 @@ use Symfony\Component\Console\Question\Question; /** + * Symfony Style Guide compliant question helper. + * * @author Kevin Bond */ class StandardQuestionHelper extends QuestionHelper @@ -35,7 +37,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu } // make required - if (!is_bool($value) && 0 === strlen($value)) { + if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) { throw new \Exception('A value is required.'); } diff --git a/src/Symfony/Component/Console/Style/Standard/TextFormatter.php b/src/Symfony/Component/Console/Style/Standard/TextFormatter.php index 04a48ea641f4b..782176476b521 100644 --- a/src/Symfony/Component/Console/Style/Standard/TextFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/TextFormatter.php @@ -14,7 +14,7 @@ use Symfony\Component\Console\Style\FormatterInterface; /** - * Formats informational text + * Formats informational text. * * @author Kevin Bond */ diff --git a/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php b/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php index c63926c15c2e0..fa2ea69e3cec2 100644 --- a/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php +++ b/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php @@ -14,7 +14,7 @@ use Symfony\Component\Console\Style\FormatterInterface; /** - * Formats a command title + * Formats a command title. * * @author Kevin Bond */ From 16ad7b99cbf034f858b03b6dca248702e3e4357f Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 3 Oct 2014 09:46:24 -0400 Subject: [PATCH 21/34] refactored, removed FormatterInterface --- .../SymfonyQuestionHelper.php} | 8 +- .../Console/Style/FormatterInterface.php | 23 --- ...bstractOutputStyle.php => OutputStyle.php} | 14 +- .../Console/Style/Standard/BlockFormatter.php | 80 --------- .../Console/Style/Standard/ListFormatter.php | 46 ----- .../Console/Style/Standard/TextFormatter.php | 44 ----- .../Console/Style/Standard/TitleFormatter.php | 56 ------ .../Console/Style/StyleInterface.php | 151 ++++++++++++++++ ...andardOutputStyle.php => SymfonyStyle.php} | 167 +++++++++--------- 9 files changed, 240 insertions(+), 349 deletions(-) rename src/Symfony/Component/Console/{Style/Standard/StandardQuestionHelper.php => Helper/SymfonyQuestionHelper.php} (93%) delete mode 100644 src/Symfony/Component/Console/Style/FormatterInterface.php rename src/Symfony/Component/Console/Style/{AbstractOutputStyle.php => OutputStyle.php} (86%) delete mode 100644 src/Symfony/Component/Console/Style/Standard/BlockFormatter.php delete mode 100644 src/Symfony/Component/Console/Style/Standard/ListFormatter.php delete mode 100644 src/Symfony/Component/Console/Style/Standard/TextFormatter.php delete mode 100644 src/Symfony/Component/Console/Style/Standard/TitleFormatter.php create mode 100644 src/Symfony/Component/Console/Style/StyleInterface.php rename src/Symfony/Component/Console/Style/{Standard/StandardOutputStyle.php => SymfonyStyle.php} (55%) diff --git a/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php similarity index 93% rename from src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php rename to src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php index 3ddf9c5fe8c5e..002b29b5cf7a3 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardQuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php @@ -9,21 +9,21 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Style\Standard; +namespace Symfony\Component\Console\Helper; -use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Symfony Style Guide compliant question helper. * * @author Kevin Bond */ -class StandardQuestionHelper extends QuestionHelper +class SymfonyQuestionHelper extends QuestionHelper { /** * {@inheritdoc} @@ -94,7 +94,7 @@ protected function writePrompt(OutputInterface $output, Question $question) */ protected function writeError(OutputInterface $output, \Exception $error) { - if ($output instanceof StandardOutputStyle) { + if ($output instanceof SymfonyStyle) { $output->ln(); $output->error($error->getMessage()); diff --git a/src/Symfony/Component/Console/Style/FormatterInterface.php b/src/Symfony/Component/Console/Style/FormatterInterface.php deleted file mode 100644 index 47dcfdc3f39ea..0000000000000 --- a/src/Symfony/Component/Console/Style/FormatterInterface.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Style; - -/** - * @author Kevin Bond - */ -interface FormatterInterface -{ - /** - * @return string - */ - public function format(); -} diff --git a/src/Symfony/Component/Console/Style/AbstractOutputStyle.php b/src/Symfony/Component/Console/Style/OutputStyle.php similarity index 86% rename from src/Symfony/Component/Console/Style/AbstractOutputStyle.php rename to src/Symfony/Component/Console/Style/OutputStyle.php index fe9a73e387389..d37f7eef85180 100644 --- a/src/Symfony/Component/Console/Style/AbstractOutputStyle.php +++ b/src/Symfony/Component/Console/Style/OutputStyle.php @@ -19,7 +19,7 @@ * * @author Kevin Bond */ -abstract class AbstractOutputStyle implements OutputInterface +abstract class OutputStyle implements OutputInterface, StyleInterface { private $output; @@ -32,17 +32,7 @@ public function __construct(OutputInterface $output) } /** - * @param FormatterInterface $formatter - */ - public function format(FormatterInterface $formatter) - { - $this->writeln($formatter->format()); - } - - /** - * Add newline(s) - * - * @param int $count The number of newlines + * {@inheritdoc} */ public function ln($count = 1) { diff --git a/src/Symfony/Component/Console/Style/Standard/BlockFormatter.php b/src/Symfony/Component/Console/Style/Standard/BlockFormatter.php deleted file mode 100644 index d365809b1b17a..0000000000000 --- a/src/Symfony/Component/Console/Style/Standard/BlockFormatter.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Style\Standard; - -use Symfony\Component\Console\Formatter\OutputFormatter; -use Symfony\Component\Console\Helper\Helper; -use Symfony\Component\Console\Style\FormatterInterface; - -/** - * Formats a message as a block of text. - * - * @author Kevin Bond - */ -class BlockFormatter implements FormatterInterface -{ - const MAX_LENGTH = 120; - - protected $messages; - protected $type; - protected $style; - protected $prefix; - - /** - * @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 - */ - public function __construct($messages, $type = null, $style = null, $prefix = ' ') - { - $this->messages = $messages; - $this->type = $type; - $this->style = $style; - $this->prefix = $prefix; - } - - /** - * {@inheritdoc} - */ - public function format() - { - $messages = array_values((array) $this->messages); - $lines = array(); - - // add type - if (null !== $this->type) { - $messages[0] = sprintf('[%s] %s', $this->type, $messages[0]); - } - - // wrap and add newlines for each element - foreach ($messages as $key => $message) { - $message = OutputFormatter::escape($message); - $lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LENGTH - Helper::strlen($this->prefix)))); - - if (count($messages) > 1 && $key < count($message)) { - $lines[] = ''; - } - } - - foreach ($lines as &$line) { - $line = sprintf('%s%s', $this->prefix, $line); - $line .= str_repeat(' ', self::MAX_LENGTH - Helper::strlen($line)); - - if ($this->style) { - $line = sprintf('<%s>%s', $this->style, $line, $this->style); - } - } - - return implode("\n", $lines)."\n"; - } -} diff --git a/src/Symfony/Component/Console/Style/Standard/ListFormatter.php b/src/Symfony/Component/Console/Style/Standard/ListFormatter.php deleted file mode 100644 index 9a1f25241658c..0000000000000 --- a/src/Symfony/Component/Console/Style/Standard/ListFormatter.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Style\Standard; - -use Symfony\Component\Console\Style\FormatterInterface; - -/** - * Formats a list element. - * - * @author Kevin Bond - */ -class ListFormatter implements FormatterInterface -{ - protected $messages; - - /** - * @param string|array $messages - */ - public function __construct($messages) - { - $this->messages = $messages; - } - - /** - * {@inheritdoc} - */ - public function format() - { - $messages = array_map(function ($message) { - return sprintf(' * %s', $message); - }, - $this->messages - ); - - return implode("\n\n", $messages)."\n"; - } -} diff --git a/src/Symfony/Component/Console/Style/Standard/TextFormatter.php b/src/Symfony/Component/Console/Style/Standard/TextFormatter.php deleted file mode 100644 index 782176476b521..0000000000000 --- a/src/Symfony/Component/Console/Style/Standard/TextFormatter.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Style\Standard; - -use Symfony\Component\Console\Style\FormatterInterface; - -/** - * Formats informational text. - * - * @author Kevin Bond - */ -class TextFormatter implements FormatterInterface -{ - protected $messages; - - /** - * @param string|array $messages - */ - public function __construct($messages) - { - $this->messages = $messages; - } - - /** - * {@inheritdoc} - */ - public function format() - { - return implode("\n", array_map(function ($value) { - return sprintf(' // %s', $value); - }, - (array) $this->messages - )); - } -} diff --git a/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php b/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php deleted file mode 100644 index fa2ea69e3cec2..0000000000000 --- a/src/Symfony/Component/Console/Style/Standard/TitleFormatter.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Style\Standard; - -use Symfony\Component\Console\Style\FormatterInterface; - -/** - * Formats a command title. - * - * @author Kevin Bond - */ -class TitleFormatter implements FormatterInterface -{ - protected $title; - protected $underlineChar; - protected $newLineBefore; - - /** - * @param string $title - * @param string $underlineChar - * @param bool $newLineBefore - */ - public function __construct($title, $underlineChar = '=', $newLineBefore = false) - { - $this->title = $title; - $this->underlineChar = $underlineChar; - $this->newLineBefore = $newLineBefore; - } - - /** - * {@inheritdoc} - */ - public function format() - { - $ret = array(); - - if ($this->newLineBefore) { - $ret[] = ''; - } - - $ret[] = sprintf('%s', $this->title); - $ret[] = sprintf('%s', str_repeat($this->underlineChar, strlen($this->title))); - $ret[] = ''; - - return implode("\n", $ret); - } -} diff --git a/src/Symfony/Component/Console/Style/StyleInterface.php b/src/Symfony/Component/Console/Style/StyleInterface.php new file mode 100644 index 0000000000000..b589096fb4f0b --- /dev/null +++ b/src/Symfony/Component/Console/Style/StyleInterface.php @@ -0,0 +1,151 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Style; + +/** + * Output style helpers + * + * @author Kevin Bond + */ +interface StyleInterface +{ + /** + * Formats a command title. + * + * @param string $message + */ + public function title($message); + + /** + * Formats a section title. + * + * @param string $message + */ + public function section($message); + + /** + * Formats a list. + * + * @param array $elements + */ + public function listing(array $elements); + + /** + * Formats informational text. + * + * @param string|array $message + */ + public function text($message); + + /** + * Formats a success result bar. + * + * @param string|array $message + */ + public function success($message); + + /** + * Formats an error result bar. + * + * @param string|array $message + */ + public function error($message); + + /** + * Formats an warning result bar. + * + * @param string|array $message + */ + public function warning($message); + + /** + * Formats a note admonition. + * + * @param string|array $message + */ + public function note($message); + + /** + * Formats a caution admonition. + * + * @param string|array $message + */ + public function caution($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); + + /** + * Asks a question with the user input hidden. + * + * @param string $question + * @param callable|null $validator + * + * @return string + */ + public function askHidden($question, $validator = null); + + /** + * Asks for confirmation. + * + * @param string $question + * @param bool $default + * + * @return bool + */ + public function confirm($question, $default = true); + + /** + * 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); + + /** + * Asks a multiple choice question. + * + * @param string $question + * @param array $choices + * @param array $default + * + * @return string + */ + public function multipleChoice($question, array $choices, array $default = array()); + + /** + * Add newline(s) + * + * @param int $count The number of newlines + */ + public function ln($count = 1); +} diff --git a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php similarity index 55% rename from src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php rename to src/Symfony/Component/Console/Style/SymfonyStyle.php index 42e77d592db83..1cc89ca39399b 100644 --- a/src/Symfony/Component/Console/Style/Standard/StandardOutputStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -9,23 +9,27 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Console\Style\Standard; +namespace Symfony\Component\Console\Style; +use Symfony\Component\Console\Formatter\OutputFormatter; +use Symfony\Component\Console\Helper\Helper; +use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; -use Symfony\Component\Console\Style\AbstractOutputStyle; /** * Output decorator helpers for the Symfony Style Guide. * * @author Kevin Bond */ -class StandardOutputStyle extends AbstractOutputStyle +class SymfonyStyle extends OutputStyle { + const MAX_LENGTH = 120; + private $input; private $questionHelper; @@ -36,7 +40,6 @@ class StandardOutputStyle extends AbstractOutputStyle public function __construct(InputInterface $input, OutputInterface $output) { $this->input = $input; - $this->questionHelper = new StandardQuestionHelper(); parent::__construct($output); } @@ -51,104 +54,124 @@ public function __construct(InputInterface $input, OutputInterface $output) */ public function block($messages, $type = null, $style = null, $prefix = ' ') { - $this->format(new BlockFormatter($messages, $type, $style, $prefix)); + $messages = array_values((array) $messages); + $lines = array(); + + // add type + if (null !== $type) { + $messages[0] = sprintf('[%s] %s', $type, $messages[0]); + } + + // wrap and add newlines for each element + foreach ($messages as $key => $message) { + $message = OutputFormatter::escape($message); + $lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LENGTH - Helper::strlen($prefix)))); + + if (count($messages) > 1 && $key < count($message)) { + $lines[] = ''; + } + } + + foreach ($lines as &$line) { + $line = sprintf('%s%s', $prefix, $line); + $line .= str_repeat(' ', self::MAX_LENGTH - Helper::strlen($line)); + + if ($style) { + $line = sprintf('<%s>%s', $style, $line, $style); + } + } + + $this->writeln(implode("\n", $lines)."\n"); } /** - * Formats a command title. - * - * @param string $message + * {@inheritdoc} */ public function title($message) { - $this->format(new TitleFormatter($message, '=', true)); + $this->writeln(sprintf("\n%s\n%s\n", $message, str_repeat('=', strlen($message)))); } /** - * Formats a section title. - * - * @param string $message + * {@inheritdoc} */ public function section($message) { - $this->format(new TitleFormatter($message, '-')); + $this->writeln(sprintf("%s\n%s\n", $message, str_repeat('-', strlen($message)))); } /** - * Formats a list. - * - * @param array $elements + * {@inheritdoc} */ public function listing(array $elements) { - $this->format(new ListFormatter($elements)); + $elements = array_map(function ($element) { + return sprintf(' * %s', $element); + }, + $elements + ); + + $this->writeln(implode("\n\n", $elements)."\n"); } /** - * Formats informational text. - * - * @param string|array $messages + * {@inheritdoc} */ - public function text($messages) + public function text($message) { - $this->format(new TextFormatter($messages)); + if (!is_array($message)) { + $this->writeln(sprintf(' // %s', $message)); + + return; + } + + foreach ($message as $element) { + $this->text($element); + } } /** - * Formats a success result bar. - * - * @param string|array $messages + * {@inheritdoc} */ - public function success($messages) + public function success($message) { - $this->format(new BlockFormatter($messages, 'OK', 'fg=white;bg=green')); + $this->block($message, 'OK', 'fg=white;bg=green'); } /** - * Formats an error result bar. - * - * @param string|array $messages + * {@inheritdoc} */ - public function error($messages) + public function error($message) { - $this->format(new BlockFormatter($messages, 'ERROR', 'fg=white;bg=red')); + $this->block($message, 'ERROR', 'fg=white;bg=red'); } /** - * Formats an warning result bar. - * - * @param string|array $messages + * {@inheritdoc} */ - public function warning($messages) + public function warning($message) { - $this->format(new BlockFormatter($messages, 'WARNING', 'fg=black;bg=yellow')); + $this->block($message, 'WARNING', 'fg=black;bg=yellow'); } /** - * Formats a note admonition. - * - * @param string|array $messages + * {@inheritdoc} */ - public function note($messages) + public function note($message) { - $this->format(new BlockFormatter($messages, 'NOTE', null, ' ! ')); + $this->block($message, 'NOTE', null, ' ! '); } /** - * Formats a caution admonition. - * - * @param string|array $messages + * {@inheritdoc} */ - public function caution($messages) + public function caution($message) { - $this->format(new BlockFormatter($messages, 'CAUTION', 'fg=white;bg=red', ' ! ')); + $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! '); } /** - * Formats a table. - * - * @param array $headers - * @param array $rows + * {@inheritdoc} */ public function table(array $headers, array $rows) { @@ -162,13 +185,7 @@ public function table(array $headers, array $rows) } /** - * Asks a question. - * - * @param string $question - * @param string|null $default - * @param callable|null $validator - * - * @return string + * {@inheritdoc} */ public function ask($question, $default = null, $validator = null) { @@ -179,12 +196,7 @@ public function ask($question, $default = null, $validator = null) } /** - * Asks a question with the user input hidden. - * - * @param string $question - * @param callable|null $validator - * - * @return string + * {@inheritdoc} */ public function askHidden($question, $validator = null) { @@ -195,12 +207,7 @@ public function askHidden($question, $validator = null) } /** - * Asks for confirmation. - * - * @param string $question - * @param bool $default - * - * @return bool + * {@inheritdoc} */ public function confirm($question, $default = true) { @@ -208,13 +215,7 @@ public function confirm($question, $default = true) } /** - * Asks a choice question. - * - * @param string $question - * @param array $choices - * @param string|int|null $default - * - * @return string + * {@inheritdoc} */ public function choice($question, array $choices, $default = null) { @@ -227,13 +228,7 @@ public function choice($question, array $choices, $default = null) } /** - * Asks a choice question. - * - * @param string $question - * @param array $choices - * @param array $default - * - * @return string + * {@inheritdoc} */ public function multipleChoice($question, array $choices, array $default = array()) { @@ -250,6 +245,10 @@ public function multipleChoice($question, array $choices, array $default = array */ public function askQuestion(Question $question) { + if (!$this->questionHelper) { + $this->questionHelper = new SymfonyQuestionHelper(); + } + $ret = $this->questionHelper->ask($this->input, $this, $question); $this->ln(); From de14472c405fa604f5e756e03d7270640f2eaac6 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 3 Oct 2014 09:54:08 -0400 Subject: [PATCH 22/34] match current style guide --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 1cc89ca39399b..32d893b33fe14 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -151,7 +151,7 @@ public function error($message) */ public function warning($message) { - $this->block($message, 'WARNING', 'fg=black;bg=yellow'); + $this->block($message, 'WARNING', 'fg=white;bg=red'); } /** @@ -159,7 +159,7 @@ public function warning($message) */ public function note($message) { - $this->block($message, 'NOTE', null, ' ! '); + $this->block($message, 'NOTE', 'fg=yellow', ' ! '); } /** From d4d6f52726f61bcc40e85f324f8f4f400e39ba78 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 3 Oct 2014 10:20:20 -0400 Subject: [PATCH 23/34] added vertical padding for blocks --- .../Component/Console/Style/SymfonyStyle.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 32d893b33fe14..443d999158856 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -51,8 +51,9 @@ public function __construct(InputInterface $input, OutputInterface $output) * @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 */ - public function block($messages, $type = null, $style = null, $prefix = ' ') + public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false) { $messages = array_values((array) $messages); $lines = array(); @@ -72,6 +73,11 @@ public function block($messages, $type = null, $style = null, $prefix = ' ') } } + if ($padding && $this->isDecorated()) { + array_unshift($lines, ''); + $lines[] = ''; + } + foreach ($lines as &$line) { $line = sprintf('%s%s', $prefix, $line); $line .= str_repeat(' ', self::MAX_LENGTH - Helper::strlen($line)); @@ -135,7 +141,7 @@ public function text($message) */ public function success($message) { - $this->block($message, 'OK', 'fg=white;bg=green'); + $this->block($message, 'OK', 'fg=white;bg=green', ' ', true); } /** @@ -143,7 +149,7 @@ public function success($message) */ public function error($message) { - $this->block($message, 'ERROR', 'fg=white;bg=red'); + $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true); } /** @@ -151,7 +157,7 @@ public function error($message) */ public function warning($message) { - $this->block($message, 'WARNING', 'fg=white;bg=red'); + $this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true); } /** @@ -167,7 +173,7 @@ public function note($message) */ public function caution($message) { - $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! '); + $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true); } /** From a33d8a7a10d0eeaefd10d9eab1eabd3478ecf98b Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 3 Oct 2014 11:47:18 -0400 Subject: [PATCH 24/34] various fixes --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 443d999158856..aa65df7dfda66 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -28,7 +28,7 @@ */ class SymfonyStyle extends OutputStyle { - const MAX_LENGTH = 120; + const MAX_LINE_LENGTH = 120; private $input; private $questionHelper; @@ -66,7 +66,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa // wrap and add newlines for each element foreach ($messages as $key => $message) { $message = OutputFormatter::escape($message); - $lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LENGTH - Helper::strlen($prefix)))); + $lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LINE_LENGTH - Helper::strlen($prefix)))); if (count($messages) > 1 && $key < count($message)) { $lines[] = ''; @@ -80,7 +80,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa foreach ($lines as &$line) { $line = sprintf('%s%s', $prefix, $line); - $line .= str_repeat(' ', self::MAX_LENGTH - Helper::strlen($line)); + $line .= str_repeat(' ', self::MAX_LINE_LENGTH - Helper::strlen($line)); if ($style) { $line = sprintf('<%s>%s', $style, $line, $style); @@ -255,10 +255,10 @@ public function askQuestion(Question $question) $this->questionHelper = new SymfonyQuestionHelper(); } - $ret = $this->questionHelper->ask($this->input, $this, $question); + $answer = $this->questionHelper->ask($this->input, $this, $question); $this->ln(); - return $ret; + return $answer; } } From 18dbdb365dcfbd4e8f95f89e6dd0f2283a633628 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 25 Mar 2015 09:03:37 -0400 Subject: [PATCH 25/34] revert changes to FormatterHelper --- .../Component/Console/Helper/FormatterHelper.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index b94294345e38d..ac736f982e5c1 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -45,7 +45,9 @@ public function formatSection($section, $message, $style = 'info') */ public function formatBlock($messages, $style, $large = false) { - $messages = (array) $messages; + if (!is_array($messages)) { + $messages = array($messages); + } $len = 0; $lines = array(); @@ -56,15 +58,15 @@ public function formatBlock($messages, $style, $large = false) } $messages = $large ? array(str_repeat(' ', $len)) : array(); - foreach ($lines as $line) { - $messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); + for ($i = 0; isset($lines[$i]); ++$i) { + $messages[] = $lines[$i].str_repeat(' ', $len - $this->strlen($lines[$i])); } if ($large) { $messages[] = str_repeat(' ', $len); } - foreach ($messages as &$message) { - $message = sprintf('<%s>%s', $style, $message, $style); + for ($i = 0; isset($messages[$i]); ++$i) { + $messages[$i] = sprintf('<%s>%s', $style, $messages[$i], $style); } return implode("\n", $messages); From ae6d7a72f7745d757b7fcd04e56e333382eb239f Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 09:46:24 -0400 Subject: [PATCH 26/34] ensure formatter helper exists in QuestionHelper::writeError() --- src/Symfony/Component/Console/Helper/QuestionHelper.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index f0d5fd8548f34..9e4fa02f2c615 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -180,7 +180,13 @@ protected function writePrompt(OutputInterface $output, Question $question) */ protected function writeError(OutputInterface $output, \Exception $error) { - $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); + if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { + $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); + } else { + $message = ''.$error->getMessage().''; + } + + $output->writeln($message); } /** From 5c774cce2c18d663a1827a00199896158df8dcf2 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 09:51:33 -0400 Subject: [PATCH 27/34] simplify style tag --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index aa65df7dfda66..3740afb40109d 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -83,7 +83,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa $line .= str_repeat(' ', self::MAX_LINE_LENGTH - Helper::strlen($line)); if ($style) { - $line = sprintf('<%s>%s', $style, $line, $style); + $line = sprintf('<%s>%s', $style, $line); } } From c36189af745a30c2871e8727255086f3d1503169 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 09:53:04 -0400 Subject: [PATCH 28/34] rename StyleInterface::ln() to StyleInterface:newLine() --- src/Symfony/Component/Console/Style/OutputStyle.php | 2 +- src/Symfony/Component/Console/Style/StyleInterface.php | 2 +- src/Symfony/Component/Console/Style/SymfonyStyle.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Style/OutputStyle.php b/src/Symfony/Component/Console/Style/OutputStyle.php index d37f7eef85180..0e7a5c654db09 100644 --- a/src/Symfony/Component/Console/Style/OutputStyle.php +++ b/src/Symfony/Component/Console/Style/OutputStyle.php @@ -34,7 +34,7 @@ public function __construct(OutputInterface $output) /** * {@inheritdoc} */ - public function ln($count = 1) + public function newLine($count = 1) { $this->output->write(str_repeat(PHP_EOL, $count)); } diff --git a/src/Symfony/Component/Console/Style/StyleInterface.php b/src/Symfony/Component/Console/Style/StyleInterface.php index b589096fb4f0b..428c6db823a95 100644 --- a/src/Symfony/Component/Console/Style/StyleInterface.php +++ b/src/Symfony/Component/Console/Style/StyleInterface.php @@ -147,5 +147,5 @@ public function multipleChoice($question, array $choices, array $default = array * * @param int $count The number of newlines */ - public function ln($count = 1); + public function newLine($count = 1); } diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 3740afb40109d..5a35b490ddb64 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -187,7 +187,7 @@ public function table(array $headers, array $rows) $table->setStyle('symfony-style-guide'); $table->render(); - $this->ln(); + $this->newLine(); } /** @@ -257,7 +257,7 @@ public function askQuestion(Question $question) $answer = $this->questionHelper->ask($this->input, $this, $question); - $this->ln(); + $this->newLine(); return $answer; } From 85cc04c7f90d8e79c6d166899b51ee72c67a8fdc Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 10:01:23 -0400 Subject: [PATCH 29/34] add StyleInterface::progress() --- src/Symfony/Component/Console/Style/OutputStyle.php | 9 +++++++++ .../Component/Console/Style/StyleInterface.php | 9 +++++++++ .../Component/Console/Style/SymfonyStyle.php | 13 +++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Symfony/Component/Console/Style/OutputStyle.php b/src/Symfony/Component/Console/Style/OutputStyle.php index 0e7a5c654db09..5f4d50e9a46dc 100644 --- a/src/Symfony/Component/Console/Style/OutputStyle.php +++ b/src/Symfony/Component/Console/Style/OutputStyle.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Style; use Symfony\Component\Console\Formatter\OutputFormatterInterface; +use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\OutputInterface; /** @@ -39,6 +40,14 @@ public function newLine($count = 1) $this->output->write(str_repeat(PHP_EOL, $count)); } + /** + * {@inheritdoc} + */ + public function progress($max = 0) + { + return new ProgressBar($this->output, $max); + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Console/Style/StyleInterface.php b/src/Symfony/Component/Console/Style/StyleInterface.php index 428c6db823a95..9ec6457fd70e3 100644 --- a/src/Symfony/Component/Console/Style/StyleInterface.php +++ b/src/Symfony/Component/Console/Style/StyleInterface.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Console\Style; +use Symfony\Component\Console\Helper\ProgressBar; + /** * Output style helpers * @@ -148,4 +150,11 @@ public function multipleChoice($question, array $choices, array $default = array * @param int $count The number of newlines */ public function newLine($count = 1); + + /** + * @param int $max Maximum steps (0 if unknown) + * + * @return ProgressBar + */ + public function progress($max = 0); } diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 5a35b490ddb64..928eac7d11b83 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -244,6 +244,19 @@ public function multipleChoice($question, array $choices, array $default = array return $this->askQuestion($question); } + /** + * {@inheritdoc} + */ + public function progress($max = 0) + { + $progress = parent::progress($max); + $progress->setEmptyBarCharacter('░'); // light shade character \u2591 + $progress->setProgressCharacter(''); + $progress->setBarCharacter('▓'); // dark shade character \u2593 + + return $progress; + } + /** * @param Question $question * From e0efaa8c4c5ef654a315b3bedc479173c9a361c1 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 10:22:30 -0400 Subject: [PATCH 30/34] fix bug --- src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php index 002b29b5cf7a3..77130f9776ac2 100644 --- a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php @@ -95,7 +95,7 @@ protected function writePrompt(OutputInterface $output, Question $question) protected function writeError(OutputInterface $output, \Exception $error) { if ($output instanceof SymfonyStyle) { - $output->ln(); + $output->newLine(); $output->error($error->getMessage()); return; From 91941f85b8789073e557c1a8463993b41cf7e344 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 11:15:15 -0400 Subject: [PATCH 31/34] add OutputStyle::progressStart, progressAdvance, progressFinish --- .../Component/Console/Style/OutputStyle.php | 6 ++- .../Console/Style/StyleInterface.php | 18 ++++++-- .../Component/Console/Style/SymfonyStyle.php | 45 ++++++++++++++++++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Console/Style/OutputStyle.php b/src/Symfony/Component/Console/Style/OutputStyle.php index 5f4d50e9a46dc..13ed05b13dc4a 100644 --- a/src/Symfony/Component/Console/Style/OutputStyle.php +++ b/src/Symfony/Component/Console/Style/OutputStyle.php @@ -41,9 +41,11 @@ public function newLine($count = 1) } /** - * {@inheritdoc} + * @param int $max + * + * @return ProgressBar */ - public function progress($max = 0) + public function createProgressBar($max = 0) { return new ProgressBar($this->output, $max); } diff --git a/src/Symfony/Component/Console/Style/StyleInterface.php b/src/Symfony/Component/Console/Style/StyleInterface.php index 9ec6457fd70e3..90e3eb92fb4ec 100644 --- a/src/Symfony/Component/Console/Style/StyleInterface.php +++ b/src/Symfony/Component/Console/Style/StyleInterface.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Console\Style; -use Symfony\Component\Console\Helper\ProgressBar; - /** * Output style helpers * @@ -152,9 +150,21 @@ public function multipleChoice($question, array $choices, array $default = array public function newLine($count = 1); /** + * Starts the progress output. + * * @param int $max Maximum steps (0 if unknown) + */ + public function progressStart($max = 0); + + /** + * Advances the progress output X steps. * - * @return ProgressBar + * @param int $step Number of steps to advance + */ + public function progressAdvance($step = 1); + + /** + * Finishes the progress output. */ - public function progress($max = 0); + public function progressFinish(); } diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 928eac7d11b83..ed9c759f6fcca 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -13,6 +13,7 @@ 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\Input\InputInterface; @@ -32,6 +33,7 @@ class SymfonyStyle extends OutputStyle private $input; private $questionHelper; + private $progressBar; /** * @param InputInterface $input @@ -247,9 +249,36 @@ public function multipleChoice($question, array $choices, array $default = array /** * {@inheritdoc} */ - public function progress($max = 0) + public function progressStart($max = 0) { - $progress = parent::progress($max); + $this->progressBar = $this->createProgressBar($max); + $this->progressBar->start(); + } + + /** + * {@inheritdoc} + */ + public function progressAdvance($step = 1) + { + $this->getProgressBar()->advance($step); + } + + /** + * {@inheritdoc} + */ + public function progressFinish() + { + $this->getProgressBar()->finish(); + $this->newLine(2); + $this->progressBar = null; + } + + /** + * {@inheritdoc} + */ + public function createProgressBar($max = 0) + { + $progress = parent::createProgressBar($max); $progress->setEmptyBarCharacter('░'); // light shade character \u2591 $progress->setProgressCharacter(''); $progress->setBarCharacter('▓'); // dark shade character \u2593 @@ -274,4 +303,16 @@ public function askQuestion(Question $question) return $answer; } + + /** + * @return ProgressBar + */ + private function getProgressBar() + { + if (!$this->progressBar) { + throw new \RuntimeException('The ProgressBar is not started.'); + } + + return $this->progressBar; + } } From 8143e7786db3785b287e975c73af4fc78895277b Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 26 Mar 2015 12:34:41 -0400 Subject: [PATCH 32/34] remove StyleInterface::multipleChoice() --- .../Component/Console/Style/StyleInterface.php | 11 ----------- src/Symfony/Component/Console/Style/SymfonyStyle.php | 11 ----------- 2 files changed, 22 deletions(-) diff --git a/src/Symfony/Component/Console/Style/StyleInterface.php b/src/Symfony/Component/Console/Style/StyleInterface.php index 90e3eb92fb4ec..214782e3939fd 100644 --- a/src/Symfony/Component/Console/Style/StyleInterface.php +++ b/src/Symfony/Component/Console/Style/StyleInterface.php @@ -131,17 +131,6 @@ public function confirm($question, $default = true); */ public function choice($question, array $choices, $default = null); - /** - * Asks a multiple choice question. - * - * @param string $question - * @param array $choices - * @param array $default - * - * @return string - */ - public function multipleChoice($question, array $choices, array $default = array()); - /** * Add newline(s) * diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index ed9c759f6fcca..5aee242be847d 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -235,17 +235,6 @@ public function choice($question, array $choices, $default = null) return $this->askQuestion(new ChoiceQuestion($question, $choices, $default)); } - /** - * {@inheritdoc} - */ - public function multipleChoice($question, array $choices, array $default = array()) - { - $question = new ChoiceQuestion($question, $choices, $default); - $question->setMultiselect(true); - - return $this->askQuestion($question); - } - /** * {@inheritdoc} */ From f0cb90a705bdd4d821addb4103c80cf9959c5174 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 27 Mar 2015 08:16:04 -0400 Subject: [PATCH 33/34] set custom progressbar characters only in non-windows environments --- .../Component/Console/Style/SymfonyStyle.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 5aee242be847d..3c72c12234f8d 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -267,12 +267,15 @@ public function progressFinish() */ public function createProgressBar($max = 0) { - $progress = parent::createProgressBar($max); - $progress->setEmptyBarCharacter('░'); // light shade character \u2591 - $progress->setProgressCharacter(''); - $progress->setBarCharacter('▓'); // dark shade character \u2593 + $progressBar = parent::createProgressBar($max); - return $progress; + if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 + $progressBar->setProgressCharacter(''); + $progressBar->setBarCharacter('▓'); // dark shade character \u2593 + } + + return $progressBar; } /** From bbdb37c2297b5c828a00324b7cf8da96a9aadef0 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 27 Mar 2015 12:04:30 -0400 Subject: [PATCH 34/34] use DIRECTORY_SEPARATOR to detect Windows --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 3c72c12234f8d..1b2e7368f62ee 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -269,7 +269,7 @@ public function createProgressBar($max = 0) { $progressBar = parent::createProgressBar($max); - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if ('\\' === DIRECTORY_SEPARATOR) { $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 $progressBar->setProgressCharacter(''); $progressBar->setBarCharacter('▓'); // dark shade character \u2593