DebugFormatterHelper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. private $started = [];
  22. private $count = -1;
  23. /**
  24. * Starts a debug formatting session.
  25. *
  26. * @return string
  27. */
  28. public function start(string $id, string $message, string $prefix = 'RUN')
  29. {
  30. $this->started[$id] = ['border' => ++$this->count % \count(self::COLORS)];
  31. return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
  32. }
  33. /**
  34. * Adds progress to a formatting session.
  35. *
  36. * @return string
  37. */
  38. public function progress(string $id, string $buffer, bool $error = false, string $prefix = 'OUT', string $errorPrefix = 'ERR')
  39. {
  40. $message = '';
  41. if ($error) {
  42. if (isset($this->started[$id]['out'])) {
  43. $message .= "\n";
  44. unset($this->started[$id]['out']);
  45. }
  46. if (!isset($this->started[$id]['err'])) {
  47. $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix);
  48. $this->started[$id]['err'] = true;
  49. }
  50. $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer);
  51. } else {
  52. if (isset($this->started[$id]['err'])) {
  53. $message .= "\n";
  54. unset($this->started[$id]['err']);
  55. }
  56. if (!isset($this->started[$id]['out'])) {
  57. $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix);
  58. $this->started[$id]['out'] = true;
  59. }
  60. $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer);
  61. }
  62. return $message;
  63. }
  64. /**
  65. * Stops a formatting session.
  66. *
  67. * @return string
  68. */
  69. public function stop(string $id, string $message, bool $successful, string $prefix = 'RES')
  70. {
  71. $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : '';
  72. if ($successful) {
  73. return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  74. }
  75. $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
  76. unset($this->started[$id]['out'], $this->started[$id]['err']);
  77. return $message;
  78. }
  79. private function getBorder(string $id): string
  80. {
  81. return sprintf('<bg=%s> </>', self::COLORS[$this->started[$id]['border']]);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getName()
  87. {
  88. return 'debug_formatter';
  89. }
  90. }