DebugFormatterHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 const COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
  21. /**
  22. * @var mixed[]
  23. */
  24. private $started = [];
  25. /**
  26. * @var int
  27. */
  28. private $count = -1;
  29. /**
  30. * Starts a debug formatting session.
  31. * @param string $id
  32. * @param string $message
  33. * @param string $prefix
  34. */
  35. public function start($id, $message, $prefix = 'RUN')
  36. {
  37. $this->started[$id] = ['border' => ++$this->count % \count(self::COLORS)];
  38. return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
  39. }
  40. /**
  41. * Adds progress to a formatting session.
  42. * @param string $id
  43. * @param string $buffer
  44. * @param bool $error
  45. * @param string $prefix
  46. * @param string $errorPrefix
  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. * @param string $id
  77. * @param string $message
  78. * @param bool $successful
  79. * @param string $prefix
  80. */
  81. public function stop($id, $message, $successful, $prefix = 'RES')
  82. {
  83. $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : '';
  84. if ($successful) {
  85. return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  86. }
  87. $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  88. unset($this->started[$id]['out'], $this->started[$id]['err']);
  89. return $message;
  90. }
  91. /**
  92. * @param string $id
  93. */
  94. private function getBorder($id)
  95. {
  96. return sprintf('<bg=%s> </>', self::COLORS[$this->started[$id]['border']]);
  97. }
  98. public function getName()
  99. {
  100. return 'debug_formatter';
  101. }
  102. }