TYPO3  7.6
OutputFormatterStyle.php
Go to the documentation of this file.
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\Formatter;
13 
22 {
23  private static $availableForegroundColors = array(
24  'black' => array('set' => 30, 'unset' => 39),
25  'red' => array('set' => 31, 'unset' => 39),
26  'green' => array('set' => 32, 'unset' => 39),
27  'yellow' => array('set' => 33, 'unset' => 39),
28  'blue' => array('set' => 34, 'unset' => 39),
29  'magenta' => array('set' => 35, 'unset' => 39),
30  'cyan' => array('set' => 36, 'unset' => 39),
31  'white' => array('set' => 37, 'unset' => 39),
32  'default' => array('set' => 39, 'unset' => 39),
33  );
34  private static $availableBackgroundColors = array(
35  'black' => array('set' => 40, 'unset' => 49),
36  'red' => array('set' => 41, 'unset' => 49),
37  'green' => array('set' => 42, 'unset' => 49),
38  'yellow' => array('set' => 43, 'unset' => 49),
39  'blue' => array('set' => 44, 'unset' => 49),
40  'magenta' => array('set' => 45, 'unset' => 49),
41  'cyan' => array('set' => 46, 'unset' => 49),
42  'white' => array('set' => 47, 'unset' => 49),
43  'default' => array('set' => 49, 'unset' => 49),
44  );
45  private static $availableOptions = array(
46  'bold' => array('set' => 1, 'unset' => 22),
47  'underscore' => array('set' => 4, 'unset' => 24),
48  'blink' => array('set' => 5, 'unset' => 25),
49  'reverse' => array('set' => 7, 'unset' => 27),
50  'conceal' => array('set' => 8, 'unset' => 28),
51  );
52 
53  private $foreground;
54  private $background;
55  private $options = array();
56 
66  public function __construct($foreground = null, $background = null, array $options = array())
67  {
68  if (null !== $foreground) {
69  $this->setForeground($foreground);
70  }
71  if (null !== $background) {
72  $this->setBackground($background);
73  }
74  if (count($options)) {
75  $this->setOptions($options);
76  }
77  }
78 
88  public function setForeground($color = null)
89  {
90  if (null === $color) {
91  $this->foreground = null;
92 
93  return;
94  }
95 
96  if (!isset(static::$availableForegroundColors[$color])) {
97  throw new \InvalidArgumentException(sprintf(
98  'Invalid foreground color specified: "%s". Expected one of (%s)',
99  $color,
100  implode(', ', array_keys(static::$availableForegroundColors))
101  ));
102  }
103 
104  $this->foreground = static::$availableForegroundColors[$color];
105  }
106 
116  public function setBackground($color = null)
117  {
118  if (null === $color) {
119  $this->background = null;
120 
121  return;
122  }
123 
124  if (!isset(static::$availableBackgroundColors[$color])) {
125  throw new \InvalidArgumentException(sprintf(
126  'Invalid background color specified: "%s". Expected one of (%s)',
127  $color,
128  implode(', ', array_keys(static::$availableBackgroundColors))
129  ));
130  }
131 
132  $this->background = static::$availableBackgroundColors[$color];
133  }
134 
144  public function setOption($option)
145  {
146  if (!isset(static::$availableOptions[$option])) {
147  throw new \InvalidArgumentException(sprintf(
148  'Invalid option specified: "%s". Expected one of (%s)',
149  $option,
150  implode(', ', array_keys(static::$availableOptions))
151  ));
152  }
153 
154  if (!in_array(static::$availableOptions[$option], $this->options)) {
155  $this->options[] = static::$availableOptions[$option];
156  }
157  }
158 
166  public function unsetOption($option)
167  {
168  if (!isset(static::$availableOptions[$option])) {
169  throw new \InvalidArgumentException(sprintf(
170  'Invalid option specified: "%s". Expected one of (%s)',
171  $option,
172  implode(', ', array_keys(static::$availableOptions))
173  ));
174  }
175 
176  $pos = array_search(static::$availableOptions[$option], $this->options);
177  if (false !== $pos) {
178  unset($this->options[$pos]);
179  }
180  }
181 
187  public function setOptions(array $options)
188  {
189  $this->options = array();
190 
191  foreach ($options as $option) {
192  $this->setOption($option);
193  }
194  }
195 
203  public function apply($text)
204  {
205  $setCodes = array();
206  $unsetCodes = array();
207 
208  if (null !== $this->foreground) {
209  $setCodes[] = $this->foreground['set'];
210  $unsetCodes[] = $this->foreground['unset'];
211  }
212  if (null !== $this->background) {
213  $setCodes[] = $this->background['set'];
214  $unsetCodes[] = $this->background['unset'];
215  }
216  if (count($this->options)) {
217  foreach ($this->options as $option) {
218  $setCodes[] = $option['set'];
219  $unsetCodes[] = $option['unset'];
220  }
221  }
222 
223  if (0 === count($setCodes)) {
224  return $text;
225  }
226 
227  return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
228  }
229 }