annotate vendor/symfony/console/Helper/DebugFormatterHelper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Helper;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Helps outputting debug information when running an external program from a command.
Chris@0 16 *
Chris@0 17 * An external program can be a Process, an HTTP request, or anything else.
Chris@0 18 *
Chris@0 19 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 20 */
Chris@0 21 class DebugFormatterHelper extends Helper
Chris@0 22 {
Chris@17 23 private $colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
Chris@17 24 private $started = [];
Chris@0 25 private $count = -1;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Starts a debug formatting session.
Chris@0 29 *
Chris@0 30 * @param string $id The id of the formatting session
Chris@0 31 * @param string $message The message to display
Chris@0 32 * @param string $prefix The prefix to use
Chris@0 33 *
Chris@0 34 * @return string
Chris@0 35 */
Chris@0 36 public function start($id, $message, $prefix = 'RUN')
Chris@0 37 {
Chris@17 38 $this->started[$id] = ['border' => ++$this->count % \count($this->colors)];
Chris@0 39
Chris@0 40 return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Adds progress to a formatting session.
Chris@0 45 *
Chris@0 46 * @param string $id The id of the formatting session
Chris@0 47 * @param string $buffer The message to display
Chris@0 48 * @param bool $error Whether to consider the buffer as error
Chris@0 49 * @param string $prefix The prefix for output
Chris@0 50 * @param string $errorPrefix The prefix for error output
Chris@0 51 *
Chris@0 52 * @return string
Chris@0 53 */
Chris@0 54 public function progress($id, $buffer, $error = false, $prefix = 'OUT', $errorPrefix = 'ERR')
Chris@0 55 {
Chris@0 56 $message = '';
Chris@0 57
Chris@0 58 if ($error) {
Chris@0 59 if (isset($this->started[$id]['out'])) {
Chris@0 60 $message .= "\n";
Chris@0 61 unset($this->started[$id]['out']);
Chris@0 62 }
Chris@0 63 if (!isset($this->started[$id]['err'])) {
Chris@0 64 $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix);
Chris@0 65 $this->started[$id]['err'] = true;
Chris@0 66 }
Chris@0 67
Chris@0 68 $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer);
Chris@0 69 } else {
Chris@0 70 if (isset($this->started[$id]['err'])) {
Chris@0 71 $message .= "\n";
Chris@0 72 unset($this->started[$id]['err']);
Chris@0 73 }
Chris@0 74 if (!isset($this->started[$id]['out'])) {
Chris@0 75 $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix);
Chris@0 76 $this->started[$id]['out'] = true;
Chris@0 77 }
Chris@0 78
Chris@0 79 $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer);
Chris@0 80 }
Chris@0 81
Chris@0 82 return $message;
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Stops a formatting session.
Chris@0 87 *
Chris@0 88 * @param string $id The id of the formatting session
Chris@0 89 * @param string $message The message to display
Chris@0 90 * @param bool $successful Whether to consider the result as success
Chris@0 91 * @param string $prefix The prefix for the end output
Chris@0 92 *
Chris@0 93 * @return string
Chris@0 94 */
Chris@0 95 public function stop($id, $message, $successful, $prefix = 'RES')
Chris@0 96 {
Chris@0 97 $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : '';
Chris@0 98
Chris@0 99 if ($successful) {
Chris@0 100 return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
Chris@0 101 }
Chris@0 102
Chris@0 103 $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message);
Chris@0 104
Chris@0 105 unset($this->started[$id]['out'], $this->started[$id]['err']);
Chris@0 106
Chris@0 107 return $message;
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * @param string $id The id of the formatting session
Chris@0 112 *
Chris@0 113 * @return string
Chris@0 114 */
Chris@0 115 private function getBorder($id)
Chris@0 116 {
Chris@0 117 return sprintf('<bg=%s> </>', $this->colors[$this->started[$id]['border']]);
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * {@inheritdoc}
Chris@0 122 */
Chris@0 123 public function getName()
Chris@0 124 {
Chris@0 125 return 'debug_formatter';
Chris@0 126 }
Chris@0 127 }