MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Output.php
Ir para a documentação deste ficheiro.
1<?php
2
4
5/*
6 * This file is part of the Symfony framework.
7 *
8 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9 *
10 * This source file is subject to the MIT license that is bundled
11 * with this source code in the file LICENSE.
12 */
13
25abstract class Output implements OutputInterface
26{
27 const VERBOSITY_QUIET = 0;
30
31 const OUTPUT_NORMAL = 0;
32 const OUTPUT_RAW = 1;
33 const OUTPUT_PLAIN = 2;
34
35 protected $verbosity;
36 protected $decorated;
37
38 static protected $styles = array(
39 'error' => array('bg' => 'red', 'fg' => 'white'),
40 'info' => array('fg' => 'green'),
41 'comment' => array('fg' => 'yellow'),
42 'question' => array('bg' => 'cyan', 'fg' => 'black'),
43 );
44 static protected $options = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8);
45 static protected $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37);
46 static protected $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
47
54 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null)
55 {
56 $this->decorated = (Boolean) $decorated;
57 $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
58 }
59
66 static public function setStyle($name, $options = array())
67 {
68 static::$styles[strtolower($name)] = $options;
69 }
70
76 public function setDecorated($decorated)
77 {
78 $this->decorated = (Boolean) $decorated;
79 }
80
86 public function isDecorated()
87 {
88 return $this->decorated;
89 }
90
96 public function setVerbosity($level)
97 {
98 $this->verbosity = (int) $level;
99 }
100
106 public function getVerbosity()
107 {
108 return $this->verbosity;
109 }
110
117 public function writeln($messages, $type = 0)
118 {
119 $this->write($messages, true, $type);
120 }
121
131 public function write($messages, $newline = false, $type = 0)
132 {
133 if (self::VERBOSITY_QUIET === $this->verbosity) {
134 return;
135 }
136
137 if (!is_array($messages)) {
138 $messages = array($messages);
139 }
140
141 foreach ($messages as $message) {
142 switch ($type) {
144 $message = $this->format($message);
145 break;
147 break;
149 $message = strip_tags($this->format($message));
150 break;
151 default:
152 throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
153 }
154
155 $this->doWrite($message, $newline);
156 }
157 }
158
165 abstract public function doWrite($message, $newline);
166
174 protected function format($message)
175 {
176 $message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);
177
178 return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
179 }
180
184 protected function replaceStartStyle($match)
185 {
186 if (!$this->decorated) {
187 return '';
188 }
189
190 if (isset(static::$styles[strtolower($match[1])])) {
191 $parameters = static::$styles[strtolower($match[1])];
192 } else {
193 // bg=blue;fg=red
194 if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
195 throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
196 }
197
198 $parameters = array();
199 foreach ($matches as $match) {
200 $parameters[$match[1]] = $match[2];
201 }
202 }
203
204 $codes = array();
205
206 if (isset($parameters['fg'])) {
207 $codes[] = static::$foreground[$parameters['fg']];
208 }
209
210 if (isset($parameters['bg'])) {
211 $codes[] = static::$background[$parameters['bg']];
212 }
213
214 foreach (static::$options as $option => $value) {
215 if (isset($parameters[$option]) && $parameters[$option]) {
216 $codes[] = $value;
217 }
218 }
219
220 return "\033[".implode(';', $codes).'m';
221 }
222
223 protected function replaceEndStyle($match)
224 {
225 if (!$this->decorated) {
226 return '';
227 }
228
229 return "\033[0m";
230 }
231}
__construct($verbosity=self::VERBOSITY_NORMAL, $decorated=null)
Definição Output.php:54
static setStyle($name, $options=array())
Definição Output.php:66
write($messages, $newline=false, $type=0)
Definição Output.php:131