Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Console / Adapter / Posix.php
blob7e5c5059e0c49d2ed5b260586743c8a6e1419d07
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Console\Adapter;
12 use ReflectionClass;
13 use Zend\Console\Charset;
14 use Zend\Console\Exception;
15 use Zend\Console\Color\Xterm256;
16 use Zend\Console\ColorInterface as Color;
18 /**
19 * @todo Add GNU readline support
20 * @link http://en.wikipedia.org/wiki/ANSI_escape_code
22 class Posix extends AbstractAdapter
24 /**
25 * Whether or not mbstring is enabled
27 * @var null|bool
29 protected static $hasMBString;
31 /**
32 * @var Charset\CharsetInterface
34 protected $charset;
36 /**
37 * Map of colors to ANSI codes
39 * @var array
41 protected static $ansiColorMap = array(
42 'fg' => array(
43 Color::NORMAL => '22;39',
44 Color::RESET => '22;39',
46 Color::BLACK => '0;30',
47 Color::RED => '0;31',
48 Color::GREEN => '0;32',
49 Color::YELLOW => '0;33',
50 Color::BLUE => '0;34',
51 Color::MAGENTA => '0;35',
52 Color::CYAN => '0;36',
53 Color::WHITE => '0;37',
55 Color::GRAY => '1;30',
56 Color::LIGHT_RED => '1;31',
57 Color::LIGHT_GREEN => '1;32',
58 Color::LIGHT_YELLOW => '1;33',
59 Color::LIGHT_BLUE => '1;34',
60 Color::LIGHT_MAGENTA => '1;35',
61 Color::LIGHT_CYAN => '1;36',
62 Color::LIGHT_WHITE => '1;37',
64 'bg' => array(
65 Color::NORMAL => '0;49',
66 Color::RESET => '0;49',
68 Color::BLACK => '40',
69 Color::RED => '41',
70 Color::GREEN => '42',
71 Color::YELLOW => '43',
72 Color::BLUE => '44',
73 Color::MAGENTA => '45',
74 Color::CYAN => '46',
75 Color::WHITE => '47',
77 Color::GRAY => '40',
78 Color::LIGHT_RED => '41',
79 Color::LIGHT_GREEN => '42',
80 Color::LIGHT_YELLOW => '43',
81 Color::LIGHT_BLUE => '44',
82 Color::LIGHT_MAGENTA => '45',
83 Color::LIGHT_CYAN => '46',
84 Color::LIGHT_WHITE => '47',
88 /**
89 * Last fetched TTY mode
91 * @var string|null
93 protected $lastTTYMode = null;
95 /**
96 * Write a single line of text to console and advance cursor to the next line.
98 * This override works around a bug in some terminals that cause the background color
99 * to fill the next line after EOL. To remedy this, we are sending the colored string with
100 * appropriate color reset sequences before sending EOL character.
102 * @link https://github.com/zendframework/zf2/issues/4167
103 * @param string $text
104 * @param null|int $color
105 * @param null|int $bgColor
107 public function writeLine($text = "", $color = null, $bgColor = null)
109 $this->write($text, $color, $bgColor);
110 $this->write(PHP_EOL);
114 * Determine and return current console width.
116 * @return int
118 public function getWidth()
120 static $width;
121 if ($width > 0) {
122 return $width;
126 * Try to read env variable
128 if (($result = getenv('COLUMNS')) !== false) {
129 return $width = (int) $result;
133 * Try to read console size from "tput" command
135 $result = exec('tput cols', $output, $return);
136 if (!$return && is_numeric($result)) {
137 return $width = (int) $result;
140 return $width = parent::getWidth();
144 * Determine and return current console height.
146 * @return false|int
148 public function getHeight()
150 static $height;
151 if ($height > 0) {
152 return $height;
155 // Try to read env variable
156 if (($result = getenv('LINES')) !== false) {
157 return $height = (int) $result;
160 // Try to read console size from "tput" command
161 $result = exec('tput lines', $output, $return);
162 if (!$return && is_numeric($result)) {
163 return $height = (int) $result;
166 return $height = parent::getHeight();
170 * Run a mode command and store results
172 * @return void
174 protected function runModeCommand()
176 exec('mode', $output, $return);
177 if ($return || !count($output)) {
178 $this->modeResult = '';
179 } else {
180 $this->modeResult = trim(implode('', $output));
185 * Check if console is UTF-8 compatible
187 * @return bool
189 public function isUtf8()
191 // Try to retrieve it from LANG env variable
192 if (($lang = getenv('LANG')) !== false) {
193 return stristr($lang, 'utf-8') || stristr($lang, 'utf8');
196 return false;
200 * Show console cursor
202 public function showCursor()
204 echo "\x1b[?25h";
208 * Hide console cursor
210 public function hideCursor()
212 echo "\x1b[?25l";
216 * Set cursor position
217 * @param int $x
218 * @param int $y
220 public function setPos($x, $y)
222 echo "\x1b[" . $y . ';' . $x . 'f';
226 * Prepare a string that will be rendered in color.
228 * @param string $string
229 * @param int $color
230 * @param null|int $bgColor
231 * @throws Exception\BadMethodCallException
232 * @return string
234 public function colorize($string, $color = null, $bgColor = null)
236 $color = $this->getColorCode($color, 'fg');
237 $bgColor = $this->getColorCode($bgColor, 'bg');
238 return ($color !== null ? "\x1b[" . $color . 'm' : '')
239 . ($bgColor !== null ? "\x1b[" . $bgColor . 'm' : '')
240 . $string
241 . "\x1b[22;39m\x1b[0;49m";
245 * Change current drawing color.
247 * @param int $color
248 * @throws Exception\BadMethodCallException
250 public function setColor($color)
252 $color = $this->getColorCode($color, 'fg');
253 echo "\x1b[" . $color . 'm';
257 * Change current drawing background color
259 * @param int $bgColor
260 * @throws Exception\BadMethodCallException
262 public function setBgColor($bgColor)
264 $bgColor = $this->getColorCode($bgColor, 'bg');
265 echo "\x1b[" . ($bgColor) . 'm';
269 * Reset color to console default.
271 public function resetColor()
273 echo "\x1b[0;49m"; // reset bg color
274 echo "\x1b[22;39m"; // reset fg bold, bright and faint
275 echo "\x1b[25;39m"; // reset fg blink
276 echo "\x1b[24;39m"; // reset fg underline
280 * Set Console charset to use.
282 * @param Charset\CharsetInterface $charset
284 public function setCharset(Charset\CharsetInterface $charset)
286 $this->charset = $charset;
290 * Get charset currently in use by this adapter.
292 * @return Charset\CharsetInterface $charset
294 public function getCharset()
296 if ($this->charset === null) {
297 $this->charset = $this->getDefaultCharset();
300 return $this->charset;
304 * @return Charset\CharsetInterface
306 public function getDefaultCharset()
308 if ($this->isUtf8()) {
309 return new Charset\Utf8;
311 return new Charset\DECSG();
315 * Read a single character from the console input
317 * @param string|null $mask A list of allowed chars
318 * @return string
320 public function readChar($mask = null)
322 $this->setTTYMode('-icanon -echo');
324 $stream = fopen('php://stdin', 'rb');
325 do {
326 $char = fgetc($stream);
327 } while (strlen($char) !== 1 || ($mask !== null && false === strstr($mask, $char)));
328 fclose($stream);
330 $this->restoreTTYMode();
331 return $char;
335 * Reset color to console default.
337 public function clear()
339 echo "\x1b[2J"; // reset bg color
340 $this->setPos(1, 1); // reset cursor position
344 * Restore TTY (Console) mode to previous value.
346 * @return void
348 protected function restoreTTYMode()
350 if ($this->lastTTYMode === null) {
351 return;
354 shell_exec('stty ' . escapeshellarg($this->lastTTYMode));
358 * Change TTY (Console) mode
360 * @link http://en.wikipedia.org/wiki/Stty
361 * @param $mode
363 protected function setTTYMode($mode)
365 // Store last mode
366 $this->lastTTYMode = trim(`stty -g`);
368 // Set new mode
369 shell_exec('stty '.escapeshellcmd($mode));
373 * Get the final color code and throw exception on error
375 * @param null|int|Xterm256 $color
376 * @throws Exception\BadMethodCallException
377 * @return string
379 protected function getColorCode($color, $type = 'fg')
381 if ($color instanceof Xterm256) {
382 $r = new ReflectionClass($color);
383 $code = $r->getStaticPropertyValue('color');
384 if ($type == 'fg') {
385 $code = sprintf($code, $color::FOREGROUND);
386 } else {
387 $code = sprintf($code, $color::BACKGROUND);
389 return $code;
392 if ($color !== null) {
393 if (!isset(static::$ansiColorMap[$type][$color])) {
394 throw new Exception\BadMethodCallException(sprintf(
395 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate',
396 $color
400 return static::$ansiColorMap[$type][$color];
403 return null;