4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
15 * Helps outputting debug information when running an external program from a command.
17 * An external program can be a Process, an HTTP request, or anything else.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class DebugFormatterHelper extends Helper
23 private $colors = array('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default');
24 private $started = array();
28 * Starts a debug formatting session.
30 * @param string $id The id of the formatting session
31 * @param string $message The message to display
32 * @param string $prefix The prefix to use
36 public function start($id, $message, $prefix = 'RUN')
38 $this->started[$id] = array('border' => ++$this->count % count($this->colors));
40 return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
44 * Adds progress to a formatting session.
46 * @param string $id The id of the formatting session
47 * @param string $buffer The message to display
48 * @param bool $error Whether to consider the buffer as error
49 * @param string $prefix The prefix for output
50 * @param string $errorPrefix The prefix for error output
54 public function progress($id, $buffer, $error = false, $prefix = 'OUT', $errorPrefix = 'ERR')
59 if (isset($this->started[$id]['out'])) {
61 unset($this->started[$id]['out']);
63 if (!isset($this->started[$id]['err'])) {
64 $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix);
65 $this->started[$id]['err'] = true;
68 $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer);
70 if (isset($this->started[$id]['err'])) {
72 unset($this->started[$id]['err']);
74 if (!isset($this->started[$id]['out'])) {
75 $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix);
76 $this->started[$id]['out'] = true;
79 $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer);
86 * Stops a formatting session.
88 * @param string $id The id of the formatting session
89 * @param string $message The message to display
90 * @param bool $successful Whether to consider the result as success
91 * @param string $prefix The prefix for the end output
95 public function stop($id, $message, $successful, $prefix = 'RES')
97 $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : '';
100 return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
103 $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
105 unset($this->started[$id]['out'], $this->started[$id]['err']);
111 * @param string $id The id of the formatting session
115 private function getBorder($id)
117 return sprintf('<bg=%s> </>', $this->colors[$this->started[$id]['border']]);
123 public function getName()
125 return 'debug_formatter';