1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Karma\Logging; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
trait OutputAware |
10
|
|
|
{ |
11
|
|
|
protected ?OutputInterface |
|
|
|
|
12
|
|
|
$output = null; |
13
|
|
|
|
14
|
40 |
|
public function setOutput(OutputInterface $output) |
15
|
|
|
{ |
16
|
40 |
|
$this->output = $output; |
17
|
|
|
|
18
|
40 |
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
protected function error($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void |
22
|
|
|
{ |
23
|
1 |
|
$this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'red'); |
24
|
1 |
|
} |
25
|
|
|
|
26
|
1 |
|
protected function warning($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void |
27
|
|
|
{ |
28
|
1 |
|
$this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'yellow'); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
1 |
|
protected function info($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void |
32
|
|
|
{ |
33
|
1 |
|
$this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'white'); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
1 |
|
protected function debug($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void |
37
|
|
|
{ |
38
|
1 |
|
$this->write($messages, $newline, $type, OutputInterface::VERBOSITY_VERBOSE, 'white'); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
4 |
|
private function write($messages, $newline, $type, $verbosity, $textColor): void |
42
|
|
|
{ |
43
|
4 |
|
if($this->output instanceof OutputInterface) |
44
|
|
|
{ |
45
|
4 |
|
if($verbosity <= $this->output->getVerbosity()) |
46
|
|
|
{ |
47
|
4 |
|
if(! is_array($messages)) |
48
|
|
|
{ |
49
|
4 |
|
$messages = array($messages); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
array_walk($messages, function(& $message) use($textColor) { |
53
|
4 |
|
$message = "<fg=$textColor>$message</fg=$textColor>"; |
54
|
4 |
|
}); |
55
|
|
|
|
56
|
4 |
|
$this->output->write($messages, $newline, $type); |
57
|
|
|
} |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
} |
61
|
|
|
|