Revert "merged branch jfsimon/master (PR #3613)"
This reverts commit959158f9b9
, reversing changes made tob9de0be349
.
This commit is contained in:
parent
97fd965b70
commit
479d808e6e
@ -23,11 +23,10 @@ class OutputFormatter implements OutputFormatterInterface
|
||||
/**
|
||||
* The pattern to phrase the format.
|
||||
*/
|
||||
const FORMAT_PATTERN = '#<(/?)([a-z][a-z0-9_=;-]+)?>#is';
|
||||
const FORMAT_PATTERN = '#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#is';
|
||||
|
||||
private $decorated;
|
||||
private $styles = array();
|
||||
private $styleStack;
|
||||
|
||||
/**
|
||||
* Initializes console output formatter.
|
||||
@ -49,8 +48,6 @@ class OutputFormatter implements OutputFormatterInterface
|
||||
foreach ($styles as $name => $style) {
|
||||
$this->setStyle($name, $style);
|
||||
}
|
||||
|
||||
$this->styleStack = new OutputFormatterStyleStack();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,30 +144,21 @@ class OutputFormatter implements OutputFormatterInterface
|
||||
*/
|
||||
private function replaceStyle($match)
|
||||
{
|
||||
// Special "</>" tag resets all styles.
|
||||
if (!isset($match[2])) {
|
||||
$this->styleStack->reset();
|
||||
|
||||
return $this->isDecorated() ? "\033[0m" : '';
|
||||
if (!$this->isDecorated()) {
|
||||
return $match[2];
|
||||
}
|
||||
|
||||
if (isset($this->styles[strtolower($match[2])])) {
|
||||
$style = $this->styles[strtolower($match[2])];
|
||||
if (isset($this->styles[strtolower($match[1])])) {
|
||||
$style = $this->styles[strtolower($match[1])];
|
||||
} else {
|
||||
$style = $this->createStyleFromString($match[2]);
|
||||
$style = $this->createStyleFromString($match[1]);
|
||||
|
||||
if (false === $style) {
|
||||
return '';
|
||||
return $match[0];
|
||||
}
|
||||
}
|
||||
|
||||
if ('/' === $match[1]) {
|
||||
$this->styleStack->popStyle($style);
|
||||
} else {
|
||||
$this->styleStack->pushStyle($style);
|
||||
}
|
||||
|
||||
return $this->isDecorated() ? $this->styleStack->getCurrentStyle()->getTerminalSequence() : '';
|
||||
return $style->apply($this->format($match[2]));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -178,7 +166,7 @@ class OutputFormatter implements OutputFormatterInterface
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return \Symfony\Component\Console\Formatter\OutputFormatterStyle|Boolean false if string is not format string
|
||||
* @return Symfony\Component\Console\Format\FormatterStyle|Boolean false if string is not format string
|
||||
*/
|
||||
private function createStyleFromString($string)
|
||||
{
|
||||
|
@ -102,22 +102,6 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
|
||||
$this->foreground = static::$availableForegroundColors[$color];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets style foreground color.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getForeground()
|
||||
{
|
||||
if (null === $this->foreground) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$names = array_flip(static::$availableForegroundColors);
|
||||
|
||||
return $names[$this->foreground];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets style background color.
|
||||
*
|
||||
@ -146,22 +130,6 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
|
||||
$this->background = static::$availableBackgroundColors[$color];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets style background color.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getBackground()
|
||||
{
|
||||
if (null === $this->background) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$names = array_flip(static::$availableBackgroundColors);
|
||||
|
||||
return $names[$this->background];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets some specific style option.
|
||||
*
|
||||
@ -224,23 +192,6 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets specific style options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
$names = array_flip(static::$availableOptions);
|
||||
$options = array();
|
||||
|
||||
foreach ($this->options as $code) {
|
||||
$options[] = $names[$code];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the style to a given text.
|
||||
*
|
||||
@ -249,16 +200,6 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
|
||||
* @return string
|
||||
*/
|
||||
public function apply($text)
|
||||
{
|
||||
return sprintf("%s%s\033[0m", $this->getTerminalSequence(), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets terminal colorization sequence.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTerminalSequence()
|
||||
{
|
||||
$codes = array();
|
||||
|
||||
@ -271,10 +212,7 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
|
||||
if (count($this->options)) {
|
||||
$codes = array_merge($codes, $this->options);
|
||||
}
|
||||
if (0 === count($codes)) {
|
||||
$codes = array(0);
|
||||
}
|
||||
|
||||
return sprintf("\033[%sm", implode(';', $codes));
|
||||
return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
|
||||
}
|
||||
}
|
||||
|
@ -1,136 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* @author: Jean-François Simon <contact@jfsimon.fr>
|
||||
*/
|
||||
class OutputFormatterStyleStack
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $foregrounds;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $backgrounds;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets stack (ie. empty internal arrays).
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->foregrounds = array();
|
||||
$this->backgrounds = array();
|
||||
$this->options = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes a style in the stack.
|
||||
*
|
||||
* @param OutputFormatterStyle $style
|
||||
*/
|
||||
public function pushStyle(OutputFormatterStyle $style)
|
||||
{
|
||||
$foreground = $style->getForeground();
|
||||
if (null !== $foreground) {
|
||||
$this->foregrounds[] = $foreground;
|
||||
}
|
||||
|
||||
$background = $style->getBackground();
|
||||
if (null !== $background) {
|
||||
$this->backgrounds[] = $background;
|
||||
}
|
||||
|
||||
foreach ($style->getOptions() as $option) {
|
||||
if (!isset($this->options[$option])) {
|
||||
$this->options[$option] = 0;
|
||||
}
|
||||
$this->options[$option] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a style from the stack.
|
||||
*
|
||||
* @param OutputFormatterStyle $style
|
||||
*
|
||||
* @throws \InvalidArgumentException When style tags incorrectly nested
|
||||
*/
|
||||
public function popStyle(OutputFormatterStyle $style)
|
||||
{
|
||||
$this->popArrayCode($this->foregrounds, $style->getForeground());
|
||||
$this->popArrayCode($this->backgrounds, $style->getBackground());
|
||||
|
||||
foreach ($style->getOptions() as $option) {
|
||||
if (!isset($this->options[$option])) {
|
||||
throw new \InvalidArgumentException('Unexpected style "'.$option.'" option end.');
|
||||
}
|
||||
|
||||
$this->options[$option] -= 1;
|
||||
if (0 === $this->options[$option]) {
|
||||
unset($this->options[$option]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes current style with stacks top codes.
|
||||
*
|
||||
* @return OutputFormatterStyle
|
||||
*/
|
||||
public function getCurrentStyle()
|
||||
{
|
||||
return new OutputFormatterStyle(
|
||||
end($this->foregrounds) ?: null,
|
||||
end($this->backgrounds) ?: null,
|
||||
array_keys($this->options)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a color from a stack.
|
||||
*
|
||||
* @param array $stack An array of color names
|
||||
* @param string $color A color name
|
||||
*
|
||||
* @throws \InvalidArgumentException When poped color is not the expected one
|
||||
*/
|
||||
private function popArrayCode(&$stack, $color)
|
||||
{
|
||||
if (null === $color) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current = end($stack);
|
||||
if ($current !== $color) {
|
||||
throw new \InvalidArgumentException('Expected style "'.$current.'" color end but "'.$color.'" end found.');
|
||||
}
|
||||
|
||||
array_pop($stack);
|
||||
}
|
||||
}
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\Console\Tests\Formatter;
|
||||
|
||||
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
|
||||
class FormatterStyleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -44,7 +43,7 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$formatter = new OutputFormatter(true);
|
||||
|
||||
$this->assertEquals(
|
||||
"\033[37;41msome \033[32;41msome info\033[37;41m error\033[0m", $formatter->format('<error>some <info>some info</info> error</error>')
|
||||
"\033[37;41msome \033[32msome info\033[0m error\033[0m", $formatter->format('<error>some <info>some info</info> error</error>')
|
||||
);
|
||||
}
|
||||
|
||||
@ -52,23 +51,36 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$formatter = new OutputFormatter(true);
|
||||
|
||||
$style = new OutputFormatterStyle('red', 'blue');
|
||||
$style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')->getMock();
|
||||
$formatter->setStyle('test', $style);
|
||||
|
||||
$this->assertEquals($style, $formatter->getStyle('test'));
|
||||
$this->assertNotEquals($style, $formatter->getStyle('info'));
|
||||
|
||||
$this->assertEquals("\033[31;44msome custom msg\033[0m", $formatter->format('<test>some custom msg</test>'));
|
||||
$style
|
||||
->expects($this->once())
|
||||
->method('apply')
|
||||
->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
|
||||
|
||||
$this->assertEquals("[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>'));
|
||||
}
|
||||
|
||||
public function testRedefineStyle()
|
||||
{
|
||||
$formatter = new OutputFormatter(true);
|
||||
|
||||
$style = new OutputFormatterStyle('red', 'blue');
|
||||
$style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')
|
||||
->getMock();
|
||||
$formatter->setStyle('info', $style);
|
||||
|
||||
$this->assertEquals("\033[31;44msome custom msg\033[0m", $formatter->format('<info>some custom msg</info>'));
|
||||
$style
|
||||
->expects($this->once())
|
||||
->method('apply')
|
||||
->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
|
||||
|
||||
$this->assertEquals(
|
||||
"[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<info>some custom msg</info>')
|
||||
);
|
||||
}
|
||||
|
||||
public function testInlineStyle()
|
||||
|
@ -1,74 +0,0 @@
|
||||
<?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\Tests\Component\Console\Formatter;
|
||||
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyleStack;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
|
||||
class OutputFormatterStyleStackTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testPush()
|
||||
{
|
||||
$stack = new OutputFormatterStyleStack();
|
||||
$stack->pushStyle(new OutputFormatterStyle('white'));
|
||||
$stack->pushStyle(new OutputFormatterStyle(null, 'black'));
|
||||
$stack->pushStyle(new OutputFormatterStyle(null, null, array('bold')));
|
||||
|
||||
$style = $stack->getCurrentStyle();
|
||||
$this->assertEquals('white', $style->getForeground());
|
||||
$this->assertEquals('black', $style->getBackground());
|
||||
$this->assertEquals(array('bold'), $style->getOptions());
|
||||
|
||||
$stack->pushStyle(new OutputFormatterStyle('yellow', null, array('blink')));
|
||||
|
||||
$style = $stack->getCurrentStyle();
|
||||
$this->assertEquals('yellow', $style->getForeground());
|
||||
$this->assertEquals('black', $style->getBackground());
|
||||
$this->assertEquals(array('bold', 'blink'), $style->getOptions());
|
||||
}
|
||||
|
||||
public function testPop()
|
||||
{
|
||||
$stack = new OutputFormatterStyleStack();
|
||||
$stack->pushStyle(new OutputFormatterStyle('white', 'black', array('blink', 'bold')));
|
||||
$stack->pushStyle(new OutputFormatterStyle('yellow', 'blue'));
|
||||
|
||||
$style = $stack->getCurrentStyle();
|
||||
$this->assertEquals('yellow', $style->getForeground());
|
||||
$this->assertEquals('blue', $style->getBackground());
|
||||
$this->assertEquals(array('blink', 'bold'), $style->getOptions());
|
||||
|
||||
$stack->popStyle(new OutputFormatterStyle(null, 'blue', array('blink')));
|
||||
|
||||
$style = $stack->getCurrentStyle();
|
||||
$this->assertEquals('yellow', $style->getForeground());
|
||||
$this->assertEquals('black', $style->getBackground());
|
||||
$this->assertEquals(array('bold'), $style->getOptions());
|
||||
|
||||
$stack->popStyle(new OutputFormatterStyle('yellow', 'black', array('bold')));
|
||||
|
||||
$style = $stack->getCurrentStyle();
|
||||
$this->assertEquals('white', $style->getForeground());
|
||||
$this->assertEquals(null, $style->getBackground());
|
||||
$this->assertEquals(array(), $style->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidPop()
|
||||
{
|
||||
$stack = new OutputFormatterStyleStack();
|
||||
$stack->pushStyle(new OutputFormatterStyle('white', 'black', array('blink', 'bold')));
|
||||
$stack->popStyle(new OutputFormatterStyle('yellow'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user