1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the box project. |
5
|
|
|
* |
6
|
|
|
* (c) Kevin Herrera <[email protected]> |
7
|
|
|
* Théo Fidry <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the MIT license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace KevinGH\RequirementChecker; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run. |
17
|
|
|
* |
18
|
|
|
* @private |
19
|
|
|
*/ |
20
|
|
|
final class Printer |
21
|
|
|
{ |
22
|
|
|
private $styles = array( |
23
|
|
|
'reset' => "\033[0m", |
24
|
|
|
'red' => "\033[31m", |
25
|
|
|
'green' => "\033[32m", |
26
|
|
|
'yellow' => "\033[33m", |
27
|
|
|
'title' => "\033[33m", |
28
|
|
|
'error' => "\033[37;41m", |
29
|
|
|
'success' => "\033[30;42m", |
30
|
|
|
); |
31
|
|
|
private $verbosity; |
32
|
|
|
private $supportColors; |
33
|
|
|
private $width; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $verbosity |
37
|
|
|
* @param bool $supportColors |
38
|
|
|
* @param null|int $width |
39
|
|
|
*/ |
40
|
|
|
public function __construct($verbosity, $supportColors, $width = null) |
41
|
|
|
{ |
42
|
|
|
if (null === $width) { |
43
|
|
|
$terminal = new Terminal(); |
44
|
|
|
$width = \min($terminal->getWidth(), 80); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->verbosity = $verbosity; |
48
|
|
|
$this->supportColors = $supportColors; |
49
|
|
|
$this->width = $width; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function getVerbosity() |
56
|
|
|
{ |
57
|
|
|
return $this->verbosity; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $verbosity |
62
|
|
|
*/ |
63
|
|
|
public function setVerbosity($verbosity) |
64
|
|
|
{ |
65
|
|
|
$this->verbosity = $verbosity; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $title |
70
|
|
|
* @param int $verbosity |
71
|
|
|
* @param null|string $style |
72
|
|
|
*/ |
73
|
|
|
public function title($title, $verbosity, $style = null) |
74
|
|
|
{ |
75
|
|
|
if (null === $style) { |
76
|
|
|
$style = 'title'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->printvln('', $verbosity, $style); |
80
|
|
|
$this->printvln($title, $verbosity, $style); |
81
|
|
|
$this->printvln( |
82
|
|
|
\str_repeat( |
83
|
|
|
'=', |
84
|
|
|
\min(\strlen($title), $this->width) |
85
|
|
|
), |
86
|
|
|
$verbosity, |
87
|
|
|
$style |
88
|
|
|
); |
89
|
|
|
$this->printvln('', $verbosity, $style); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Requirement $requirement |
94
|
|
|
* |
95
|
|
|
* @return null|string |
96
|
|
|
*/ |
97
|
|
|
public function getRequirementErrorMessage(Requirement $requirement) |
98
|
|
|
{ |
99
|
|
|
if ($requirement->isFulfilled()) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$errorMessage = \wordwrap($requirement->getTestMessage(), $this->width - 3, \PHP_EOL.' ').\PHP_EOL; |
104
|
|
|
|
105
|
|
|
return $errorMessage; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $title |
110
|
|
|
* @param string $message |
111
|
|
|
* @param int $verbosity |
112
|
|
|
* @param null|string $style |
113
|
|
|
*/ |
114
|
|
|
public function block($title, $message, $verbosity, $style = null) |
115
|
|
|
{ |
116
|
|
|
$prefix = ' ['.$title.'] '; |
117
|
|
|
$message = $prefix.\trim($message); |
118
|
|
|
|
119
|
|
|
$lines = array(); |
120
|
|
|
|
121
|
|
|
$remainingMessage = $message; |
122
|
|
|
|
123
|
|
|
while (\strlen($remainingMessage) > 0) { |
124
|
|
|
$wrapped = \wordwrap($remainingMessage, $this->width - 3, '¬'); |
125
|
|
|
$exploded = \explode('¬', $wrapped); |
126
|
|
|
$line = $exploded[0]; |
127
|
|
|
$remainingMessage = \ltrim(\substr($remainingMessage, \strlen($line))); |
128
|
|
|
|
129
|
|
|
if (\strlen($remainingMessage) > 0) { |
130
|
|
|
$remainingMessage = \str_repeat(' ', \strlen($prefix)).$remainingMessage; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$lines[] = \str_pad($line, $this->width, ' ', STR_PAD_RIGHT); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->printvln('', $verbosity); |
137
|
|
|
$this->printvln(\str_repeat(' ', $this->width), $verbosity, $style); |
138
|
|
|
foreach ($lines as $line) { |
139
|
|
|
$this->printvln($line, $verbosity, $style); |
140
|
|
|
} |
141
|
|
|
$this->printv(\str_repeat(' ', $this->width), $verbosity, $style); |
142
|
|
|
$this->printvln('', $verbosity); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $message |
147
|
|
|
* @param int $verbosity |
148
|
|
|
* @param null|string $style |
149
|
|
|
*/ |
150
|
|
|
public function printvln($message, $verbosity, $style = null) |
151
|
|
|
{ |
152
|
|
|
$this->printv($message, $verbosity, $style); |
153
|
|
|
$this->printv(\PHP_EOL, $verbosity, null); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $message |
158
|
|
|
* @param int $verbosity |
159
|
|
|
* @param null|string $style |
160
|
|
|
*/ |
161
|
|
|
public function printv($message, $verbosity, $style = null) |
162
|
|
|
{ |
163
|
|
|
if ($verbosity > $this->verbosity) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$message = \wordwrap($message, $this->width); |
168
|
|
|
|
169
|
|
|
$message = \sprintf( |
170
|
|
|
'%s%s%s', |
171
|
|
|
$this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', |
172
|
|
|
$message, |
173
|
|
|
$this->supportColors ? $this->styles['reset'] : '' |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
echo $message; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|