Completed
Pull Request — master (#217)
by Thomas
03:10
created

HtmlOutputFormatter::setStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace Owncloud\Updater\Formatter;
24
25
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
26
use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
27
28
class HtmlOutputFormatter implements OutputFormatterInterface {
29
30
	const PATTERN = "/\[(([\d+];?)*)m(.*?)\[(([\d+];?)*)m/i";
31
32
	static private $styles = array(
33
		'30' => 'color:rgba(0,0,0,1)',
34
		'31' => 'color:rgba(230,50,50,1)',
35
		'32' => 'color:rgba(50,230,50,1)',
36
		'33' => 'color:rgba(230,230,50,1)',
37
		'34' => 'color:rgba(50,50,230,1)',
38
		'35' => 'color:rgba(230,50,150,1)',
39
		'36' => 'color:rgba(50,230,230,1)',
40
		'37' => 'color:rgba(250,250,250,1)',
41
		'40' => 'color:rgba(0,0,0,1)',
42
		'41' => 'background-color:rgba(230,50,50,1)',
43
		'42' => 'background-color:rgba(50,230,50,1)',
44
		'43' => 'background-color:rgba(230,230,50,1)',
45
		'44' => 'background-color:rgba(50,50,230,1)',
46
		'45' => 'background-color:rgba(230,50,150,1)',
47
		'46' => 'background-color:rgba(50,230,230,1)',
48
		'47' => 'background-color:rgba(250,250,250,1)',
49
		'1' => 'font-weight:bold',
50
		'4' => 'text-decoration:underline',
51
		'8' => 'visibility:hidden',
52
	);
53
	private $formatter;
54
55
	public function __construct($formatter){
56
		$this->formatter = $formatter;
57
	}
58
59
	public function setDecorated($decorated){
60
		return $this->formatter->setDecorated($decorated);
61
	}
62
63
	public function isDecorated(){
64
		return $this->formatter->isDecorated();
65
	}
66
67
	public function setStyle($name, OutputFormatterStyleInterface $style){
68
		return $this->formatter->setStyle($name, $style);
69
	}
70
71
	public function hasStyle($name){
72
		return $this->formatter->hasStyle($name);
73
	}
74
75
	public function getStyle($name){
76
		return $this->formatter->getStyle($name);
77
	}
78
79
	public function format($message){
80
		$formatted = $this->formatter->format($message);
81
		$escaped = htmlspecialchars($formatted, ENT_QUOTES, 'UTF-8');
82
		$converted = preg_replace_callback(self::PATTERN, array($this, 'replaceFormat'), $escaped);
83
84
		return $converted;
85
	}
86
87
	protected function replaceFormat($matches){
88
		$text = $matches[3];
89
		$styles = explode(';', $matches[1]);
90
		$css = array();
91
92
		foreach ($styles as $style){
93
			if (isset(self::$styles[$style])){
94
				$css[] = self::$styles[$style];
95
			}
96
		}
97
98
		return sprintf('<span style="%s">%s</span>', implode(';', $css), $text);
99
	}
100
101
}
102