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();
84 * Sets the decorated flag.
86 * @param bool $decorated Whether to decorate the messages or not
88 public function setDecorated($decorated)
90 $this->decorated = (bool) $decorated;
94 * Gets the decorated flag.
96 * @return bool true if the output will decorate messages, false otherwise
98 public function isDecorated()
100 return $this->decorated;
106 * @param string $name The style name
107 * @param OutputFormatterStyleInterface $style The style instance
109 public function setStyle($name, OutputFormatterStyleInterface $style)
111 $this->styles[strtolower($name)] = $style;
115 * Checks if output formatter has style with specified name.
117 * @param string $name
121 public function hasStyle($name)
123 return isset($this->styles[strtolower($name)]);
127 * Gets style options from style with specified name.
129 * @param string $name
131 * @return OutputFormatterStyleInterface
133 * @throws InvalidArgumentException When style isn't defined
135 public function getStyle($name)
137 if (!$this->hasStyle($name)) {
138 throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
141 return $this->styles[strtolower($name)];
145 * Formats a message according to the given styles.
147 * @param string $message The message to style
149 * @return string The styled message
151 public function format($message)
153 $message = (string) $message;
156 $tagRegex = '[a-z][a-z0-9_=;-]*+';
157 preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
158 foreach ($matches[0] as $i => $match) {
162 if (0 != $pos && '\\' == $message[$pos - 1]) {
166 // add the text up to the next tag
167 $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
168 $offset = $pos + strlen($text);
171 if ($open = '/' != $text[1]) {
172 $tag = $matches[1][$i][0];
174 $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
177 if (!$open && !$tag) {
179 $this->styleStack->pop();
180 } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
181 $output .= $this->applyCurrentStyle($text);
183 $this->styleStack->push($style);
185 $this->styleStack->pop($style);
189 $output .= $this->applyCurrentStyle(substr($message, $offset));
191 if (false !== strpos($output, '<<')) {
192 return strtr($output, array('\\<' => '<', '<<' => '\\'));
195 return str_replace('\\<', '<', $output);
199 * @return OutputFormatterStyleStack
201 public function getStyleStack()
203 return $this->styleStack;
207 * Tries to create new style instance from string.
209 * @param string $string
211 * @return OutputFormatterStyle|false false if string is not format string
213 private function createStyleFromString($string)
215 if (isset($this->styles[$string])) {
216 return $this->styles[$string];
219 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
223 $style = new OutputFormatterStyle();
224 foreach ($matches as $match) {
227 if ('fg' == $match[0]) {
228 $style->setForeground($match[1]);
229 } elseif ('bg' == $match[0]) {
230 $style->setBackground($match[1]);
233 $style->setOption($match[1]);
234 } catch (\InvalidArgumentException $e) {
244 * Applies current style from stack to text, if must be applied.
246 * @param string $text Input text
248 * @return string Styled text
250 private function applyCurrentStyle($text)
252 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;