4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Formatter;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
17 * Formatter class for console output.
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class OutputFormatter implements OutputFormatterInterface
24 private $styles = array();
28 * Escapes "<" special char in given text.
30 * @param string $text Text to escape
32 * @return string Escaped text
34 public static function escape($text)
36 $text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
38 return self::escapeTrailingBackslash($text);
42 * Escapes trailing "\" in given text.
44 * @param string $text Text to escape
46 * @return string Escaped text
50 public static function escapeTrailingBackslash($text)
52 if ('\\' === substr($text, -1)) {
54 $text = rtrim($text, '\\');
55 $text .= str_repeat('<<', $len - strlen($text));
62 * Initializes console output formatter.
64 * @param bool $decorated Whether this formatter should actually decorate strings
65 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
67 public function __construct($decorated = false, array $styles = array())
69 $this->decorated = (bool) $decorated;
71 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
72 $this->setStyle('info', new OutputFormatterStyle('green'));
73 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
74 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
76 foreach ($styles as $name => $style) {
77 $this->setStyle($name, $style);
80 $this->styleStack = new OutputFormatterStyleStack();
86 public function setDecorated($decorated)
88 $this->decorated = (bool) $decorated;
94 public function isDecorated()
96 return $this->decorated;
102 public function setStyle($name, OutputFormatterStyleInterface $style)
104 $this->styles[strtolower($name)] = $style;
110 public function hasStyle($name)
112 return isset($this->styles[strtolower($name)]);
118 public function getStyle($name)
120 if (!$this->hasStyle($name)) {
121 throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
124 return $this->styles[strtolower($name)];
130 public function format($message)
132 $message = (string) $message;
135 $tagRegex = '[a-z][a-z0-9_=;-]*+';
136 preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
137 foreach ($matches[0] as $i => $match) {
141 if (0 != $pos && '\\' == $message[$pos - 1]) {
145 // add the text up to the next tag
146 $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
147 $offset = $pos + strlen($text);
150 if ($open = '/' != $text[1]) {
151 $tag = $matches[1][$i][0];
153 $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
156 if (!$open && !$tag) {
158 $this->styleStack->pop();
159 } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
160 $output .= $this->applyCurrentStyle($text);
162 $this->styleStack->push($style);
164 $this->styleStack->pop($style);
168 $output .= $this->applyCurrentStyle(substr($message, $offset));
170 if (false !== strpos($output, '<<')) {
171 return strtr($output, array('\\<' => '<', '<<' => '\\'));
174 return str_replace('\\<', '<', $output);
178 * @return OutputFormatterStyleStack
180 public function getStyleStack()
182 return $this->styleStack;
186 * Tries to create new style instance from string.
188 * @param string $string
190 * @return OutputFormatterStyle|false false if string is not format string
192 private function createStyleFromString($string)
194 if (isset($this->styles[$string])) {
195 return $this->styles[$string];
198 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
202 $style = new OutputFormatterStyle();
203 foreach ($matches as $match) {
206 if ('fg' == $match[0]) {
207 $style->setForeground($match[1]);
208 } elseif ('bg' == $match[0]) {
209 $style->setBackground($match[1]);
212 $style->setOption($match[1]);
213 } catch (\InvalidArgumentException $e) {
223 * Applies current style from stack to text, if must be applied.
225 * @param string $text Input text
227 * @return string Styled text
229 private function applyCurrentStyle($text)
231 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;