1: <?php
2: /**
3: * ConsoleOutput file.
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @since CakePHP(tm) v 2.0
15: * @license http://www.opensource.org/licenses/mit-license.php MIT License
16: */
17:
18: /**
19: * Object wrapper for outputting information from a shell application.
20: * Can be connected to any stream resource that can be used with fopen()
21: *
22: * Can generate colorized output on consoles that support it. There are a few
23: * built in styles
24: *
25: * - `error` Error messages.
26: * - `warning` Warning messages.
27: * - `info` Informational messages.
28: * - `comment` Additional text.
29: * - `question` Magenta text used for user prompts
30: *
31: * By defining styles with addStyle() you can create custom console styles.
32: *
33: * ### Using styles in output
34: *
35: * You can format console output using tags with the name of the style to apply. From inside a shell object
36: *
37: * `$this->out('<warning>Overwrite:</warning> foo.php was overwritten.');`
38: *
39: * This would create orange 'Overwrite:' text, while the rest of the text would remain the normal color.
40: * See ConsoleOutput::styles() to learn more about defining your own styles. Nested styles are not supported
41: * at this time.
42: *
43: * @package Cake.Console
44: */
45: class ConsoleOutput {
46:
47: /**
48: * Raw output constant - no modification of output text.
49: *
50: * @var int
51: */
52: const RAW = 0;
53:
54: /**
55: * Plain output - tags will be stripped.
56: *
57: * @var int
58: */
59: const PLAIN = 1;
60:
61: /**
62: * Color output - Convert known tags in to ANSI color escape codes.
63: *
64: * @var int
65: */
66: const COLOR = 2;
67:
68: /**
69: * Constant for a newline.
70: *
71: * @var string
72: */
73: const LF = PHP_EOL;
74:
75: /**
76: * File handle for output.
77: *
78: * @var resource
79: */
80: protected $_output;
81:
82: /**
83: * The current output type. Manipulated with ConsoleOutput::outputAs();
84: *
85: * @var int
86: */
87: protected $_outputAs = self::COLOR;
88:
89: /**
90: * text colors used in colored output.
91: *
92: * @var array
93: */
94: protected static $_foregroundColors = array(
95: 'black' => 30,
96: 'red' => 31,
97: 'green' => 32,
98: 'yellow' => 33,
99: 'blue' => 34,
100: 'magenta' => 35,
101: 'cyan' => 36,
102: 'white' => 37
103: );
104:
105: /**
106: * background colors used in colored output.
107: *
108: * @var array
109: */
110: protected static $_backgroundColors = array(
111: 'black' => 40,
112: 'red' => 41,
113: 'green' => 42,
114: 'yellow' => 43,
115: 'blue' => 44,
116: 'magenta' => 45,
117: 'cyan' => 46,
118: 'white' => 47
119: );
120:
121: /**
122: * formatting options for colored output
123: *
124: * @var string
125: */
126: protected static $_options = array(
127: 'bold' => 1,
128: 'underline' => 4,
129: 'blink' => 5,
130: 'reverse' => 7,
131: );
132:
133: /**
134: * Styles that are available as tags in console output.
135: * You can modify these styles with ConsoleOutput::styles()
136: *
137: * @var array
138: */
139: protected static $_styles = array(
140: 'emergency' => array('text' => 'red', 'underline' => true),
141: 'alert' => array('text' => 'red', 'underline' => true),
142: 'critical' => array('text' => 'red', 'underline' => true),
143: 'error' => array('text' => 'red', 'underline' => true),
144: 'warning' => array('text' => 'yellow'),
145: 'info' => array('text' => 'cyan'),
146: 'debug' => array('text' => 'yellow'),
147: 'success' => array('text' => 'green'),
148: 'comment' => array('text' => 'blue'),
149: 'question' => array('text' => 'magenta'),
150: 'notice' => array('text' => 'cyan')
151: );
152:
153: /**
154: * Construct the output object.
155: *
156: * Checks for a pretty console environment. Ansicon allows pretty consoles
157: * on windows, and is supported.
158: *
159: * @param string $stream The identifier of the stream to write output to.
160: */
161: public function __construct($stream = 'php://stdout') {
162: $this->_output = fopen($stream, 'w');
163:
164: if ((DS === '\\' && !(bool)env('ANSICON')) ||
165: (function_exists('posix_isatty') && !posix_isatty($this->_output))
166: ) {
167: $this->_outputAs = self::PLAIN;
168: }
169: }
170:
171: /**
172: * Outputs a single or multiple messages to stdout. If no parameters
173: * are passed, outputs just a newline.
174: *
175: * @param string|array $message A string or an array of strings to output
176: * @param int $newlines Number of newlines to append
177: * @return int Returns the number of bytes returned from writing to stdout.
178: */
179: public function write($message, $newlines = 1) {
180: if (is_array($message)) {
181: $message = implode(self::LF, $message);
182: }
183: return $this->_write($this->styleText($message . str_repeat(self::LF, $newlines)));
184: }
185:
186: /**
187: * Apply styling to text.
188: *
189: * @param string $text Text with styling tags.
190: * @return string String with color codes added.
191: */
192: public function styleText($text) {
193: if ($this->_outputAs == self::RAW) {
194: return $text;
195: }
196: if ($this->_outputAs == self::PLAIN) {
197: $tags = implode('|', array_keys(self::$_styles));
198: return preg_replace('#</?(?:' . $tags . ')>#', '', $text);
199: }
200: return preg_replace_callback(
201: '/<(?P<tag>[a-z0-9-_]+)>(?P<text>.*?)<\/(\1)>/ims', array($this, '_replaceTags'), $text
202: );
203: }
204:
205: /**
206: * Replace tags with color codes.
207: *
208: * @param array $matches An array of matches to replace.
209: * @return string
210: */
211: protected function _replaceTags($matches) {
212: $style = $this->styles($matches['tag']);
213: if (empty($style)) {
214: return '<' . $matches['tag'] . '>' . $matches['text'] . '</' . $matches['tag'] . '>';
215: }
216:
217: $styleInfo = array();
218: if (!empty($style['text']) && isset(self::$_foregroundColors[$style['text']])) {
219: $styleInfo[] = self::$_foregroundColors[$style['text']];
220: }
221: if (!empty($style['background']) && isset(self::$_backgroundColors[$style['background']])) {
222: $styleInfo[] = self::$_backgroundColors[$style['background']];
223: }
224: unset($style['text'], $style['background']);
225: foreach ($style as $option => $value) {
226: if ($value) {
227: $styleInfo[] = self::$_options[$option];
228: }
229: }
230: return "\033[" . implode($styleInfo, ';') . 'm' . $matches['text'] . "\033[0m";
231: }
232:
233: /**
234: * Writes a message to the output stream.
235: *
236: * @param string $message Message to write.
237: * @return bool success
238: */
239: protected function _write($message) {
240: return fwrite($this->_output, $message);
241: }
242:
243: /**
244: * Get the current styles offered, or append new ones in.
245: *
246: * ### Get a style definition
247: *
248: * `$this->output->styles('error');`
249: *
250: * ### Get all the style definitions
251: *
252: * `$this->output->styles();`
253: *
254: * ### Create or modify an existing style
255: *
256: * `$this->output->styles('annoy', array('text' => 'purple', 'background' => 'yellow', 'blink' => true));`
257: *
258: * ### Remove a style
259: *
260: * `$this->output->styles('annoy', false);`
261: *
262: * @param string $style The style to get or create.
263: * @param array $definition The array definition of the style to change or create a style
264: * or false to remove a style.
265: * @return mixed If you are getting styles, the style or null will be returned. If you are creating/modifying
266: * styles true will be returned.
267: */
268: public function styles($style = null, $definition = null) {
269: if ($style === null && $definition === null) {
270: return self::$_styles;
271: }
272: if (is_string($style) && $definition === null) {
273: return isset(self::$_styles[$style]) ? self::$_styles[$style] : null;
274: }
275: if ($definition === false) {
276: unset(self::$_styles[$style]);
277: return true;
278: }
279: self::$_styles[$style] = $definition;
280: return true;
281: }
282:
283: /**
284: * Get/Set the output type to use. The output type how formatting tags are treated.
285: *
286: * @param int $type The output type to use. Should be one of the class constants.
287: * @return mixed Either null or the value if getting.
288: */
289: public function outputAs($type = null) {
290: if ($type === null) {
291: return $this->_outputAs;
292: }
293: $this->_outputAs = $type;
294: }
295:
296: /**
297: * Clean up and close handles
298: */
299: public function __destruct() {
300: if (is_resource($this->_output)) {
301: fclose($this->_output);
302: }
303: }
304:
305: }
306: