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 / AbstractAdapter.php
blob5cf5ae9d2732279fbd27b51eccdaa58a1fb7c694
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 Zend\Console\Charset;
13 use Zend\Console\Exception;
14 use Zend\Stdlib\StringUtils;
16 /**
17 * Common console adapter codebase
19 abstract class AbstractAdapter implements AdapterInterface
21 /**
22 * Whether or not mbstring is enabled
24 * @var null|bool
26 protected static $hasMBString;
28 /**
29 * @var Charset\CharsetInterface
31 protected $charset;
33 /**
34 * Current cursor X position
36 * @var int
38 protected $posX;
40 /**
41 * Current cursor Y position
43 * @var int
45 protected $posY;
47 /**
48 * Write a chunk of text to console.
50 * @param string $text
51 * @param null|int $color
52 * @param null|int $bgColor
54 public function write($text, $color = null, $bgColor = null)
56 if ($color !== null || $bgColor !== null) {
57 echo $this->colorize($text, $color, $bgColor);
58 } else {
59 echo $text;
63 /**
64 * Alias for write()
66 * @param string $text
67 * @param null|int $color
68 * @param null|int $bgColor
70 public function writeText($text, $color = null, $bgColor = null)
72 return $this->write($text, $color, $bgColor);
75 /**
76 * Write a single line of text to console and advance cursor to the next line.
78 * @param string $text
79 * @param null|int $color
80 * @param null|int $bgColor
82 public function writeLine($text = "", $color = null, $bgColor = null)
84 $this->write($text . PHP_EOL, $color, $bgColor);
87 /**
88 * Write a piece of text at the coordinates of $x and $y
91 * @param string $text Text to write
92 * @param int $x Console X coordinate (column)
93 * @param int $y Console Y coordinate (row)
94 * @param null|int $color
95 * @param null|int $bgColor
97 public function writeAt($text, $x, $y, $color = null, $bgColor = null)
99 $this->setPos($x, $y);
100 $this->write($text, $color, $bgColor);
104 * Write a box at the specified coordinates.
105 * If X or Y coordinate value is negative, it will be calculated as the distance from far right or bottom edge
106 * of the console (respectively).
108 * @param int $x1 Top-left corner X coordinate (column)
109 * @param int $y1 Top-left corner Y coordinate (row)
110 * @param int $x2 Bottom-right corner X coordinate (column)
111 * @param int $y2 Bottom-right corner Y coordinate (row)
112 * @param int $lineStyle (optional) Box border style.
113 * @param int $fillStyle (optional) Box fill style or a single character to fill it with.
114 * @param int $color (optional) Foreground color
115 * @param int $bgColor (optional) Background color
116 * @param null|int $fillColor (optional) Foreground color of box fill
117 * @param null|int $fillBgColor (optional) Background color of box fill
118 * @throws Exception\BadMethodCallException if coordinates are invalid
120 public function writeBox(
121 $x1,
122 $y1,
123 $x2,
124 $y2,
125 $lineStyle = self::LINE_SINGLE,
126 $fillStyle = self::FILL_NONE,
127 $color = null,
128 $bgColor = null,
129 $fillColor = null,
130 $fillBgColor = null
132 // Sanitize coordinates
133 $x1 = (int) $x1;
134 $y1 = (int) $y1;
135 $x2 = (int) $x2;
136 $y2 = (int) $y2;
138 // Translate negative coordinates
139 if ($x2 < 0) {
140 $x2 = $this->getWidth() - $x2;
143 if ($y2 < 0) {
144 $y2 = $this->getHeight() - $y2;
147 // Validate coordinates
148 if ($x1 < 0
149 || $y1 < 0
150 || $x2 < $x1
151 || $y2 < $y1
153 throw new Exception\BadMethodCallException('Supplied X,Y coordinates are invalid.');
156 // Determine charset and dimensions
157 $charset = $this->getCharset();
158 $width = $x2 - $x1 + 1;
159 $height = $y2 - $y1 + 1;
161 if ($width <= 2) {
162 $lineStyle = static::LINE_NONE;
165 // Activate line drawing
166 $this->write($charset::ACTIVATE);
168 // Draw horizontal lines
169 if ($lineStyle !== static::LINE_NONE) {
170 switch ($lineStyle) {
171 case static::LINE_SINGLE:
172 $lineChar = $charset::LINE_SINGLE_EW;
173 break;
175 case static::LINE_DOUBLE:
176 $lineChar = $charset::LINE_DOUBLE_EW;
177 break;
179 case static::LINE_BLOCK:
180 default:
181 $lineChar = $charset::LINE_BLOCK_EW;
182 break;
185 $this->setPos($x1 + 1, $y1);
186 $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
187 $this->setPos($x1 + 1, $y2);
188 $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
191 // Draw vertical lines and fill
192 if (is_numeric($fillStyle)
193 && $fillStyle !== static::FILL_NONE) {
195 switch ($fillStyle) {
196 case static::FILL_SHADE_LIGHT:
197 $fillChar = $charset::SHADE_LIGHT;
198 break;
199 case static::FILL_SHADE_MEDIUM:
200 $fillChar = $charset::SHADE_MEDIUM;
201 break;
202 case static::FILL_SHADE_DARK:
203 $fillChar = $charset::SHADE_DARK;
204 break;
205 case static::FILL_BLOCK:
206 default:
207 $fillChar = $charset::BLOCK;
208 break;
211 } elseif ($fillStyle) {
212 $fillChar = StringUtils::getWrapper()->substr($fillStyle, 0, 1);
213 } else {
214 $fillChar = ' ';
217 if ($lineStyle === static::LINE_NONE) {
218 for ($y = $y1; $y <= $y2; $y++) {
219 $this->setPos($x1, $y);
220 $this->write(str_repeat($fillChar, $width), $fillColor, $fillBgColor);
222 } else {
223 switch ($lineStyle) {
224 case static::LINE_DOUBLE:
225 $lineChar = $charset::LINE_DOUBLE_NS;
226 break;
227 case static::LINE_BLOCK:
228 $lineChar = $charset::LINE_BLOCK_NS;
229 break;
230 case static::LINE_SINGLE:
231 default:
232 $lineChar = $charset::LINE_SINGLE_NS;
233 break;
236 for ($y = $y1 + 1; $y < $y2; $y++) {
237 $this->setPos($x1, $y);
238 $this->write($lineChar, $color, $bgColor);
239 $this->write(str_repeat($fillChar, $width - 2), $fillColor, $fillBgColor);
240 $this->write($lineChar, $color, $bgColor);
245 // Draw corners
246 if ($lineStyle !== static::LINE_NONE) {
247 if ($color !== null) {
248 $this->setColor($color);
250 if ($bgColor !== null) {
251 $this->setBgColor($bgColor);
253 if ($lineStyle === static::LINE_SINGLE) {
254 $this->writeAt($charset::LINE_SINGLE_NW, $x1, $y1);
255 $this->writeAt($charset::LINE_SINGLE_NE, $x2, $y1);
256 $this->writeAt($charset::LINE_SINGLE_SE, $x2, $y2);
257 $this->writeAt($charset::LINE_SINGLE_SW, $x1, $y2);
258 } elseif ($lineStyle === static::LINE_DOUBLE) {
259 $this->writeAt($charset::LINE_DOUBLE_NW, $x1, $y1);
260 $this->writeAt($charset::LINE_DOUBLE_NE, $x2, $y1);
261 $this->writeAt($charset::LINE_DOUBLE_SE, $x2, $y2);
262 $this->writeAt($charset::LINE_DOUBLE_SW, $x1, $y2);
263 } elseif ($lineStyle === static::LINE_BLOCK) {
264 $this->writeAt($charset::LINE_BLOCK_NW, $x1, $y1);
265 $this->writeAt($charset::LINE_BLOCK_NE, $x2, $y1);
266 $this->writeAt($charset::LINE_BLOCK_SE, $x2, $y2);
267 $this->writeAt($charset::LINE_BLOCK_SW, $x1, $y2);
271 // Deactivate line drawing and reset colors
272 $this->write($charset::DEACTIVATE);
273 $this->resetColor();
277 * Write a block of text at the given coordinates, matching the supplied width and height.
278 * In case a line of text does not fit desired width, it will be wrapped to the next line.
279 * In case the whole text does not fit in desired height, it will be truncated.
281 * @param string $text Text to write
282 * @param int $width Maximum block width. Negative value means distance from right edge.
283 * @param int|null $height Maximum block height. Negative value means distance from bottom edge.
284 * @param int $x Block X coordinate (column)
285 * @param int $y Block Y coordinate (row)
286 * @param null|int $color (optional) Text color
287 * @param null|int $bgColor (optional) Text background color
289 public function writeTextBlock(
290 $text,
291 $width,
292 $height = null,
293 $x = 0,
294 $y = 0,
295 $color = null,
296 $bgColor = null
301 * Determine and return current console width.
303 * @return int
305 public function getWidth()
307 return 80;
311 * Determine and return current console height.
313 * @return int
315 public function getHeight()
317 return 25;
321 * Determine and return current console width and height.
323 * @return array array($width, $height)
325 public function getSize()
327 return array(
328 $this->getWidth(),
329 $this->getHeight(),
334 * Check if console is UTF-8 compatible
336 * @return bool
338 public function isUtf8()
340 return true;
344 * Set cursor position
346 * @param int $x
347 * @param int $y
349 public function setPos($x, $y)
354 * Show console cursor
356 public function showCursor()
361 * Hide console cursor
363 public function hideCursor()
368 * Return current console window title.
370 * @return string
372 public function getTitle()
374 return '';
378 * Prepare a string that will be rendered in color.
380 * @param string $string
381 * @param int $color
382 * @param null|int $bgColor
383 * @return string
385 public function colorize($string, $color = null, $bgColor = null)
387 return $string;
391 * Change current drawing color.
393 * @param int $color
395 public function setColor($color)
400 * Change current drawing background color
402 * @param int $color
404 public function setBgColor($color)
409 * Reset color to console default.
411 public function resetColor()
416 * Set Console charset to use.
418 * @param Charset\CharsetInterface $charset
420 public function setCharset(Charset\CharsetInterface $charset)
422 $this->charset = $charset;
426 * Get charset currently in use by this adapter.
428 * @return Charset\CharsetInterface $charset
430 public function getCharset()
432 if ($this->charset === null) {
433 $this->charset = $this->getDefaultCharset();
436 return $this->charset;
440 * @return Charset\Utf8
442 public function getDefaultCharset()
444 return new Charset\Utf8;
448 * Clear console screen
450 public function clear()
452 echo "\f";
456 * Clear line at cursor position
458 public function clearLine()
460 echo "\r" . str_repeat(" ", $this->getWidth()) . "\r";
464 * Clear console screen
466 public function clearScreen()
468 return $this->clear();
472 * Read a single line from the console input
474 * @param int $maxLength Maximum response length
475 * @return string
477 public function readLine($maxLength = 2048)
479 $f = fopen('php://stdin','r');
480 $line = stream_get_line($f, $maxLength, PHP_EOL);
481 fclose($f);
482 return rtrim($line,"\n\r");
486 * Read a single character from the console input
488 * @param string|null $mask A list of allowed chars
489 * @return string
491 public function readChar($mask = null)
493 $f = fopen('php://stdin','r');
494 do {
495 $char = fread($f,1);
496 } while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
497 fclose($f);
498 return $char;