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