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_replace("\0", '', $text);
56 $text .= str_repeat("\0", $len - strlen($text));
63 * Initializes console output formatter.
65 * @param bool $decorated Whether this formatter should actually decorate strings
66 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
68 public function __construct($decorated = false, array $styles = array())
70 $this->decorated = (bool) $decorated;
72 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
73 $this->setStyle('info', new OutputFormatterStyle('green'));
74 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
75 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
77 foreach ($styles as $name => $style) {
78 $this->setStyle($name, $style);
81 $this->styleStack = new OutputFormatterStyleStack();
87 public function setDecorated($decorated)
89 $this->decorated = (bool) $decorated;
95 public function isDecorated()
97 return $this->decorated;
103 public function setStyle($name, OutputFormatterStyleInterface $style)
105 $this->styles[strtolower($name)] = $style;
111 public function hasStyle($name)
113 return isset($this->styles[strtolower($name)]);
119 public function getStyle($name)
121 if (!$this->hasStyle($name)) {
122 throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
125 return $this->styles[strtolower($name)];
131 public function format($message)
133 $message = (string) $message;
136 $tagRegex = '[a-z][a-z0-9,_=;-]*+';
137 preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
138 foreach ($matches[0] as $i => $match) {
142 if (0 != $pos && '\\' == $message[$pos - 1]) {
146 // add the text up to the next tag
147 $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
148 $offset = $pos + strlen($text);
151 if ($open = '/' != $text[1]) {
152 $tag = $matches[1][$i][0];
154 $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
157 if (!$open && !$tag) {
159 $this->styleStack->pop();
160 } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
161 $output .= $this->applyCurrentStyle($text);
163 $this->styleStack->push($style);
165 $this->styleStack->pop($style);
169 $output .= $this->applyCurrentStyle(substr($message, $offset));
171 if (false !== strpos($output, "\0")) {
172 return strtr($output, array("\0" => '\\', '\\<' => '<'));
175 return str_replace('\\<', '<', $output);
179 * @return OutputFormatterStyleStack
181 public function getStyleStack()
183 return $this->styleStack;
187 * Tries to create new style instance from string.
189 * @param string $string
191 * @return OutputFormatterStyle|false false if string is not format string
193 private function createStyleFromString($string)
195 if (isset($this->styles[$string])) {
196 return $this->styles[$string];
199 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, PREG_SET_ORDER)) {
203 $style = new OutputFormatterStyle();
204 foreach ($matches as $match) {
207 if ('fg' == $match[0]) {
208 $style->setForeground($match[1]);
209 } elseif ('bg' == $match[0]) {
210 $style->setBackground($match[1]);
211 } elseif ('options' === $match[0]) {
212 preg_match_all('([^,;]+)', $match[1], $options);
213 $options = array_shift($options);
214 foreach ($options as $option) {
216 $style->setOption($option);
217 } catch (\InvalidArgumentException $e) {
218 @trigger_error(sprintf('Unknown style options are deprecated since Symfony 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
232 * Applies current style from stack to text, if must be applied.
234 * @param string $text Input text
236 * @return string Styled text
238 private function applyCurrentStyle($text)
240 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;