1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
5
|
|
|
*/ |
6
|
|
|
namespace Vaimo\ComposerPatches; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
10
|
|
|
*/ |
11
|
|
|
class Logger |
12
|
|
|
{ |
13
|
|
|
const TYPE_NONE = 'none'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Composer\IO\IOInterface $appIO |
17
|
|
|
*/ |
18
|
|
|
private $appIO; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $indentationStack = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $muteDepth = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Composer\IO\IOInterface $appIO |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
\Composer\IO\IOInterface $appIO |
35
|
|
|
) { |
36
|
|
|
$this->appIO = $appIO; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function writeRaw($message, array $args = array()) |
40
|
|
|
{ |
41
|
|
|
if ($this->muteDepth) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$prefix = $this->getIndentationString(); |
46
|
|
|
|
47
|
|
|
$lines = array_map(function ($line) use ($prefix) { |
48
|
|
|
return $prefix . $line; |
49
|
|
|
}, explode(PHP_EOL, $message)); |
50
|
|
|
|
51
|
|
|
$prefixedMessage = implode(PHP_EOL, $lines); |
52
|
|
|
|
53
|
|
|
$this->appIO->write( |
54
|
|
|
!$args ? $prefixedMessage : vsprintf($prefixedMessage, $args) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function write($type, $message, array $args = array()) |
59
|
|
|
{ |
60
|
|
|
$this->writeRaw($this->createTag($type, $message), $args); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function writeNotice($type, $message, array $args = array()) |
64
|
|
|
{ |
65
|
|
|
if (!is_array($message)) { |
66
|
|
|
$message = array($message); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$length = 0; |
70
|
|
|
|
71
|
|
|
foreach ($message as $item) { |
72
|
|
|
$length = max(min(80, strlen($item)), $length); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($message as $item) { |
76
|
|
|
$this->write($type, str_pad($item, $length, ' '), $args); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function writeVerbose($type, $message, array $args = array()) |
81
|
|
|
{ |
82
|
|
|
if (!$this->appIO->isVerbose()) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->write($type, $message, $args); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function writeException(\Exception $exception) |
90
|
|
|
{ |
91
|
|
|
if (!$this->appIO->isVerbose()) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->write('error', trim($exception->getMessage(), PHP_EOL . ' ')); |
96
|
|
|
$this->write('', trim($exception->getTraceAsString(), PHP_EOL . ' ')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function writeNewLine() |
100
|
|
|
{ |
101
|
|
|
if ($this->muteDepth) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->appIO->write(''); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function createTag($type, $contents) |
109
|
|
|
{ |
110
|
|
|
if (!$type || $type === \Vaimo\ComposerPatches\Logger::TYPE_NONE) { |
111
|
|
|
return $contents; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return '<' . $type . '>' . $contents . '</' . $type . '>'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getIndentationString() |
118
|
|
|
{ |
119
|
|
|
return str_pad('', count($this->indentationStack) * 2, ' ') . end($this->indentationStack); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function writeIndentation() |
123
|
|
|
{ |
124
|
|
|
if ($this->muteDepth) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->appIO->write( |
129
|
|
|
$this->getIndentationString(), |
130
|
|
|
false |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function reset($index = 0) |
135
|
|
|
{ |
136
|
|
|
$this->indentationStack = array_slice($this->indentationStack, 0, $index); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function push($prefix = '') |
140
|
|
|
{ |
141
|
|
|
$index = count($this->indentationStack); |
142
|
|
|
|
143
|
|
|
$this->indentationStack[] = $prefix ? ($prefix . ' ') : ''; |
144
|
|
|
|
145
|
|
|
return $index; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function pop() |
149
|
|
|
{ |
150
|
|
|
array_pop($this->indentationStack); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return \Composer\IO\IOInterface|\Composer\IO\ConsoleIO |
155
|
|
|
*/ |
156
|
|
|
public function getOutputInstance() |
157
|
|
|
{ |
158
|
|
|
return $this->appIO; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function isMuted() |
162
|
|
|
{ |
163
|
|
|
return (bool)$this->muteDepth; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function mute() |
167
|
|
|
{ |
168
|
|
|
return $this->muteDepth++; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function unMute($muteDepth = null) |
172
|
|
|
{ |
173
|
|
|
$this->muteDepth = $muteDepth ?: max(--$this->muteDepth, 0); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function escape($message) |
177
|
|
|
{ |
178
|
|
|
return str_replace(array('%'), array('%%'), $message); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|