summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Formatter
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2017-10-25 16:22:10 -0700
committerFrederic Guillot <fred@kanboard.net>2017-10-25 16:22:10 -0700
commit9e2b2a32fd0e967ad3184e9a5d091a29953acb91 (patch)
tree00822e24aa1110c73ca455a8d096ef296c008cbc /vendor/symfony/console/Formatter
parentc507c5416251c505cb3e088a03c6664bed73c812 (diff)
Include composer dependencies in repo
Diffstat (limited to 'vendor/symfony/console/Formatter')
-rw-r--r--vendor/symfony/console/Formatter/OutputFormatter.php240
-rw-r--r--vendor/symfony/console/Formatter/OutputFormatterInterface.php69
-rw-r--r--vendor/symfony/console/Formatter/OutputFormatterStyle.php221
-rw-r--r--vendor/symfony/console/Formatter/OutputFormatterStyleInterface.php64
-rw-r--r--vendor/symfony/console/Formatter/OutputFormatterStyleStack.php123
5 files changed, 717 insertions, 0 deletions
diff --git a/vendor/symfony/console/Formatter/OutputFormatter.php b/vendor/symfony/console/Formatter/OutputFormatter.php
new file mode 100644
index 00000000..56cd5e56
--- /dev/null
+++ b/vendor/symfony/console/Formatter/OutputFormatter.php
@@ -0,0 +1,240 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Formatter;
+
+use Symfony\Component\Console\Exception\InvalidArgumentException;
+
+/**
+ * Formatter class for console output.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+class OutputFormatter implements OutputFormatterInterface
+{
+ private $decorated;
+ private $styles = array();
+ private $styleStack;
+
+ /**
+ * Escapes "<" special char in given text.
+ *
+ * @param string $text Text to escape
+ *
+ * @return string Escaped text
+ */
+ public static function escape($text)
+ {
+ $text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
+
+ if ('\\' === substr($text, -1)) {
+ $len = strlen($text);
+ $text = rtrim($text, '\\');
+ $text .= str_repeat('<<', $len - strlen($text));
+ }
+
+ return $text;
+ }
+
+ /**
+ * Initializes console output formatter.
+ *
+ * @param bool $decorated Whether this formatter should actually decorate strings
+ * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
+ */
+ public function __construct($decorated = false, array $styles = array())
+ {
+ $this->decorated = (bool) $decorated;
+
+ $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
+ $this->setStyle('info', new OutputFormatterStyle('green'));
+ $this->setStyle('comment', new OutputFormatterStyle('yellow'));
+ $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
+
+ foreach ($styles as $name => $style) {
+ $this->setStyle($name, $style);
+ }
+
+ $this->styleStack = new OutputFormatterStyleStack();
+ }
+
+ /**
+ * Sets the decorated flag.
+ *
+ * @param bool $decorated Whether to decorate the messages or not
+ */
+ public function setDecorated($decorated)
+ {
+ $this->decorated = (bool) $decorated;
+ }
+
+ /**
+ * Gets the decorated flag.
+ *
+ * @return bool true if the output will decorate messages, false otherwise
+ */
+ public function isDecorated()
+ {
+ return $this->decorated;
+ }
+
+ /**
+ * Sets a new style.
+ *
+ * @param string $name The style name
+ * @param OutputFormatterStyleInterface $style The style instance
+ */
+ public function setStyle($name, OutputFormatterStyleInterface $style)
+ {
+ $this->styles[strtolower($name)] = $style;
+ }
+
+ /**
+ * Checks if output formatter has style with specified name.
+ *
+ * @param string $name
+ *
+ * @return bool
+ */
+ public function hasStyle($name)
+ {
+ return isset($this->styles[strtolower($name)]);
+ }
+
+ /**
+ * Gets style options from style with specified name.
+ *
+ * @param string $name
+ *
+ * @return OutputFormatterStyleInterface
+ *
+ * @throws InvalidArgumentException When style isn't defined
+ */
+ public function getStyle($name)
+ {
+ if (!$this->hasStyle($name)) {
+ throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
+ }
+
+ return $this->styles[strtolower($name)];
+ }
+
+ /**
+ * Formats a message according to the given styles.
+ *
+ * @param string $message The message to style
+ *
+ * @return string The styled message
+ */
+ public function format($message)
+ {
+ $message = (string) $message;
+ $offset = 0;
+ $output = '';
+ $tagRegex = '[a-z][a-z0-9_=;-]*+';
+ preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
+ foreach ($matches[0] as $i => $match) {
+ $pos = $match[1];
+ $text = $match[0];
+
+ if (0 != $pos && '\\' == $message[$pos - 1]) {
+ continue;
+ }
+
+ // add the text up to the next tag
+ $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
+ $offset = $pos + strlen($text);
+
+ // opening tag?
+ if ($open = '/' != $text[1]) {
+ $tag = $matches[1][$i][0];
+ } else {
+ $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
+ }
+
+ if (!$open && !$tag) {
+ // </>
+ $this->styleStack->pop();
+ } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
+ $output .= $this->applyCurrentStyle($text);
+ } elseif ($open) {
+ $this->styleStack->push($style);
+ } else {
+ $this->styleStack->pop($style);
+ }
+ }
+
+ $output .= $this->applyCurrentStyle(substr($message, $offset));
+
+ if (false !== strpos($output, '<<')) {
+ return strtr($output, array('\\<' => '<', '<<' => '\\'));
+ }
+
+ return str_replace('\\<', '<', $output);
+ }
+
+ /**
+ * @return OutputFormatterStyleStack
+ */
+ public function getStyleStack()
+ {
+ return $this->styleStack;
+ }
+
+ /**
+ * Tries to create new style instance from string.
+ *
+ * @param string $string
+ *
+ * @return OutputFormatterStyle|bool false if string is not format string
+ */
+ private function createStyleFromString($string)
+ {
+ if (isset($this->styles[$string])) {
+ return $this->styles[$string];
+ }
+
+ if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
+ return false;
+ }
+
+ $style = new OutputFormatterStyle();
+ foreach ($matches as $match) {
+ array_shift($match);
+
+ if ('fg' == $match[0]) {
+ $style->setForeground($match[1]);
+ } elseif ('bg' == $match[0]) {
+ $style->setBackground($match[1]);
+ } else {
+ try {
+ $style->setOption($match[1]);
+ } catch (\InvalidArgumentException $e) {
+ return false;
+ }
+ }
+ }
+
+ return $style;
+ }
+
+ /**
+ * Applies current style from stack to text, if must be applied.
+ *
+ * @param string $text Input text
+ *
+ * @return string Styled text
+ */
+ private function applyCurrentStyle($text)
+ {
+ return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
+ }
+}
diff --git a/vendor/symfony/console/Formatter/OutputFormatterInterface.php b/vendor/symfony/console/Formatter/OutputFormatterInterface.php
new file mode 100644
index 00000000..5a52ba09
--- /dev/null
+++ b/vendor/symfony/console/Formatter/OutputFormatterInterface.php
@@ -0,0 +1,69 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Formatter;
+
+/**
+ * Formatter interface for console output.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+interface OutputFormatterInterface
+{
+ /**
+ * Sets the decorated flag.
+ *
+ * @param bool $decorated Whether to decorate the messages or not
+ */
+ public function setDecorated($decorated);
+
+ /**
+ * Gets the decorated flag.
+ *
+ * @return bool true if the output will decorate messages, false otherwise
+ */
+ public function isDecorated();
+
+ /**
+ * Sets a new style.
+ *
+ * @param string $name The style name
+ * @param OutputFormatterStyleInterface $style The style instance
+ */
+ public function setStyle($name, OutputFormatterStyleInterface $style);
+
+ /**
+ * Checks if output formatter has style with specified name.
+ *
+ * @param string $name
+ *
+ * @return bool
+ */
+ public function hasStyle($name);
+
+ /**
+ * Gets style options from style with specified name.
+ *
+ * @param string $name
+ *
+ * @return OutputFormatterStyleInterface
+ */
+ public function getStyle($name);
+
+ /**
+ * Formats a message according to the given styles.
+ *
+ * @param string $message The message to style
+ *
+ * @return string The styled message
+ */
+ public function format($message);
+}
diff --git a/vendor/symfony/console/Formatter/OutputFormatterStyle.php b/vendor/symfony/console/Formatter/OutputFormatterStyle.php
new file mode 100644
index 00000000..c7c6b4a0
--- /dev/null
+++ b/vendor/symfony/console/Formatter/OutputFormatterStyle.php
@@ -0,0 +1,221 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Formatter;
+
+use Symfony\Component\Console\Exception\InvalidArgumentException;
+
+/**
+ * Formatter style class for defining styles.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+class OutputFormatterStyle implements OutputFormatterStyleInterface
+{
+ private static $availableForegroundColors = array(
+ 'black' => array('set' => 30, 'unset' => 39),
+ 'red' => array('set' => 31, 'unset' => 39),
+ 'green' => array('set' => 32, 'unset' => 39),
+ 'yellow' => array('set' => 33, 'unset' => 39),
+ 'blue' => array('set' => 34, 'unset' => 39),
+ 'magenta' => array('set' => 35, 'unset' => 39),
+ 'cyan' => array('set' => 36, 'unset' => 39),
+ 'white' => array('set' => 37, 'unset' => 39),
+ 'default' => array('set' => 39, 'unset' => 39),
+ );
+ private static $availableBackgroundColors = array(
+ 'black' => array('set' => 40, 'unset' => 49),
+ 'red' => array('set' => 41, 'unset' => 49),
+ 'green' => array('set' => 42, 'unset' => 49),
+ 'yellow' => array('set' => 43, 'unset' => 49),
+ 'blue' => array('set' => 44, 'unset' => 49),
+ 'magenta' => array('set' => 45, 'unset' => 49),
+ 'cyan' => array('set' => 46, 'unset' => 49),
+ 'white' => array('set' => 47, 'unset' => 49),
+ 'default' => array('set' => 49, 'unset' => 49),
+ );
+ private static $availableOptions = array(
+ 'bold' => array('set' => 1, 'unset' => 22),
+ 'underscore' => array('set' => 4, 'unset' => 24),
+ 'blink' => array('set' => 5, 'unset' => 25),
+ 'reverse' => array('set' => 7, 'unset' => 27),
+ 'conceal' => array('set' => 8, 'unset' => 28),
+ );
+
+ private $foreground;
+ private $background;
+ private $options = array();
+
+ /**
+ * Initializes output formatter style.
+ *
+ * @param string|null $foreground The style foreground color name
+ * @param string|null $background The style background color name
+ * @param array $options The style options
+ */
+ public function __construct($foreground = null, $background = null, array $options = array())
+ {
+ if (null !== $foreground) {
+ $this->setForeground($foreground);
+ }
+ if (null !== $background) {
+ $this->setBackground($background);
+ }
+ if (count($options)) {
+ $this->setOptions($options);
+ }
+ }
+
+ /**
+ * Sets style foreground color.
+ *
+ * @param string|null $color The color name
+ *
+ * @throws InvalidArgumentException When the color name isn't defined
+ */
+ public function setForeground($color = null)
+ {
+ if (null === $color) {
+ $this->foreground = null;
+
+ return;
+ }
+
+ if (!isset(static::$availableForegroundColors[$color])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Invalid foreground color specified: "%s". Expected one of (%s)',
+ $color,
+ implode(', ', array_keys(static::$availableForegroundColors))
+ ));
+ }
+
+ $this->foreground = static::$availableForegroundColors[$color];
+ }
+
+ /**
+ * Sets style background color.
+ *
+ * @param string|null $color The color name
+ *
+ * @throws InvalidArgumentException When the color name isn't defined
+ */
+ public function setBackground($color = null)
+ {
+ if (null === $color) {
+ $this->background = null;
+
+ return;
+ }
+
+ if (!isset(static::$availableBackgroundColors[$color])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Invalid background color specified: "%s". Expected one of (%s)',
+ $color,
+ implode(', ', array_keys(static::$availableBackgroundColors))
+ ));
+ }
+
+ $this->background = static::$availableBackgroundColors[$color];
+ }
+
+ /**
+ * Sets some specific style option.
+ *
+ * @param string $option The option name
+ *
+ * @throws InvalidArgumentException When the option name isn't defined
+ */
+ public function setOption($option)
+ {
+ if (!isset(static::$availableOptions[$option])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Invalid option specified: "%s". Expected one of (%s)',
+ $option,
+ implode(', ', array_keys(static::$availableOptions))
+ ));
+ }
+
+ if (!in_array(static::$availableOptions[$option], $this->options)) {
+ $this->options[] = static::$availableOptions[$option];
+ }
+ }
+
+ /**
+ * Unsets some specific style option.
+ *
+ * @param string $option The option name
+ *
+ * @throws InvalidArgumentException When the option name isn't defined
+ */
+ public function unsetOption($option)
+ {
+ if (!isset(static::$availableOptions[$option])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Invalid option specified: "%s". Expected one of (%s)',
+ $option,
+ implode(', ', array_keys(static::$availableOptions))
+ ));
+ }
+
+ $pos = array_search(static::$availableOptions[$option], $this->options);
+ if (false !== $pos) {
+ unset($this->options[$pos]);
+ }
+ }
+
+ /**
+ * Sets multiple style options at once.
+ *
+ * @param array $options
+ */
+ public function setOptions(array $options)
+ {
+ $this->options = array();
+
+ foreach ($options as $option) {
+ $this->setOption($option);
+ }
+ }
+
+ /**
+ * Applies the style to a given text.
+ *
+ * @param string $text The text to style
+ *
+ * @return string
+ */
+ public function apply($text)
+ {
+ $setCodes = array();
+ $unsetCodes = array();
+
+ if (null !== $this->foreground) {
+ $setCodes[] = $this->foreground['set'];
+ $unsetCodes[] = $this->foreground['unset'];
+ }
+ if (null !== $this->background) {
+ $setCodes[] = $this->background['set'];
+ $unsetCodes[] = $this->background['unset'];
+ }
+ if (count($this->options)) {
+ foreach ($this->options as $option) {
+ $setCodes[] = $option['set'];
+ $unsetCodes[] = $option['unset'];
+ }
+ }
+
+ if (0 === count($setCodes)) {
+ return $text;
+ }
+
+ return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
+ }
+}
diff --git a/vendor/symfony/console/Formatter/OutputFormatterStyleInterface.php b/vendor/symfony/console/Formatter/OutputFormatterStyleInterface.php
new file mode 100644
index 00000000..c36fda80
--- /dev/null
+++ b/vendor/symfony/console/Formatter/OutputFormatterStyleInterface.php
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Formatter;
+
+/**
+ * Formatter style interface for defining styles.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+interface OutputFormatterStyleInterface
+{
+ /**
+ * Sets style foreground color.
+ *
+ * @param string $color The color name
+ */
+ public function setForeground($color = null);
+
+ /**
+ * Sets style background color.
+ *
+ * @param string $color The color name
+ */
+ public function setBackground($color = null);
+
+ /**
+ * Sets some specific style option.
+ *
+ * @param string $option The option name
+ */
+ public function setOption($option);
+
+ /**
+ * Unsets some specific style option.
+ *
+ * @param string $option The option name
+ */
+ public function unsetOption($option);
+
+ /**
+ * Sets multiple style options at once.
+ *
+ * @param array $options
+ */
+ public function setOptions(array $options);
+
+ /**
+ * Applies the style to a given text.
+ *
+ * @param string $text The text to style
+ *
+ * @return string
+ */
+ public function apply($text);
+}
diff --git a/vendor/symfony/console/Formatter/OutputFormatterStyleStack.php b/vendor/symfony/console/Formatter/OutputFormatterStyleStack.php
new file mode 100644
index 00000000..e5d14ea3
--- /dev/null
+++ b/vendor/symfony/console/Formatter/OutputFormatterStyleStack.php
@@ -0,0 +1,123 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Formatter;
+
+use Symfony\Component\Console\Exception\InvalidArgumentException;
+
+/**
+ * @author Jean-François Simon <contact@jfsimon.fr>
+ */
+class OutputFormatterStyleStack
+{
+ /**
+ * @var OutputFormatterStyleInterface[]
+ */
+ private $styles;
+
+ /**
+ * @var OutputFormatterStyleInterface
+ */
+ private $emptyStyle;
+
+ /**
+ * Constructor.
+ *
+ * @param OutputFormatterStyleInterface|null $emptyStyle
+ */
+ public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
+ {
+ $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
+ $this->reset();
+ }
+
+ /**
+ * Resets stack (ie. empty internal arrays).
+ */
+ public function reset()
+ {
+ $this->styles = array();
+ }
+
+ /**
+ * Pushes a style in the stack.
+ *
+ * @param OutputFormatterStyleInterface $style
+ */
+ public function push(OutputFormatterStyleInterface $style)
+ {
+ $this->styles[] = $style;
+ }
+
+ /**
+ * Pops a style from the stack.
+ *
+ * @param OutputFormatterStyleInterface|null $style
+ *
+ * @return OutputFormatterStyleInterface
+ *
+ * @throws InvalidArgumentException When style tags incorrectly nested
+ */
+ public function pop(OutputFormatterStyleInterface $style = null)
+ {
+ if (empty($this->styles)) {
+ return $this->emptyStyle;
+ }
+
+ if (null === $style) {
+ return array_pop($this->styles);
+ }
+
+ foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
+ if ($style->apply('') === $stackedStyle->apply('')) {
+ $this->styles = array_slice($this->styles, 0, $index);
+
+ return $stackedStyle;
+ }
+ }
+
+ throw new InvalidArgumentException('Incorrectly nested style tag found.');
+ }
+
+ /**
+ * Computes current style with stacks top codes.
+ *
+ * @return OutputFormatterStyle
+ */
+ public function getCurrent()
+ {
+ if (empty($this->styles)) {
+ return $this->emptyStyle;
+ }
+
+ return $this->styles[count($this->styles) - 1];
+ }
+
+ /**
+ * @param OutputFormatterStyleInterface $emptyStyle
+ *
+ * @return OutputFormatterStyleStack
+ */
+ public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
+ {
+ $this->emptyStyle = $emptyStyle;
+
+ return $this;
+ }
+
+ /**
+ * @return OutputFormatterStyleInterface
+ */
+ public function getEmptyStyle()
+ {
+ return $this->emptyStyle;
+ }
+}