DebugFormatterHelper.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Helper;
  11. /**
  12. * Helps outputting debug information when running an external program from a command.
  13. *
  14. * An external program can be a Process, an HTTP request, or anything else.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class DebugFormatterHelper extends Helper
  19. {
  20. private $colors = array('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default');
  21. private $started = array();
  22. private $count = -1;
  23. /**
  24. * Starts a debug formatting session.
  25. *
  26. * @param string $id The id of the formatting session
  27. * @param string $message The message to display
  28. * @param string $prefix The prefix to use
  29. *
  30. * @return string
  31. */
  32. public function start($id, $message, $prefix = 'RUN')
  33. {
  34. $this->started[$id] = array('border' => ++$this->count % count($this->colors));
  35. return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
  36. }
  37. /**
  38. * Adds progress to a formatting session.
  39. *
  40. * @param string $id The id of the formatting session
  41. * @param string $buffer The message to display
  42. * @param bool $error Whether to consider the buffer as error
  43. * @param string $prefix The prefix for output
  44. * @param string $errorPrefix The prefix for error output
  45. *
  46. * @return string
  47. */
  48. public function progress($id, $buffer, $error = false, $prefix = 'OUT', $errorPrefix = 'ERR')
  49. {
  50. $message = '';
  51. if ($error) {
  52. if (isset($this->started[$id]['out'])) {
  53. $message .= "\n";
  54. unset($this->started[$id]['out']);
  55. }
  56. if (!isset($this->started[$id]['err'])) {
  57. $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix);
  58. $this->started[$id]['err'] = true;
  59. }
  60. $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer);
  61. } else {
  62. if (isset($this->started[$id]['err'])) {
  63. $message .= "\n";
  64. unset($this->started[$id]['err']);
  65. }
  66. if (!isset($this->started[$id]['out'])) {
  67. $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix);
  68. $this->started[$id]['out'] = true;
  69. }
  70. $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer);
  71. }
  72. return $message;
  73. }
  74. /**
  75. * Stops a formatting session.
  76. *
  77. * @param string $id The id of the formatting session
  78. * @param string $message The message to display
  79. * @param bool $successful Whether to consider the result as success
  80. * @param string $prefix The prefix for the end output
  81. *
  82. * @return string
  83. */
  84. public function stop($id, $message, $successful, $prefix = 'RES')
  85. {
  86. $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : '';
  87. if ($successful) {
  88. return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  89. }
  90. $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  91. unset($this->started[$id]['out'], $this->started[$id]['err']);
  92. return $message;
  93. }
  94. /**
  95. * @param string $id The id of the formatting session
  96. *
  97. * @return string
  98. */
  99. private function getBorder($id)
  100. {
  101. return sprintf('<bg=%s> </>', $this->colors[$this->started[$id]['border']]);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getName()
  107. {
  108. return 'debug_formatter';
  109. }
  110. }