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;
15 * Formatter class for console output.
17 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class OutputFormatter implements OutputFormatterInterface
24 * The pattern to phrase the format.
26 const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>((?:(?!\\\\?<).)*)#is';
29 private $styles = array();
33 * Escapes "<" special char in given text.
35 * @param string $text Text to escape
37 * @return string Escaped text
39 public static function escape($text)
41 return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
45 * Initializes console output formatter.
47 * @param Boolean $decorated Whether this formatter should actually decorate strings
48 * @param array $styles Array of "name => FormatterStyle" instances
52 public function __construct($decorated = null, array $styles = array())
54 $this->decorated = (Boolean) $decorated;
56 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
57 $this->setStyle('info', new OutputFormatterStyle('green'));
58 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
59 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
61 foreach ($styles as $name => $style) {
62 $this->setStyle($name, $style);
65 $this->styleStack = new OutputFormatterStyleStack();
69 * Sets the decorated flag.
71 * @param Boolean $decorated Whether to decorate the messages or not
75 public function setDecorated($decorated)
77 $this->decorated = (Boolean) $decorated;
81 * Gets the decorated flag.
83 * @return Boolean true if the output will decorate messages, false otherwise
87 public function isDecorated()
89 return $this->decorated;
95 * @param string $name The style name
96 * @param OutputFormatterStyleInterface $style The style instance
100 public function setStyle($name, OutputFormatterStyleInterface $style)
102 $this->styles[strtolower($name)] = $style;
106 * Checks if output formatter has style with specified name.
108 * @param string $name
114 public function hasStyle($name)
116 return isset($this->styles[strtolower($name)]);
120 * Gets style options from style with specified name.
122 * @param string $name
124 * @return OutputFormatterStyleInterface
126 * @throws \InvalidArgumentException When style isn't defined
130 public function getStyle($name)
132 if (!$this->hasStyle($name)) {
133 throw new \InvalidArgumentException('Undefined style: '.$name);
136 return $this->styles[strtolower($name)];
140 * Formats a message according to the given styles.
142 * @param string $message The message to style
144 * @return string The styled message
148 public function format($message)
150 $message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
152 return str_replace('\\<', '<', $message);
156 * @return OutputFormatterStyleStack
158 public function getStyleStack()
160 return $this->styleStack;
164 * Replaces style of the output.
166 * @param array $match
168 * @return string The replaced style
170 private function replaceStyle($match)
172 // we got "\<" escaped char
173 if ('\\' === $match[1]) {
174 return $this->applyCurrentStyle($match[0]);
177 if ('' === $match[3]) {
178 if ('/' === $match[2]) {
180 $this->styleStack->pop();
182 return $this->applyCurrentStyle($match[4]);
186 return '<>'.$this->applyCurrentStyle($match[4]);
189 if (isset($this->styles[strtolower($match[3])])) {
190 $style = $this->styles[strtolower($match[3])];
192 $style = $this->createStyleFromString($match[3]);
194 if (false === $style) {
195 return $this->applyCurrentStyle($match[0]);
199 if ('/' === $match[2]) {
200 $this->styleStack->pop($style);
202 $this->styleStack->push($style);
205 return $this->applyCurrentStyle($match[4]);
209 * Tries to create new style instance from string.
211 * @param string $string
213 * @return OutputFormatterStyle|Boolean false if string is not format string
215 private function createStyleFromString($string)
217 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
221 $style = new OutputFormatterStyle();
222 foreach ($matches as $match) {
225 if ('fg' == $match[0]) {
226 $style->setForeground($match[1]);
227 } elseif ('bg' == $match[0]) {
228 $style->setBackground($match[1]);
230 $style->setOption($match[1]);
238 * Applies current style from stack to text, if must be applied.
240 * @param string $text Input text
242 * @return string string Styled text
244 private function applyCurrentStyle($text)
246 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;