PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ZF2/library/Zend/Console/Adapter/Posix.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 387 lines | 208 code | 44 blank | 135 comment | 25 complexity | 4c77e5103c4946d09d05812f1386b3e4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Console\Adapter;
  10. use ReflectionClass;
  11. use Zend\Console\Charset;
  12. use Zend\Console\Exception;
  13. use Zend\Console\Color\Xterm256;
  14. use Zend\Console\ColorInterface as Color;
  15. /**
  16. * @todo Add GNU readline support
  17. * @link http://en.wikipedia.org/wiki/ANSI_escape_code
  18. */
  19. class Posix extends AbstractAdapter
  20. {
  21. /**
  22. * Whether or not mbstring is enabled
  23. *
  24. * @var null|bool
  25. */
  26. protected static $hasMBString;
  27. /**
  28. * @var Charset\CharsetInterface
  29. */
  30. protected $charset;
  31. /**
  32. * Map of colors to ANSI codes
  33. *
  34. * @var array
  35. */
  36. protected static $ansiColorMap = array(
  37. 'fg' => array(
  38. Color::NORMAL => '22;39',
  39. Color::RESET => '22;39',
  40. Color::BLACK => '0;30',
  41. Color::RED => '0;31',
  42. Color::GREEN => '0;32',
  43. Color::YELLOW => '0;33',
  44. Color::BLUE => '0;34',
  45. Color::MAGENTA => '0;35',
  46. Color::CYAN => '0;36',
  47. Color::WHITE => '0;37',
  48. Color::GRAY => '1;30',
  49. Color::LIGHT_RED => '1;31',
  50. Color::LIGHT_GREEN => '1;32',
  51. Color::LIGHT_YELLOW => '1;33',
  52. Color::LIGHT_BLUE => '1;34',
  53. Color::LIGHT_MAGENTA => '1;35',
  54. Color::LIGHT_CYAN => '1;36',
  55. Color::LIGHT_WHITE => '1;37',
  56. ),
  57. 'bg' => array(
  58. Color::NORMAL => '0;49',
  59. Color::RESET => '0;49',
  60. Color::BLACK => '40',
  61. Color::RED => '41',
  62. Color::GREEN => '42',
  63. Color::YELLOW => '43',
  64. Color::BLUE => '44',
  65. Color::MAGENTA => '45',
  66. Color::CYAN => '46',
  67. Color::WHITE => '47',
  68. Color::GRAY => '40',
  69. Color::LIGHT_RED => '41',
  70. Color::LIGHT_GREEN => '42',
  71. Color::LIGHT_YELLOW => '43',
  72. Color::LIGHT_BLUE => '44',
  73. Color::LIGHT_MAGENTA => '45',
  74. Color::LIGHT_CYAN => '46',
  75. Color::LIGHT_WHITE => '47',
  76. ),
  77. );
  78. /**
  79. * Last fetched TTY mode
  80. *
  81. * @var string|null
  82. */
  83. protected $lastTTYMode = null;
  84. /**
  85. * Determine and return current console width.
  86. *
  87. * @return int
  88. */
  89. public function getWidth()
  90. {
  91. static $width;
  92. if ($width > 0) {
  93. return $width;
  94. }
  95. /**
  96. * Try to read env variable
  97. */
  98. if (($result = getenv('COLUMNS')) !== false) {
  99. return $width = (int) $result;
  100. }
  101. /**
  102. * Try to read console size from "tput" command
  103. */
  104. $result = exec('tput cols', $output, $return);
  105. if (!$return && is_numeric($result)) {
  106. return $width = (int) $result;
  107. }
  108. return $width = parent::getWidth();
  109. }
  110. /**
  111. * Determine and return current console height.
  112. *
  113. * @return false|int
  114. */
  115. public function getHeight()
  116. {
  117. static $height;
  118. if ($height > 0) {
  119. return $height;
  120. }
  121. // Try to read env variable
  122. if (($result = getenv('LINES')) !== false) {
  123. return $height = (int) $result;
  124. }
  125. // Try to read console size from "tput" command
  126. $result = exec('tput lines', $output, $return);
  127. if (!$return && is_numeric($result)) {
  128. return $height = (int) $result;
  129. }
  130. return $height = parent::getHeight();
  131. }
  132. /**
  133. * Run a mode command and store results
  134. *
  135. * @return void
  136. */
  137. protected function runModeCommand()
  138. {
  139. exec('mode', $output, $return);
  140. if ($return || !count($output)) {
  141. $this->modeResult = '';
  142. } else {
  143. $this->modeResult = trim(implode('', $output));
  144. }
  145. }
  146. /**
  147. * Check if console is UTF-8 compatible
  148. *
  149. * @return bool
  150. */
  151. public function isUtf8()
  152. {
  153. // Try to retrieve it from LANG env variable
  154. if (($lang = getenv('LANG')) !== false) {
  155. return stristr($lang, 'utf-8') || stristr($lang, 'utf8');
  156. }
  157. return false;
  158. }
  159. /**
  160. * Show console cursor
  161. */
  162. public function showCursor()
  163. {
  164. echo "\x1b[?25h";
  165. }
  166. /**
  167. * Hide console cursor
  168. */
  169. public function hideCursor()
  170. {
  171. echo "\x1b[?25l";
  172. }
  173. /**
  174. * Set cursor position
  175. * @param int $x
  176. * @param int $y
  177. */
  178. public function setPos($x, $y)
  179. {
  180. echo "\x1b[" . $y . ';' . $x . 'f';
  181. }
  182. /**
  183. * Prepare a string that will be rendered in color.
  184. *
  185. * @param string $string
  186. * @param int $color
  187. * @param null|int $bgColor
  188. * @throws Exception\BadMethodCallException
  189. * @return string
  190. */
  191. public function colorize($string, $color = null, $bgColor = null)
  192. {
  193. $color = $this->getColorCode($color, 'fg');
  194. $bgColor = $this->getColorCode($bgColor, 'bg');
  195. return ($color !== null ? "\x1b[" . $color . 'm' : '')
  196. . ($bgColor !== null ? "\x1b[" . $bgColor . 'm' : '')
  197. . $string
  198. . "\x1b[22;39m\x1b[0;49m";
  199. }
  200. /**
  201. * Change current drawing color.
  202. *
  203. * @param int $color
  204. * @throws Exception\BadMethodCallException
  205. */
  206. public function setColor($color)
  207. {
  208. $color = $this->getColorCode($color, 'fg');
  209. echo "\x1b[" . $color . 'm';
  210. }
  211. /**
  212. * Change current drawing background color
  213. *
  214. * @param int $bgColor
  215. * @throws Exception\BadMethodCallException
  216. */
  217. public function setBgColor($bgColor)
  218. {
  219. $bgColor = $this->getColorCode($bgColor, 'bg');
  220. echo "\x1b[" . ($bgColor) . 'm';
  221. }
  222. /**
  223. * Reset color to console default.
  224. */
  225. public function resetColor()
  226. {
  227. echo "\x1b[0;49m"; // reset bg color
  228. echo "\x1b[22;39m"; // reset fg bold, bright and faint
  229. echo "\x1b[25;39m"; // reset fg blink
  230. echo "\x1b[24;39m"; // reset fg underline
  231. }
  232. /**
  233. * Set Console charset to use.
  234. *
  235. * @param Charset\CharsetInterface $charset
  236. */
  237. public function setCharset(Charset\CharsetInterface $charset)
  238. {
  239. $this->charset = $charset;
  240. }
  241. /**
  242. * Get charset currently in use by this adapter.
  243. *
  244. * @return Charset\CharsetInterface $charset
  245. */
  246. public function getCharset()
  247. {
  248. if ($this->charset === null) {
  249. $this->charset = $this->getDefaultCharset();
  250. }
  251. return $this->charset;
  252. }
  253. /**
  254. * @return Charset\CharsetInterface
  255. */
  256. public function getDefaultCharset()
  257. {
  258. if ($this->isUtf8()) {
  259. return new Charset\Utf8;
  260. }
  261. return new Charset\DECSG();
  262. }
  263. /**
  264. * Read a single character from the console input
  265. *
  266. * @param string|null $mask A list of allowed chars
  267. * @return string
  268. */
  269. public function readChar($mask = null)
  270. {
  271. $this->setTTYMode('-icanon -echo');
  272. $stream = fopen('php://stdin', 'rb');
  273. do {
  274. $char = fgetc($stream);
  275. } while (strlen($char) !== 1 || ($mask !== null && false === strstr($mask, $char)));
  276. fclose($stream);
  277. $this->restoreTTYMode();
  278. return $char;
  279. }
  280. /**
  281. * Reset color to console default.
  282. */
  283. public function clear()
  284. {
  285. echo "\x1b[2J"; // reset bg color
  286. $this->setPos(1, 1); // reset cursor position
  287. }
  288. /**
  289. * Restore TTY (Console) mode to previous value.
  290. *
  291. * @return void
  292. */
  293. protected function restoreTTYMode()
  294. {
  295. if ($this->lastTTYMode === null) {
  296. return;
  297. }
  298. shell_exec('stty ' . escapeshellarg($this->lastTTYMode));
  299. }
  300. /**
  301. * Change TTY (Console) mode
  302. *
  303. * @link http://en.wikipedia.org/wiki/Stty
  304. * @param $mode
  305. */
  306. protected function setTTYMode($mode)
  307. {
  308. // Store last mode
  309. $this->lastTTYMode = trim(`stty -g`);
  310. // Set new mode
  311. shell_exec('stty '.escapeshellcmd($mode));
  312. }
  313. /**
  314. * Get the final color code and throw exception on error
  315. *
  316. * @param null|int|Xterm256 $color
  317. * @throws Exception\BadMethodCallException
  318. * @return string
  319. */
  320. protected function getColorCode($color, $type = 'fg')
  321. {
  322. if ($color instanceof Xterm256) {
  323. $r = new ReflectionClass($color);
  324. $code = $r->getStaticPropertyValue('color');
  325. if ($type == 'fg') {
  326. $code = sprintf($code, $color::FOREGROUND);
  327. } else {
  328. $code = sprintf($code, $color::BACKGROUND);
  329. }
  330. return $code;
  331. }
  332. if ($color !== null) {
  333. if (!isset(static::$ansiColorMap[$type][$color])) {
  334. throw new Exception\BadMethodCallException(sprintf(
  335. 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate',
  336. $color
  337. ));
  338. }
  339. return static::$ansiColorMap[$type][$color];
  340. }
  341. return null;
  342. }
  343. }