PageRenderTime 24ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/library/Zend/Console/Adapter/AbstractAdapter.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 500 lines | 259 code | 51 blank | 190 comment | 29 complexity | 68bf3f1b7fe3aa7c1cb7c87631140803 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 Zend\Console\Charset;
  11. use Zend\Console\Exception;
  12. use Zend\Stdlib\StringUtils;
  13. /**
  14. * Common console adapter codebase
  15. */
  16. abstract class AbstractAdapter implements AdapterInterface
  17. {
  18. /**
  19. * Whether or not mbstring is enabled
  20. *
  21. * @var null|bool
  22. */
  23. protected static $hasMBString;
  24. /**
  25. * @var Charset\CharsetInterface
  26. */
  27. protected $charset;
  28. /**
  29. * Current cursor X position
  30. *
  31. * @var int
  32. */
  33. protected $posX;
  34. /**
  35. * Current cursor Y position
  36. *
  37. * @var int
  38. */
  39. protected $posY;
  40. /**
  41. * Write a chunk of text to console.
  42. *
  43. * @param string $text
  44. * @param null|int $color
  45. * @param null|int $bgColor
  46. */
  47. public function write($text, $color = null, $bgColor = null)
  48. {
  49. if ($color !== null || $bgColor !== null) {
  50. echo $this->colorize($text, $color, $bgColor);
  51. } else {
  52. echo $text;
  53. }
  54. }
  55. /**
  56. * Alias for write()
  57. *
  58. * @param string $text
  59. * @param null|int $color
  60. * @param null|int $bgColor
  61. */
  62. public function writeText($text, $color = null, $bgColor = null)
  63. {
  64. return $this->write($text, $color, $bgColor);
  65. }
  66. /**
  67. * Write a single line of text to console and advance cursor to the next line.
  68. *
  69. * @param string $text
  70. * @param null|int $color
  71. * @param null|int $bgColor
  72. */
  73. public function writeLine($text = "", $color = null, $bgColor = null)
  74. {
  75. $this->write($text . PHP_EOL, $color, $bgColor);
  76. }
  77. /**
  78. * Write a piece of text at the coordinates of $x and $y
  79. *
  80. *
  81. * @param string $text Text to write
  82. * @param int $x Console X coordinate (column)
  83. * @param int $y Console Y coordinate (row)
  84. * @param null|int $color
  85. * @param null|int $bgColor
  86. */
  87. public function writeAt($text, $x, $y, $color = null, $bgColor = null)
  88. {
  89. $this->setPos( $x, $y );
  90. $this->write( $text, $color, $bgColor );
  91. }
  92. /**
  93. * Write a box at the specified coordinates.
  94. * If X or Y coordinate value is negative, it will be calculated as the distance from far right or bottom edge
  95. * of the console (respectively).
  96. *
  97. * @param int $x1 Top-left corner X coordinate (column)
  98. * @param int $y1 Top-left corner Y coordinate (row)
  99. * @param int $x2 Bottom-right corner X coordinate (column)
  100. * @param int $y2 Bottom-right corner Y coordinate (row)
  101. * @param int $lineStyle (optional) Box border style.
  102. * @param int $fillStyle (optional) Box fill style or a single character to fill it with.
  103. * @param int $color (optional) Foreground color
  104. * @param int $bgColor (optional) Background color
  105. * @param null|int $fillColor (optional) Foreground color of box fill
  106. * @param null|int $fillBgColor (optional) Background color of box fill
  107. * @throws Exception\BadMethodCallException if coordinates are invalid
  108. */
  109. public function writeBox(
  110. $x1,
  111. $y1,
  112. $x2,
  113. $y2,
  114. $lineStyle = self::LINE_SINGLE,
  115. $fillStyle = self::FILL_NONE,
  116. $color = null,
  117. $bgColor = null,
  118. $fillColor = null,
  119. $fillBgColor = null
  120. ) {
  121. // Sanitize coordinates
  122. $x1 = (int) $x1;
  123. $y1 = (int) $y1;
  124. $x2 = (int) $x2;
  125. $y2 = (int) $y2;
  126. // Translate negative coordinates
  127. if ($x2 < 0) {
  128. $x2 = $this->getWidth() - $x2;
  129. }
  130. if ($y2 < 0) {
  131. $y2 = $this->getHeight() - $y2;
  132. }
  133. // Validate coordinates
  134. if ($x1 < 0
  135. || $y1 < 0
  136. || $x2 < $x1
  137. || $y2 < $y1
  138. ) {
  139. throw new Exception\BadMethodCallException('Supplied X,Y coordinates are invalid.');
  140. }
  141. // Determine charset and dimensions
  142. $charset = $this->getCharset();
  143. $width = $x2 - $x1 + 1;
  144. $height = $y2 - $y1 + 1;
  145. if ($width <= 2) {
  146. $lineStyle = static::LINE_NONE;
  147. }
  148. // Activate line drawing
  149. $this->write($charset::ACTIVATE);
  150. // Draw horizontal lines
  151. if ($lineStyle !== static::LINE_NONE) {
  152. switch ($lineStyle) {
  153. case static::LINE_SINGLE:
  154. $lineChar = $charset::LINE_SINGLE_EW;
  155. break;
  156. case static::LINE_DOUBLE:
  157. $lineChar = $charset::LINE_DOUBLE_EW;
  158. break;
  159. case static::LINE_BLOCK:
  160. default:
  161. $lineChar = $charset::LINE_BLOCK_EW;
  162. break;
  163. }
  164. $this->setPos($x1 + 1, $y1);
  165. $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
  166. $this->setPos($x1 + 1, $y2);
  167. $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
  168. }
  169. // Draw vertical lines and fill
  170. if (is_numeric($fillStyle)
  171. && $fillStyle !== static::FILL_NONE) {
  172. switch ($fillStyle) {
  173. case static::FILL_SHADE_LIGHT:
  174. $fillChar = $charset::SHADE_LIGHT;
  175. break;
  176. case static::FILL_SHADE_MEDIUM:
  177. $fillChar = $charset::SHADE_MEDIUM;
  178. break;
  179. case static::FILL_SHADE_DARK:
  180. $fillChar = $charset::SHADE_DARK;
  181. break;
  182. case static::FILL_BLOCK:
  183. default:
  184. $fillChar = $charset::BLOCK;
  185. break;
  186. }
  187. } elseif ($fillStyle) {
  188. $fillChar = StringUtils::getWrapper()->substr($fillStyle, 0, 1);
  189. } else {
  190. $fillChar = ' ';
  191. }
  192. if ($lineStyle === static::LINE_NONE) {
  193. for ($y = $y1; $y <= $y2; $y++) {
  194. $this->setPos($x1, $y);
  195. $this->write(str_repeat($fillChar, $width), $fillColor, $fillBgColor);
  196. }
  197. } else {
  198. switch ($lineStyle) {
  199. case static::LINE_DOUBLE:
  200. $lineChar = $charset::LINE_DOUBLE_NS;
  201. break;
  202. case static::LINE_BLOCK:
  203. $lineChar = $charset::LINE_BLOCK_NS;
  204. break;
  205. case static::LINE_SINGLE:
  206. default:
  207. $lineChar = $charset::LINE_SINGLE_NS;
  208. break;
  209. }
  210. for ($y = $y1 + 1; $y < $y2; $y++) {
  211. $this->setPos($x1, $y);
  212. $this->write($lineChar, $color, $bgColor);
  213. $this->write(str_repeat($fillChar, $width - 2), $fillColor, $fillBgColor);
  214. $this->write($lineChar, $color, $bgColor);
  215. }
  216. }
  217. // Draw corners
  218. if ($lineStyle !== static::LINE_NONE) {
  219. if ($color !== null) {
  220. $this->setColor($color);
  221. }
  222. if ($bgColor !== null) {
  223. $this->setBgColor($bgColor);
  224. }
  225. if ($lineStyle === static::LINE_SINGLE) {
  226. $this->writeAt($charset::LINE_SINGLE_NW, $x1, $y1);
  227. $this->writeAt($charset::LINE_SINGLE_NE, $x2, $y1);
  228. $this->writeAt($charset::LINE_SINGLE_SE, $x2, $y2);
  229. $this->writeAt($charset::LINE_SINGLE_SW, $x1, $y2);
  230. } elseif ($lineStyle === static::LINE_DOUBLE) {
  231. $this->writeAt($charset::LINE_DOUBLE_NW, $x1, $y1);
  232. $this->writeAt($charset::LINE_DOUBLE_NE, $x2, $y1);
  233. $this->writeAt($charset::LINE_DOUBLE_SE, $x2, $y2);
  234. $this->writeAt($charset::LINE_DOUBLE_SW, $x1, $y2);
  235. } elseif ($lineStyle === static::LINE_BLOCK) {
  236. $this->writeAt($charset::LINE_BLOCK_NW, $x1, $y1);
  237. $this->writeAt($charset::LINE_BLOCK_NE, $x2, $y1);
  238. $this->writeAt($charset::LINE_BLOCK_SE, $x2, $y2);
  239. $this->writeAt($charset::LINE_BLOCK_SW, $x1, $y2);
  240. }
  241. }
  242. // Deactivate line drawing and reset colors
  243. $this->write($charset::DEACTIVATE);
  244. $this->resetColor();
  245. }
  246. /**
  247. * Write a block of text at the given coordinates, matching the supplied width and height.
  248. * In case a line of text does not fit desired width, it will be wrapped to the next line.
  249. * In case the whole text does not fit in desired height, it will be truncated.
  250. *
  251. * @param string $text Text to write
  252. * @param int $width Maximum block width. Negative value means distance from right edge.
  253. * @param int|null $height Maximum block height. Negative value means distance from bottom edge.
  254. * @param int $x Block X coordinate (column)
  255. * @param int $y Block Y coordinate (row)
  256. * @param null|int $color (optional) Text color
  257. * @param null|int $bgColor (optional) Text background color
  258. */
  259. public function writeTextBlock(
  260. $text,
  261. $width,
  262. $height = null,
  263. $x = 0,
  264. $y = 0,
  265. $color = null,
  266. $bgColor = null
  267. ) {
  268. }
  269. /**
  270. * Determine and return current console width.
  271. *
  272. * @return int
  273. */
  274. public function getWidth()
  275. {
  276. return 80;
  277. }
  278. /**
  279. * Determine and return current console height.
  280. *
  281. * @return int
  282. */
  283. public function getHeight()
  284. {
  285. return 25;
  286. }
  287. /**
  288. * Determine and return current console width and height.
  289. *
  290. * @return array array($width, $height)
  291. */
  292. public function getSize()
  293. {
  294. return array(
  295. $this->getWidth(),
  296. $this->getHeight(),
  297. );
  298. }
  299. /**
  300. * Check if console is UTF-8 compatible
  301. *
  302. * @return bool
  303. */
  304. public function isUtf8()
  305. {
  306. return true;
  307. }
  308. /**
  309. * Set cursor position
  310. *
  311. * @param int $x
  312. * @param int $y
  313. */
  314. public function setPos($x, $y)
  315. {
  316. }
  317. /**
  318. * Show console cursor
  319. */
  320. public function showCursor()
  321. {
  322. }
  323. /**
  324. * Hide console cursor
  325. */
  326. public function hideCursor()
  327. {
  328. }
  329. /**
  330. * Return current console window title.
  331. *
  332. * @return string
  333. */
  334. public function getTitle()
  335. {
  336. return '';
  337. }
  338. /**
  339. * Prepare a string that will be rendered in color.
  340. *
  341. * @param string $string
  342. * @param int $color
  343. * @param null|int $bgColor
  344. * @return string
  345. */
  346. public function colorize($string, $color = null, $bgColor = null)
  347. {
  348. return $string;
  349. }
  350. /**
  351. * Change current drawing color.
  352. *
  353. * @param int $color
  354. */
  355. public function setColor($color)
  356. {
  357. }
  358. /**
  359. * Change current drawing background color
  360. *
  361. * @param int $color
  362. */
  363. public function setBgColor($color)
  364. {
  365. }
  366. /**
  367. * Reset color to console default.
  368. */
  369. public function resetColor()
  370. {
  371. }
  372. /**
  373. * Set Console charset to use.
  374. *
  375. * @param Charset\CharsetInterface $charset
  376. */
  377. public function setCharset(Charset\CharsetInterface $charset)
  378. {
  379. $this->charset = $charset;
  380. }
  381. /**
  382. * Get charset currently in use by this adapter.
  383. *
  384. * @return Charset\CharsetInterface $charset
  385. */
  386. public function getCharset()
  387. {
  388. if ($this->charset === null) {
  389. $this->charset = $this->getDefaultCharset();
  390. }
  391. return $this->charset;
  392. }
  393. /**
  394. * @return Charset\Utf8
  395. */
  396. public function getDefaultCharset()
  397. {
  398. return new Charset\Utf8;
  399. }
  400. /**
  401. * Clear console screen
  402. */
  403. public function clear()
  404. {
  405. echo "\f";
  406. }
  407. /**
  408. * Clear line at cursor position
  409. */
  410. public function clearLine()
  411. {
  412. echo "\r" . str_repeat( " ", $this->getWidth() ) . "\r";
  413. }
  414. /**
  415. * Clear console screen
  416. */
  417. public function clearScreen()
  418. {
  419. return $this->clear();
  420. }
  421. /**
  422. * Read a single line from the console input
  423. *
  424. * @param int $maxLength Maximum response length
  425. * @return string
  426. */
  427. public function readLine($maxLength = 2048)
  428. {
  429. $f = fopen('php://stdin','r');
  430. $line = stream_get_line($f, $maxLength, PHP_EOL);
  431. fclose($f);
  432. return rtrim($line,"\n\r");
  433. }
  434. /**
  435. * Read a single character from the console input
  436. *
  437. * @param string|null $mask A list of allowed chars
  438. * @return string
  439. */
  440. public function readChar($mask = null)
  441. {
  442. $f = fopen('php://stdin','r');
  443. do {
  444. $char = fread($f,1);
  445. } while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
  446. fclose($f);
  447. return $char;
  448. }
  449. }