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:
12: namespace Symfony\Component\Console\Output;
13:
14: use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15: use Symfony\Component\Console\Formatter\OutputFormatter;
16:
17: /**
18: * Base class for output classes.
19: *
20: * There are three levels of verbosity:
21: *
22: * * normal: no option passed (normal output - information)
23: * * verbose: -v (more output - debug)
24: * * quiet: -q (no output)
25: *
26: * @author Fabien Potencier <fabien@symfony.com>
27: *
28: * @api
29: */
30: abstract class Output implements OutputInterface
31: {
32: private $verbosity;
33: private $formatter;
34:
35: /**
36: * Constructor.
37: *
38: * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
39: * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
40: * @param OutputFormatterInterface $formatter Output formatter instance
41: *
42: * @api
43: */
44: public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
45: {
46: $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
47: $this->formatter = null === $formatter ? new OutputFormatter() : $formatter;
48: $this->formatter->setDecorated((Boolean) $decorated);
49: }
50:
51: /**
52: * Sets output formatter.
53: *
54: * @param OutputFormatterInterface $formatter
55: *
56: * @api
57: */
58: public function setFormatter(OutputFormatterInterface $formatter)
59: {
60: $this->formatter = $formatter;
61: }
62:
63: /**
64: * Returns current output formatter instance.
65: *
66: * @return OutputFormatterInterface
67: *
68: * @api
69: */
70: public function getFormatter()
71: {
72: return $this->formatter;
73: }
74:
75: /**
76: * Sets the decorated flag.
77: *
78: * @param Boolean $decorated Whether to decorate the messages or not
79: *
80: * @api
81: */
82: public function setDecorated($decorated)
83: {
84: $this->formatter->setDecorated((Boolean) $decorated);
85: }
86:
87: /**
88: * Gets the decorated flag.
89: *
90: * @return Boolean true if the output will decorate messages, false otherwise
91: *
92: * @api
93: */
94: public function isDecorated()
95: {
96: return $this->formatter->isDecorated();
97: }
98:
99: /**
100: * Sets the verbosity of the output.
101: *
102: * @param integer $level The level of verbosity
103: *
104: * @api
105: */
106: public function setVerbosity($level)
107: {
108: $this->verbosity = (int) $level;
109: }
110:
111: /**
112: * Gets the current verbosity of the output.
113: *
114: * @return integer The current level of verbosity
115: *
116: * @api
117: */
118: public function getVerbosity()
119: {
120: return $this->verbosity;
121: }
122:
123: /**
124: * Writes a message to the output and adds a newline at the end.
125: *
126: * @param string|array $messages The message as an array of lines of a single string
127: * @param integer $type The type of output
128: *
129: * @api
130: */
131: public function writeln($messages, $type = 0)
132: {
133: $this->write($messages, true, $type);
134: }
135:
136: /**
137: * Writes a message to the output.
138: *
139: * @param string|array $messages The message as an array of lines of a single string
140: * @param Boolean $newline Whether to add a newline or not
141: * @param integer $type The type of output
142: *
143: * @throws \InvalidArgumentException When unknown output type is given
144: *
145: * @api
146: */
147: public function write($messages, $newline = false, $type = 0)
148: {
149: if (self::VERBOSITY_QUIET === $this->verbosity) {
150: return;
151: }
152:
153: $messages = (array) $messages;
154:
155: foreach ($messages as $message) {
156: switch ($type) {
157: case OutputInterface::OUTPUT_NORMAL:
158: $message = $this->formatter->format($message);
159: break;
160: case OutputInterface::OUTPUT_RAW:
161: break;
162: case OutputInterface::OUTPUT_PLAIN:
163: $message = strip_tags($this->formatter->format($message));
164: break;
165: default:
166: throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
167: }
168:
169: $this->doWrite($message, $newline);
170: }
171: }
172:
173: /**
174: * Writes a message to the output.
175: *
176: * @param string $message A message to write to the output
177: * @param Boolean $newline Whether to add a newline or not
178: */
179: abstract public function doWrite($message, $newline);
180: }
181: