DebugFormatterHelper.php 3.2 KB

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