Drupal investigation

SymfonyStyle.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Style;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Exception\RuntimeException;
  13. use Symfony\Component\Console\Formatter\OutputFormatter;
  14. use Symfony\Component\Console\Helper\Helper;
  15. use Symfony\Component\Console\Helper\ProgressBar;
  16. use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  17. use Symfony\Component\Console\Helper\Table;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\BufferedOutput;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Question\ChoiceQuestion;
  22. use Symfony\Component\Console\Question\ConfirmationQuestion;
  23. use Symfony\Component\Console\Question\Question;
  24. /**
  25. * Output decorator helpers for the Symfony Style Guide.
  26. *
  27. * @author Kevin Bond <kevinbond@gmail.com>
  28. */
  29. class SymfonyStyle extends OutputStyle
  30. {
  31. const MAX_LINE_LENGTH = 120;
  32. private $input;
  33. private $questionHelper;
  34. private $progressBar;
  35. private $lineLength;
  36. private $bufferedOutput;
  37. /**
  38. * @param InputInterface $input
  39. * @param OutputInterface $output
  40. */
  41. public function __construct(InputInterface $input, OutputInterface $output)
  42. {
  43. $this->input = $input;
  44. $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
  45. // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
  46. $this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
  47. parent::__construct($output);
  48. }
  49. /**
  50. * Formats a message as a block of text.
  51. *
  52. * @param string|array $messages The message to write in the block
  53. * @param string|null $type The block type (added in [] on first line)
  54. * @param string|null $style The style to apply to the whole block
  55. * @param string $prefix The prefix for the block
  56. * @param bool $padding Whether to add vertical padding
  57. */
  58. public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
  59. {
  60. $messages = is_array($messages) ? array_values($messages) : array($messages);
  61. $this->autoPrependBlock();
  62. $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
  63. $this->newLine();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function title($message)
  69. {
  70. $this->autoPrependBlock();
  71. $this->writeln(array(
  72. sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  73. sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  74. ));
  75. $this->newLine();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function section($message)
  81. {
  82. $this->autoPrependBlock();
  83. $this->writeln(array(
  84. sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  85. sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  86. ));
  87. $this->newLine();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function listing(array $elements)
  93. {
  94. $this->autoPrependText();
  95. $elements = array_map(function ($element) {
  96. return sprintf(' * %s', $element);
  97. }, $elements);
  98. $this->writeln($elements);
  99. $this->newLine();
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function text($message)
  105. {
  106. $this->autoPrependText();
  107. $messages = is_array($message) ? array_values($message) : array($message);
  108. foreach ($messages as $message) {
  109. $this->writeln(sprintf(' %s', $message));
  110. }
  111. }
  112. /**
  113. * Formats a command comment.
  114. *
  115. * @param string|array $message
  116. */
  117. public function comment($message)
  118. {
  119. $messages = is_array($message) ? array_values($message) : array($message);
  120. $this->autoPrependBlock();
  121. $this->writeln($this->createBlock($messages, null, null, '<fg=default;bg=default> // </>'));
  122. $this->newLine();
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function success($message)
  128. {
  129. $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function error($message)
  135. {
  136. $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function warning($message)
  142. {
  143. $this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function note($message)
  149. {
  150. $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function caution($message)
  156. {
  157. $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function table(array $headers, array $rows)
  163. {
  164. $style = clone Table::getStyleDefinition('symfony-style-guide');
  165. $style->setCellHeaderFormat('<info>%s</info>');
  166. $table = new Table($this);
  167. $table->setHeaders($headers);
  168. $table->setRows($rows);
  169. $table->setStyle($style);
  170. $table->render();
  171. $this->newLine();
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function ask($question, $default = null, $validator = null)
  177. {
  178. $question = new Question($question, $default);
  179. $question->setValidator($validator);
  180. return $this->askQuestion($question);
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function askHidden($question, $validator = null)
  186. {
  187. $question = new Question($question);
  188. $question->setHidden(true);
  189. $question->setValidator($validator);
  190. return $this->askQuestion($question);
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function confirm($question, $default = true)
  196. {
  197. return $this->askQuestion(new ConfirmationQuestion($question, $default));
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function choice($question, array $choices, $default = null)
  203. {
  204. if (null !== $default) {
  205. $values = array_flip($choices);
  206. $default = $values[$default];
  207. }
  208. return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function progressStart($max = 0)
  214. {
  215. $this->progressBar = $this->createProgressBar($max);
  216. $this->progressBar->start();
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function progressAdvance($step = 1)
  222. {
  223. $this->getProgressBar()->advance($step);
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function progressFinish()
  229. {
  230. $this->getProgressBar()->finish();
  231. $this->newLine(2);
  232. $this->progressBar = null;
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function createProgressBar($max = 0)
  238. {
  239. $progressBar = parent::createProgressBar($max);
  240. if ('\\' !== DIRECTORY_SEPARATOR) {
  241. $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
  242. $progressBar->setProgressCharacter('');
  243. $progressBar->setBarCharacter('▓'); // dark shade character \u2593
  244. }
  245. return $progressBar;
  246. }
  247. /**
  248. * @param Question $question
  249. *
  250. * @return string
  251. */
  252. public function askQuestion(Question $question)
  253. {
  254. if ($this->input->isInteractive()) {
  255. $this->autoPrependBlock();
  256. }
  257. if (!$this->questionHelper) {
  258. $this->questionHelper = new SymfonyQuestionHelper();
  259. }
  260. $answer = $this->questionHelper->ask($this->input, $this, $question);
  261. if ($this->input->isInteractive()) {
  262. $this->newLine();
  263. $this->bufferedOutput->write("\n");
  264. }
  265. return $answer;
  266. }
  267. /**
  268. * {@inheritdoc}
  269. */
  270. public function writeln($messages, $type = self::OUTPUT_NORMAL)
  271. {
  272. parent::writeln($messages, $type);
  273. $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
  279. {
  280. parent::write($messages, $newline, $type);
  281. $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
  282. }
  283. /**
  284. * {@inheritdoc}
  285. */
  286. public function newLine($count = 1)
  287. {
  288. parent::newLine($count);
  289. $this->bufferedOutput->write(str_repeat("\n", $count));
  290. }
  291. /**
  292. * @return ProgressBar
  293. */
  294. private function getProgressBar()
  295. {
  296. if (!$this->progressBar) {
  297. throw new RuntimeException('The ProgressBar is not started.');
  298. }
  299. return $this->progressBar;
  300. }
  301. private function getTerminalWidth()
  302. {
  303. $application = new Application();
  304. $dimensions = $application->getTerminalDimensions();
  305. return $dimensions[0] ?: self::MAX_LINE_LENGTH;
  306. }
  307. private function autoPrependBlock()
  308. {
  309. $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
  310. if (!isset($chars[0])) {
  311. return $this->newLine(); //empty history, so we should start with a new line.
  312. }
  313. //Prepend new line for each non LF chars (This means no blank line was output before)
  314. $this->newLine(2 - substr_count($chars, "\n"));
  315. }
  316. private function autoPrependText()
  317. {
  318. $fetched = $this->bufferedOutput->fetch();
  319. //Prepend new line if last char isn't EOL:
  320. if ("\n" !== substr($fetched, -1)) {
  321. $this->newLine();
  322. }
  323. }
  324. private function reduceBuffer($messages)
  325. {
  326. // We need to know if the two last chars are PHP_EOL
  327. // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
  328. return array_map(function ($value) {
  329. return substr($value, -4);
  330. }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
  331. }
  332. private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
  333. {
  334. $indentLength = 0;
  335. $prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
  336. $lines = array();
  337. if (null !== $type) {
  338. $type = sprintf('[%s] ', $type);
  339. $indentLength = strlen($type);
  340. $lineIndentation = str_repeat(' ', $indentLength);
  341. }
  342. // wrap and add newlines for each element
  343. foreach ($messages as $key => $message) {
  344. if ($escape) {
  345. $message = OutputFormatter::escape($message);
  346. }
  347. $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
  348. if (count($messages) > 1 && $key < count($messages) - 1) {
  349. $lines[] = '';
  350. }
  351. }
  352. $firstLineIndex = 0;
  353. if ($padding && $this->isDecorated()) {
  354. $firstLineIndex = 1;
  355. array_unshift($lines, '');
  356. $lines[] = '';
  357. }
  358. foreach ($lines as $i => &$line) {
  359. if (null !== $type) {
  360. $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
  361. }
  362. $line = $prefix.$line;
  363. $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
  364. if ($style) {
  365. $line = sprintf('<%s>%s</>', $style, $line);
  366. }
  367. }
  368. return $lines;
  369. }
  370. }