Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 249 lines (217 sloc) 6.269 kB
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Formatter;
13
14 /**
15 * Formatter class for console output.
16 *
17 * @author Konstantin Kudryashov <ever.zet@gmail.com>
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
18 *
19 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
20 */
21 class OutputFormatter implements OutputFormatterInterface
22 {
9dc6e73 @laszlokorte moved the constant from the interface back into the class
laszlokorte authored
23 /**
24 * The pattern to phrase the format.
25 */
1438f83 @jfsimon [Console] Fixed output formatter regex.
jfsimon authored
26 const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>((?:(?!\\\\?<).)*)#is';
9dc6e73 @laszlokorte moved the constant from the interface back into the class
laszlokorte authored
27
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
28 private $decorated;
29 private $styles = array();
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
30 private $styleStack;
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
31
32 /**
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
33 * Escapes "<" special char in given text.
34 *
35 * @param string $text Text to escape
36 *
37 * @return string Escaped text
38 */
39 public static function escape($text)
40 {
41 return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
42 }
43
44 /**
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
45 * Initializes console output formatter.
46 *
0e67dc7 @fabpot fixed phpdoc @param alignment
fabpot authored
47 * @param Boolean $decorated Whether this formatter should actually decorate strings
48 * @param array $styles Array of "name => FormatterStyle" instances
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
49 *
50 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
51 */
52 public function __construct($decorated = null, array $styles = array())
53 {
54 $this->decorated = (Boolean) $decorated;
55
b9307cb @fabpot [Console] fixed some CS
fabpot authored
56 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
57 $this->setStyle('info', new OutputFormatterStyle('green'));
58 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
59 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
60
61 foreach ($styles as $name => $style) {
62 $this->setStyle($name, $style);
63 }
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
64
65 $this->styleStack = new OutputFormatterStyleStack();
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
66 }
67
68 /**
69 * Sets the decorated flag.
70 *
e2dd5a0 @javiereguiluz [Console][Formatter] Added missing PHPDoc @throws and fixed some mino…
javiereguiluz authored
71 * @param Boolean $decorated Whether to decorate the messages or not
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
72 *
73 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
74 */
75 public function setDecorated($decorated)
76 {
77 $this->decorated = (Boolean) $decorated;
78 }
79
80 /**
81 * Gets the decorated flag.
82 *
83 * @return Boolean true if the output will decorate messages, false otherwise
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
84 *
85 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
86 */
87 public function isDecorated()
88 {
89 return $this->decorated;
90 }
91
92 /**
93 * Sets a new style.
94 *
4ec78d1 @pborreli [Phpdoc] Cleaning/fixing
pborreli authored
95 * @param string $name The style name
96 * @param OutputFormatterStyleInterface $style The style instance
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
97 *
98 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
99 */
100 public function setStyle($name, OutputFormatterStyleInterface $style)
101 {
102 $this->styles[strtolower($name)] = $style;
103 }
104
105 /**
106 * Checks if output formatter has style with specified name.
107 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
108 * @param string $name
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
109 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
110 * @return Boolean
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
111 *
112 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
113 */
114 public function hasStyle($name)
115 {
116 return isset($this->styles[strtolower($name)]);
117 }
118
119 /**
120 * Gets style options from style with specified name.
121 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
122 * @param string $name
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
123 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
124 * @return OutputFormatterStyleInterface
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
125 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
126 * @throws \InvalidArgumentException When style isn't defined
e2dd5a0 @javiereguiluz [Console][Formatter] Added missing PHPDoc @throws and fixed some mino…
javiereguiluz authored
127 *
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
128 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
129 */
130 public function getStyle($name)
131 {
132 if (!$this->hasStyle($name)) {
fc5333c @fabpot fixed CS
fabpot authored
133 throw new \InvalidArgumentException('Undefined style: '.$name);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
134 }
135
136 return $this->styles[strtolower($name)];
137 }
138
139 /**
140 * Formats a message according to the given styles.
141 *
0e67dc7 @fabpot fixed phpdoc @param alignment
fabpot authored
142 * @param string $message The message to style
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
143 *
144 * @return string The styled message
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored
145 *
146 * @api
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
147 */
148 public function format($message)
149 {
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
150 $message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
151
ebe015d @jfsimon [Console] Removed pointless constant.
jfsimon authored
152 return str_replace('\\<', '<', $message);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
153 }
154
155 /**
66b1147 @jfsimon [console] Added style stack getter in formatter.
jfsimon authored
156 * @return OutputFormatterStyleStack
157 */
158 public function getStyleStack()
159 {
160 return $this->styleStack;
161 }
162
163 /**
d8ccb83 @fabpot [Console] refactored style management
fabpot authored
164 * Replaces style of the output.
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
165 *
166 * @param array $match
167 *
168 * @return string The replaced style
169 */
d8ccb83 @fabpot [Console] refactored style management
fabpot authored
170 private function replaceStyle($match)
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
171 {
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
172 // we got "\<" escaped char
ebe015d @jfsimon [Console] Removed pointless constant.
jfsimon authored
173 if ('\\' === $match[1]) {
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
174 return $this->applyCurrentStyle($match[0]);
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
175 }
176
177 if ('' === $match[3]) {
178 if ('/' === $match[2]) {
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
179 // we got "</>" tag
180 $this->styleStack->pop();
181
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
182 return $this->applyCurrentStyle($match[4]);
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
183 }
184
185 // we got "<>" tag
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
186 return '<>'.$this->applyCurrentStyle($match[4]);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
187 }
188
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
189 if (isset($this->styles[strtolower($match[3])])) {
190 $style = $this->styles[strtolower($match[3])];
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
191 } else {
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
192 $style = $this->createStyleFromString($match[3]);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
193
194 if (false === $style) {
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
195 return $this->applyCurrentStyle($match[0]);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
196 }
197 }
198
d502a5c @jfsimon [Console] Implemented '<' escaping.
jfsimon authored
199 if ('/' === $match[2]) {
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
200 $this->styleStack->pop($style);
201 } else {
202 $this->styleStack->push($style);
203 }
204
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
205 return $this->applyCurrentStyle($match[4]);
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
206 }
207
208 /**
28ccfc2 @pborreli [Various] Fixed typos
pborreli authored
209 * Tries to create new style instance from string.
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
210 *
b9307cb @fabpot [Console] fixed some CS
fabpot authored
211 * @param string $string
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
212 *
2e9b47f @fabpot merged 2.0
fabpot authored
213 * @return OutputFormatterStyle|Boolean false if string is not format string
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
214 */
215 private function createStyleFromString($string)
216 {
217 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
218 return false;
219 }
220
221 $style = new OutputFormatterStyle();
222 foreach ($matches as $match) {
223 array_shift($match);
224
225 if ('fg' == $match[0]) {
226 $style->setForeground($match[1]);
227 } elseif ('bg' == $match[0]) {
228 $style->setBackground($match[1]);
229 } else {
230 $style->setOption($match[1]);
231 }
232 }
233
234 return $style;
235 }
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
236
237 /**
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
238 * Applies current style from stack to text, if must be applied.
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
239 *
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
240 * @param string $text Input text
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
241 *
242 * @return string string Styled text
243 */
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
244 private function applyCurrentStyle($text)
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
245 {
1b550ed @jfsimon [Console] Added current style appliance for all styled text.
jfsimon authored
246 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
d29ccab @jfsimon [Console] Updated formatter to use style stack.
jfsimon authored
247 }
6a569a1 @everzet [Console] implemented output formatter to decorate and format output …
everzet authored
248 }
Something went wrong with that request. Please try again.