PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Console/ConsoleOutput.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 287 lines | 129 code | 17 blank | 141 comment | 14 complexity | 81cb04fef51582fd4052e5ad94e45c48 MD5 | raw file
  1. <?php
  2. /**
  3. * ConsoleOutput file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. * Raw output constant - no modification of output text.
  48. */
  49. const RAW = 0;
  50. /**
  51. * Plain output - tags will be stripped.
  52. */
  53. const PLAIN = 1;
  54. /**
  55. * Color output - Convert known tags in to ANSI color escape codes.
  56. */
  57. const COLOR = 2;
  58. /**
  59. * Constant for a newline.
  60. */
  61. const LF = PHP_EOL;
  62. /**
  63. * File handle for output.
  64. *
  65. * @var resource
  66. */
  67. protected $_output;
  68. /**
  69. * The current output type. Manipulated with ConsoleOutput::outputAs();
  70. *
  71. * @var integer.
  72. */
  73. protected $_outputAs = self::COLOR;
  74. /**
  75. * text colors used in colored output.
  76. *
  77. * @var array
  78. */
  79. protected static $_foregroundColors = array(
  80. 'black' => 30,
  81. 'red' => 31,
  82. 'green' => 32,
  83. 'yellow' => 33,
  84. 'blue' => 34,
  85. 'magenta' => 35,
  86. 'cyan' => 36,
  87. 'white' => 37
  88. );
  89. /**
  90. * background colors used in colored output.
  91. *
  92. * @var array
  93. */
  94. protected static $_backgroundColors = array(
  95. 'black' => 40,
  96. 'red' => 41,
  97. 'green' => 42,
  98. 'yellow' => 43,
  99. 'blue' => 44,
  100. 'magenta' => 45,
  101. 'cyan' => 46,
  102. 'white' => 47
  103. );
  104. /**
  105. * formatting options for colored output
  106. *
  107. * @var string
  108. */
  109. protected static $_options = array(
  110. 'bold' => 1,
  111. 'underline' => 4,
  112. 'blink' => 5,
  113. 'reverse' => 7,
  114. );
  115. /**
  116. * Styles that are available as tags in console output.
  117. * You can modify these styles with ConsoleOutput::styles()
  118. *
  119. * @var array
  120. */
  121. protected static $_styles = array(
  122. 'error' => array('text' => 'red', 'underline' => true),
  123. 'warning' => array('text' => 'yellow'),
  124. 'info' => array('text' => 'cyan'),
  125. 'success' => array('text' => 'green'),
  126. 'comment' => array('text' => 'blue'),
  127. 'question' => array('text' => "magenta"),
  128. );
  129. /**
  130. * Construct the output object.
  131. *
  132. * Checks for a pretty console environment. Ansicon allows pretty consoles
  133. * on windows, and is supported.
  134. *
  135. * @param string $stream The identifier of the stream to write output to.
  136. */
  137. public function __construct($stream = 'php://stdout') {
  138. $this->_output = fopen($stream, 'w');
  139. if (DS == '\\' && !(bool)env('ANSICON')) {
  140. $this->_outputAs = self::PLAIN;
  141. }
  142. }
  143. /**
  144. * Outputs a single or multiple messages to stdout. If no parameters
  145. * are passed, outputs just a newline.
  146. *
  147. * @param mixed $message A string or a an array of strings to output
  148. * @param integer $newlines Number of newlines to append
  149. * @return integer Returns the number of bytes returned from writing to stdout.
  150. */
  151. public function write($message, $newlines = 1) {
  152. if (is_array($message)) {
  153. $message = implode(self::LF, $message);
  154. }
  155. return $this->_write($this->styleText($message . str_repeat(self::LF, $newlines)));
  156. }
  157. /**
  158. * Apply styling to text.
  159. *
  160. * @param string $text Text with styling tags.
  161. * @return string String with color codes added.
  162. */
  163. public function styleText($text) {
  164. if ($this->_outputAs == self::RAW) {
  165. return $text;
  166. }
  167. if ($this->_outputAs == self::PLAIN) {
  168. $tags = implode('|', array_keys(self::$_styles));
  169. return preg_replace('#</?(?:' . $tags . ')>#', '', $text);
  170. }
  171. return preg_replace_callback(
  172. '/<(?<tag>[a-z0-9-_]+)>(?<text>.*?)<\/(\1)>/ims', array($this, '_replaceTags'), $text
  173. );
  174. }
  175. /**
  176. * Replace tags with color codes.
  177. *
  178. * @param array $matches.
  179. * @return string
  180. */
  181. protected function _replaceTags($matches) {
  182. $style = $this->styles($matches['tag']);
  183. if (empty($style)) {
  184. return '<' . $matches['tag'] . '>' . $matches['text'] . '</' . $matches['tag'] . '>';
  185. }
  186. $styleInfo = array();
  187. if (!empty($style['text']) && isset(self::$_foregroundColors[$style['text']])) {
  188. $styleInfo[] = self::$_foregroundColors[$style['text']];
  189. }
  190. if (!empty($style['background']) && isset(self::$_backgroundColors[$style['background']])) {
  191. $styleInfo[] = self::$_backgroundColors[$style['background']];
  192. }
  193. unset($style['text'], $style['background']);
  194. foreach ($style as $option => $value) {
  195. if ($value) {
  196. $styleInfo[] = self::$_options[$option];
  197. }
  198. }
  199. return "\033[" . implode($styleInfo, ';') . 'm' . $matches['text'] . "\033[0m";
  200. }
  201. /**
  202. * Writes a message to the output stream.
  203. *
  204. * @param string $message Message to write.
  205. * @return boolean success
  206. */
  207. protected function _write($message) {
  208. return fwrite($this->_output, $message);
  209. }
  210. /**
  211. * Get the current styles offered, or append new ones in.
  212. *
  213. * ### Get a style definition
  214. *
  215. * `$this->output->styles('error');`
  216. *
  217. * ### Get all the style definitions
  218. *
  219. * `$this->output->styles();`
  220. *
  221. * ### Create or modify an existing style
  222. *
  223. * `$this->output->styles('annoy', array('text' => 'purple', 'background' => 'yellow', 'blink' => true));`
  224. *
  225. * ### Remove a style
  226. *
  227. * `$this->output->styles('annoy', false);`
  228. *
  229. * @param string $style The style to get or create.
  230. * @param mixed $definition The array definition of the style to change or create a style
  231. * or false to remove a style.
  232. * @return mixed If you are getting styles, the style or null will be returned. If you are creating/modifying
  233. * styles true will be returned.
  234. */
  235. public function styles($style = null, $definition = null) {
  236. if ($style === null && $definition === null) {
  237. return self::$_styles;
  238. }
  239. if (is_string($style) && $definition === null) {
  240. return isset(self::$_styles[$style]) ? self::$_styles[$style] : null;
  241. }
  242. if ($definition === false) {
  243. unset(self::$_styles[$style]);
  244. return true;
  245. }
  246. self::$_styles[$style] = $definition;
  247. return true;
  248. }
  249. /**
  250. * Get/Set the output type to use. The output type how formatting tags are treated.
  251. *
  252. * @param integer $type The output type to use. Should be one of the class constants.
  253. * @return mixed Either null or the value if getting.
  254. */
  255. public function outputAs($type = null) {
  256. if ($type === null) {
  257. return $this->_outputAs;
  258. }
  259. $this->_outputAs = $type;
  260. }
  261. /**
  262. * clean up and close handles
  263. *
  264. */
  265. public function __destruct() {
  266. fclose($this->_output);
  267. }
  268. }