Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Helper; Chris@0: Chris@0: /** Chris@0: * Helps outputting debug information when running an external program from a command. Chris@0: * Chris@0: * An external program can be a Process, an HTTP request, or anything else. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class DebugFormatterHelper extends Helper Chris@0: { Chris@17: private $colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default']; Chris@17: private $started = []; Chris@0: private $count = -1; Chris@0: Chris@0: /** Chris@0: * Starts a debug formatting session. Chris@0: * Chris@0: * @param string $id The id of the formatting session Chris@0: * @param string $message The message to display Chris@0: * @param string $prefix The prefix to use Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function start($id, $message, $prefix = 'RUN') Chris@0: { Chris@17: $this->started[$id] = ['border' => ++$this->count % \count($this->colors)]; Chris@0: Chris@0: return sprintf("%s %s %s\n", $this->getBorder($id), $prefix, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds progress to a formatting session. Chris@0: * Chris@0: * @param string $id The id of the formatting session Chris@0: * @param string $buffer The message to display Chris@0: * @param bool $error Whether to consider the buffer as error Chris@0: * @param string $prefix The prefix for output Chris@0: * @param string $errorPrefix The prefix for error output Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function progress($id, $buffer, $error = false, $prefix = 'OUT', $errorPrefix = 'ERR') Chris@0: { Chris@0: $message = ''; Chris@0: Chris@0: if ($error) { Chris@0: if (isset($this->started[$id]['out'])) { Chris@0: $message .= "\n"; Chris@0: unset($this->started[$id]['out']); Chris@0: } Chris@0: if (!isset($this->started[$id]['err'])) { Chris@0: $message .= sprintf('%s %s ', $this->getBorder($id), $errorPrefix); Chris@0: $this->started[$id]['err'] = true; Chris@0: } Chris@0: Chris@0: $message .= str_replace("\n", sprintf("\n%s %s ", $this->getBorder($id), $errorPrefix), $buffer); Chris@0: } else { Chris@0: if (isset($this->started[$id]['err'])) { Chris@0: $message .= "\n"; Chris@0: unset($this->started[$id]['err']); Chris@0: } Chris@0: if (!isset($this->started[$id]['out'])) { Chris@0: $message .= sprintf('%s %s ', $this->getBorder($id), $prefix); Chris@0: $this->started[$id]['out'] = true; Chris@0: } Chris@0: Chris@0: $message .= str_replace("\n", sprintf("\n%s %s ", $this->getBorder($id), $prefix), $buffer); Chris@0: } Chris@0: Chris@0: return $message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stops a formatting session. Chris@0: * Chris@0: * @param string $id The id of the formatting session Chris@0: * @param string $message The message to display Chris@0: * @param bool $successful Whether to consider the result as success Chris@0: * @param string $prefix The prefix for the end output Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function stop($id, $message, $successful, $prefix = 'RES') Chris@0: { Chris@0: $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : ''; Chris@0: Chris@0: if ($successful) { Chris@0: return sprintf("%s%s %s %s\n", $trailingEOL, $this->getBorder($id), $prefix, $message); Chris@0: } Chris@0: Chris@0: $message = sprintf("%s%s %s %s\n", $trailingEOL, $this->getBorder($id), $prefix, $message); Chris@0: Chris@0: unset($this->started[$id]['out'], $this->started[$id]['err']); Chris@0: Chris@0: return $message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $id The id of the formatting session Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: private function getBorder($id) Chris@0: { Chris@0: return sprintf(' ', $this->colors[$this->started[$id]['border']]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'debug_formatter'; Chris@0: } Chris@0: }