PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpspec/phpspec/src/PhpSpec/Console/IO.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 367 lines | 202 code | 57 blank | 108 comment | 22 complexity | 44e86f8f127e9bc52c6ec9c1504c210e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PhpSpec, A php toolset to drive emergent
  4. * design by specification.
  5. *
  6. * (c) Marcello Duarte <marcello.duarte@gmail.com>
  7. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. namespace PhpSpec\Console;
  13. use PhpSpec\IO\IOInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use PhpSpec\Config\OptionsConfig;
  17. /**
  18. * Class IO deals with input and output from command line interaction
  19. */
  20. class IO implements IOInterface
  21. {
  22. const COL_MIN_WIDTH = 40;
  23. const COL_DEFAULT_WIDTH = 60;
  24. const COL_MAX_WIDTH = 80;
  25. /**
  26. * @var InputInterface
  27. */
  28. private $input;
  29. /**
  30. * @var OutputInterface
  31. */
  32. private $output;
  33. /**
  34. * @var string
  35. */
  36. private $lastMessage;
  37. /**
  38. * @var bool
  39. */
  40. private $hasTempString = false;
  41. /**
  42. * @var OptionsConfig
  43. */
  44. private $config;
  45. /**
  46. * @var integer
  47. */
  48. private $consoleWidth;
  49. /**
  50. * @var Prompter
  51. */
  52. private $prompter;
  53. /**
  54. * @param InputInterface $input
  55. * @param OutputInterface $output
  56. * @param OptionsConfig $config
  57. * @param Prompter $prompter
  58. */
  59. public function __construct(
  60. InputInterface $input,
  61. OutputInterface $output,
  62. OptionsConfig $config,
  63. Prompter $prompter
  64. ) {
  65. $this->input = $input;
  66. $this->output = $output;
  67. $this->config = $config;
  68. $this->prompter = $prompter;
  69. }
  70. /**
  71. * @return bool
  72. */
  73. public function isInteractive()
  74. {
  75. return $this->input->isInteractive();
  76. }
  77. /**
  78. * @return bool
  79. */
  80. public function isDecorated()
  81. {
  82. return $this->output->isDecorated();
  83. }
  84. /**
  85. * @return bool
  86. */
  87. public function isCodeGenerationEnabled()
  88. {
  89. if (!$this->isInteractive()) {
  90. return false;
  91. }
  92. return $this->config->isCodeGenerationEnabled()
  93. && !$this->input->getOption('no-code-generation');
  94. }
  95. /**
  96. * @return bool
  97. */
  98. public function isStopOnFailureEnabled()
  99. {
  100. return $this->config->isStopOnFailureEnabled()
  101. || $this->input->getOption('stop-on-failure');
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function isVerbose()
  107. {
  108. return OutputInterface::VERBOSITY_VERBOSE <= $this->output->getVerbosity();
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getLastWrittenMessage()
  114. {
  115. return $this->lastMessage;
  116. }
  117. /**
  118. * @param string $message
  119. * @param integer|null $indent
  120. */
  121. public function writeln($message = '', $indent = null)
  122. {
  123. $this->write($message, $indent, true);
  124. }
  125. /**
  126. * @param string $message
  127. * @param integer|null $indent
  128. */
  129. public function writeTemp($message, $indent = null)
  130. {
  131. $this->write($message, $indent);
  132. $this->hasTempString = true;
  133. }
  134. /**
  135. * @return null|string
  136. */
  137. public function cutTemp()
  138. {
  139. if (false === $this->hasTempString) {
  140. return;
  141. }
  142. $message = $this->lastMessage;
  143. $this->write('');
  144. return $message;
  145. }
  146. /**
  147. *
  148. */
  149. public function freezeTemp()
  150. {
  151. $this->write($this->lastMessage);
  152. }
  153. /**
  154. * @param string $message
  155. * @param integer|null $indent
  156. * @param bool $newline
  157. */
  158. public function write($message, $indent = null, $newline = false)
  159. {
  160. if ($this->hasTempString) {
  161. $this->hasTempString = false;
  162. $this->overwrite($message, $indent, $newline);
  163. return;
  164. }
  165. if (null !== $indent) {
  166. $message = $this->indentText($message, $indent);
  167. }
  168. $this->output->write($message, $newline);
  169. $this->lastMessage = $message.($newline ? "\n" : '');
  170. }
  171. /**
  172. * @param string $message
  173. * @param integer|null $indent
  174. */
  175. public function overwriteln($message = '', $indent = null)
  176. {
  177. $this->overwrite($message, $indent, true);
  178. }
  179. /**
  180. * @param string $message
  181. * @param integer|null $indent
  182. * @param bool $newline
  183. */
  184. public function overwrite($message, $indent = null, $newline = false)
  185. {
  186. if (null !== $indent) {
  187. $message = $this->indentText($message, $indent);
  188. }
  189. if ($message === $this->lastMessage) {
  190. return;
  191. }
  192. $commonPrefix = $this->getCommonPrefix($message, $this->lastMessage);
  193. $newSuffix = substr($message, strlen($commonPrefix));
  194. $oldSuffix = substr($this->lastMessage, strlen($commonPrefix));
  195. $overwriteLength = strlen(strip_tags($oldSuffix));
  196. $this->write(str_repeat("\x08", $overwriteLength));
  197. $this->write($newSuffix);
  198. $fill = $overwriteLength - strlen(strip_tags($newSuffix));
  199. if ($fill > 0) {
  200. $this->write(str_repeat(' ', $fill));
  201. $this->write(str_repeat("\x08", $fill));
  202. }
  203. if ($newline) {
  204. $this->writeln();
  205. }
  206. $this->lastMessage = $message.($newline ? "\n" : '');
  207. }
  208. private function getCommonPrefix($stringA, $stringB)
  209. {
  210. for ($i = 0, $len = min(strlen($stringA), strlen($stringB)); $i<$len; $i++) {
  211. if ($stringA[$i] != $stringB[$i]) {
  212. break;
  213. }
  214. }
  215. $common = substr($stringA, 0, $i);
  216. if (preg_match('/(^.*)<[a-z-]*>?[^<]*$/', $common, $matches)) {
  217. $common = $matches[1];
  218. }
  219. return $common;
  220. }
  221. /**
  222. * @param string $question
  223. * @param bool $default
  224. *
  225. * @return Boolean
  226. */
  227. public function askConfirmation($question, $default = true)
  228. {
  229. $lines = array();
  230. $lines[] = '<question>'.str_repeat(' ', $this->getBlockWidth())."</question>";
  231. foreach (explode("\n", wordwrap($question, $this->getBlockWidth() - 4, "\n", true)) as $line) {
  232. $lines[] = '<question> '.str_pad($line, $this->getBlockWidth() - 2).'</question>';
  233. }
  234. $lines[] = '<question>'.str_repeat(' ', $this->getBlockWidth() - 8).'</question> <value>'.
  235. ($default ? '[Y/n]' : '[y/N]').'</value> ';
  236. $formattedQuestion = implode("\n", $lines) . "\n";
  237. return $this->prompter->askConfirmation($formattedQuestion, $default);
  238. }
  239. /**
  240. * @param string $text
  241. * @param integer $indent
  242. *
  243. * @return string
  244. */
  245. private function indentText($text, $indent)
  246. {
  247. return implode("\n", array_map(
  248. function ($line) use ($indent) {
  249. return str_repeat(' ', $indent).$line;
  250. },
  251. explode("\n", $text)
  252. ));
  253. }
  254. public function isRerunEnabled()
  255. {
  256. return !$this->input->getOption('no-rerun') && $this->config->isReRunEnabled();
  257. }
  258. public function isFakingEnabled()
  259. {
  260. return $this->input->getOption('fake') || $this->config->isFakingEnabled();
  261. }
  262. public function getBootstrapPath()
  263. {
  264. if ($path = $this->input->getOption('bootstrap')) {
  265. return $path;
  266. }
  267. if ($path = $this->config->getBootstrapPath()) {
  268. return $path;
  269. }
  270. return false;
  271. }
  272. /**
  273. * @param integer $width
  274. */
  275. public function setConsoleWidth($width)
  276. {
  277. $this->consoleWidth = $width;
  278. }
  279. /**
  280. * @return integer
  281. */
  282. public function getBlockWidth()
  283. {
  284. $width = self::COL_DEFAULT_WIDTH;
  285. if ($this->consoleWidth && ($this->consoleWidth - 10) > self::COL_MIN_WIDTH) {
  286. $width = $this->consoleWidth - 10;
  287. }
  288. if ($width > self::COL_MAX_WIDTH) {
  289. $width = self::COL_MAX_WIDTH;
  290. }
  291. return $width;
  292. }
  293. /**
  294. * @param string $message
  295. * @param int $indent
  296. */
  297. public function writeBrokenCodeBlock($message, $indent = 0)
  298. {
  299. $message = wordwrap($message, $this->getBlockWidth() - ($indent * 2), "\n", true);
  300. if ($indent) {
  301. $message = $this->indentText($message, $indent);
  302. }
  303. $this->output->writeln("<broken-bg>".str_repeat(" ", $this->getBlockWidth())."</broken-bg>");
  304. foreach (explode("\n", $message) as $line) {
  305. $this->output->writeln("<broken-bg>".str_pad($line, $this->getBlockWidth(), ' ')."</broken-bg>");
  306. }
  307. $this->output->writeln("<broken-bg>".str_repeat(" ", $this->getBlockWidth())."</broken-bg>");
  308. $this->output->writeln('');
  309. }
  310. }