Output.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Console;
  36. use Hoa\Stream;
  37. /**
  38. * Class \Hoa\Console\Output.
  39. *
  40. * This class represents the output of a program. Most of the time, this is
  41. * going to be STDOUT.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Output implements Stream\IStream\Out
  47. {
  48. /**
  49. * Whether the multiplexer must be considered while writing on the output.
  50. *
  51. * @var bool
  52. */
  53. protected $_considerMultiplexer = false;
  54. /**
  55. * Real output stream.
  56. *
  57. * @var \Hoa\Stream\IStream\Out
  58. */
  59. protected $_output = null;
  60. /**
  61. * Wraps an `Hoa\Stream\IStream\Out` stream.
  62. *
  63. * @param \Hoa\Stream\IStream\Out $output Output.
  64. */
  65. public function __construct(Stream\IStream\Out $output = null)
  66. {
  67. $this->_output = $output;
  68. return;
  69. }
  70. /**
  71. * Get the real output stream.
  72. *
  73. * @return \Hoa\Stream\IStream\Out
  74. */
  75. public function getStream()
  76. {
  77. return $this->_output;
  78. }
  79. /**
  80. * Write n characters.
  81. *
  82. * @param string $string String.
  83. * @param int $length Length.
  84. * @return void
  85. * @throws \Hoa\Console\Exception
  86. */
  87. public function write($string, $length)
  88. {
  89. if (0 > $length) {
  90. throw new Exception(
  91. 'Length must be greater than 0, given %d.',
  92. 0,
  93. $length
  94. );
  95. }
  96. $out = substr($string, 0, $length);
  97. if (true === $this->isMultiplexerConsidered()) {
  98. if (true === Console::isTmuxRunning()) {
  99. $out =
  100. "\033Ptmux;" .
  101. str_replace("\033", "\033\033", $out) .
  102. "\033\\";
  103. }
  104. $length = strlen($out);
  105. }
  106. if (null === $this->_output) {
  107. echo $out;
  108. } else {
  109. $this->_output->write($out, $length);
  110. }
  111. }
  112. /**
  113. * Write a string.
  114. *
  115. * @param string $string String.
  116. * @return void
  117. */
  118. public function writeString($string)
  119. {
  120. $string = (string) $string;
  121. return $this->write($string, strlen($string));
  122. }
  123. /**
  124. * Write a character.
  125. *
  126. * @param string $character Character.
  127. * @return void
  128. */
  129. public function writeCharacter($character)
  130. {
  131. return $this->write((string) $character[0], 1);
  132. }
  133. /**
  134. * Write a boolean.
  135. *
  136. * @param bool $boolean Boolean.
  137. * @return void
  138. */
  139. public function writeBoolean($boolean)
  140. {
  141. return $this->write(((bool) $boolean) ? '1' : '0', 1);
  142. }
  143. /**
  144. * Write an integer.
  145. *
  146. * @param int $integer Integer.
  147. * @return void
  148. */
  149. public function writeInteger($integer)
  150. {
  151. $integer = (string) (int) $integer;
  152. return $this->write($integer, strlen($integer));
  153. }
  154. /**
  155. * Write a float.
  156. *
  157. * @param float $float Float.
  158. * @return void
  159. */
  160. public function writeFloat($float)
  161. {
  162. $float = (string) (float) $float;
  163. return $this->write($float, strlen($float));
  164. }
  165. /**
  166. * Write an array.
  167. *
  168. * @param array $array Array.
  169. * @return void
  170. */
  171. public function writeArray(array $array)
  172. {
  173. $array = var_export($array, true);
  174. return $this->write($array, strlen($array));
  175. }
  176. /**
  177. * Write a line.
  178. *
  179. * @param string $line Line.
  180. * @return void
  181. */
  182. public function writeLine($line)
  183. {
  184. if (false === $n = strpos($line, "\n")) {
  185. return $this->write($line . "\n", strlen($line) + 1);
  186. }
  187. ++$n;
  188. return $this->write(substr($line, 0, $n), $n);
  189. }
  190. /**
  191. * Write all, i.e. as much as possible.
  192. *
  193. * @param string $string String.
  194. * @return void
  195. */
  196. public function writeAll($string)
  197. {
  198. return $this->write($string, strlen($string));
  199. }
  200. /**
  201. * Truncate a stream to a given length.
  202. *
  203. * @param int $size Size.
  204. * @return bool
  205. */
  206. public function truncate($size)
  207. {
  208. return false;
  209. }
  210. /**
  211. * Consider the multiplexer (if running) while writing on the output.
  212. *
  213. * @param bool $consider Consider the multiplexer or not.
  214. * @return bool
  215. */
  216. public function considerMultiplexer($consider)
  217. {
  218. $old = $this->_considerMultiplexer;
  219. $this->_considerMultiplexer = $consider;
  220. return $old;
  221. }
  222. /**
  223. * Check whether the multiplexer must be considered or not.
  224. *
  225. * @return bool
  226. */
  227. public function isMultiplexerConsidered()
  228. {
  229. return $this->_considerMultiplexer;
  230. }
  231. }