1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Console\Formatter;
13:
14: 15: 16: 17: 18: 19: 20:
21: class OutputFormatterStyle implements OutputFormatterStyleInterface
22: {
23: static private $availableForegroundColors = array(
24: 'black' => 30,
25: 'red' => 31,
26: 'green' => 32,
27: 'yellow' => 33,
28: 'blue' => 34,
29: 'magenta' => 35,
30: 'cyan' => 36,
31: 'white' => 37
32: );
33: static private $availableBackgroundColors = array(
34: 'black' => 40,
35: 'red' => 41,
36: 'green' => 42,
37: 'yellow' => 43,
38: 'blue' => 44,
39: 'magenta' => 45,
40: 'cyan' => 46,
41: 'white' => 47
42: );
43: static private $availableOptions = array(
44: 'bold' => 1,
45: 'underscore' => 4,
46: 'blink' => 5,
47: 'reverse' => 7,
48: 'conceal' => 8
49: );
50:
51: private $foreground;
52: private $background;
53: private $options = array();
54:
55: 56: 57: 58: 59: 60: 61: 62: 63:
64: public function __construct($foreground = null, $background = null, array $options = array())
65: {
66: if (null !== $foreground) {
67: $this->setForeground($foreground);
68: }
69: if (null !== $background) {
70: $this->setBackground($background);
71: }
72: if (count($options)) {
73: $this->setOptions($options);
74: }
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public function setForeground($color = null)
87: {
88: if (null === $color) {
89: $this->foreground = null;
90:
91: return;
92: }
93:
94: if (!isset(static::$availableForegroundColors[$color])) {
95: throw new \InvalidArgumentException(sprintf(
96: 'Invalid foreground color specified: "%s". Expected one of (%s)',
97: $color,
98: implode(', ', array_keys(static::$availableForegroundColors))
99: ));
100: }
101:
102: $this->foreground = static::$availableForegroundColors[$color];
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function setBackground($color = null)
115: {
116: if (null === $color) {
117: $this->background = null;
118:
119: return;
120: }
121:
122: if (!isset(static::$availableBackgroundColors[$color])) {
123: throw new \InvalidArgumentException(sprintf(
124: 'Invalid background color specified: "%s". Expected one of (%s)',
125: $color,
126: implode(', ', array_keys(static::$availableBackgroundColors))
127: ));
128: }
129:
130: $this->background = static::$availableBackgroundColors[$color];
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140: 141:
142: public function setOption($option)
143: {
144: if (!isset(static::$availableOptions[$option])) {
145: throw new \InvalidArgumentException(sprintf(
146: 'Invalid option specified: "%s". Expected one of (%s)',
147: $option,
148: implode(', ', array_keys(static::$availableOptions))
149: ));
150: }
151:
152: if (false === array_search(static::$availableOptions[$option], $this->options)) {
153: $this->options[] = static::$availableOptions[$option];
154: }
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164:
165: public function unsetOption($option)
166: {
167: if (!isset(static::$availableOptions[$option])) {
168: throw new \InvalidArgumentException(sprintf(
169: 'Invalid option specified: "%s". Expected one of (%s)',
170: $option,
171: implode(', ', array_keys(static::$availableOptions))
172: ));
173: }
174:
175: $pos = array_search(static::$availableOptions[$option], $this->options);
176: if (false !== $pos) {
177: unset($this->options[$pos]);
178: }
179: }
180:
181: 182: 183: 184: 185:
186: public function setOptions(array $options)
187: {
188: $this->options = array();
189:
190: foreach ($options as $option) {
191: $this->setOption($option);
192: }
193: }
194:
195: 196: 197: 198: 199: 200: 201:
202: public function apply($text)
203: {
204: $codes = array();
205:
206: if (null !== $this->foreground) {
207: $codes[] = $this->foreground;
208: }
209: if (null !== $this->background) {
210: $codes[] = $this->background;
211: }
212: if (count($this->options)) {
213: $codes = array_merge($codes, $this->options);
214: }
215:
216: return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
217: }
218: }
219: