1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\RfcLinc\Application\Cli; |
6
|
|
|
|
7
|
|
|
use EngineWorks\ProgressStatus\ProgressInterface; |
8
|
|
|
use EngineWorks\ProgressStatus\Status; |
9
|
|
|
use Psr\Log\AbstractLogger; |
10
|
|
|
use Psr\Log\LogLevel; |
11
|
|
|
use SplObserver; |
12
|
|
|
use SplSubject; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class OutputLogger extends AbstractLogger implements SplObserver |
16
|
|
|
{ |
17
|
|
|
/** @var OutputInterface */ |
18
|
|
|
private $output; |
19
|
|
|
|
20
|
4 |
|
public function __construct(OutputInterface $output) |
21
|
|
|
{ |
22
|
4 |
|
$this->output = $output; |
23
|
4 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function output(): OutputInterface |
26
|
|
|
{ |
27
|
1 |
|
return $this->output; |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function debug($message, array $context = []) |
31
|
|
|
{ |
32
|
3 |
|
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
33
|
3 |
|
parent::debug($message, $context); |
34
|
|
|
} |
35
|
3 |
|
} |
36
|
|
|
|
37
|
3 |
|
public function log($level, $message, array $context = []) |
38
|
|
|
{ |
39
|
3 |
|
$style = $this->styleFromLogLevel((string) $level); |
40
|
3 |
|
$message = $this->decorate($message, $style); |
41
|
3 |
|
$this->output->writeln($message); |
42
|
3 |
|
} |
43
|
|
|
|
44
|
4 |
|
public function styleFromLogLevel(string $level): string |
45
|
|
|
{ |
46
|
4 |
|
if (LogLevel::WARNING === $level) { |
47
|
1 |
|
return 'info'; |
48
|
|
|
} |
49
|
4 |
|
if (LogLevel::NOTICE === $level) { |
50
|
4 |
|
return 'comment'; |
51
|
|
|
} |
52
|
4 |
|
if (LogLevel::INFO === $level || LogLevel::DEBUG === $level) { |
53
|
4 |
|
return ''; |
54
|
|
|
} |
55
|
1 |
|
return 'error'; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
public function decorate(string $message, string $type = ''): string |
59
|
|
|
{ |
60
|
6 |
|
$decorated = str_replace('<', '\<', $message); |
61
|
6 |
|
if ('' !== $type) { |
62
|
4 |
|
$decorated = '<' . $type . '>' . $decorated . '</' . $type . '>'; |
63
|
|
|
} |
64
|
6 |
|
return $decorated; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param SplSubject|ProgressInterface $subject |
69
|
|
|
*/ |
70
|
2 |
|
public function update(SplSubject $subject) |
71
|
|
|
{ |
72
|
2 |
|
if (! $subject instanceof ProgressInterface) { |
73
|
1 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$this->info($this->statusToString($subject->getStatus())); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
public function statusToString(Status $status) |
80
|
|
|
{ |
81
|
1 |
|
return vsprintf('Processed %s lines in %s [%d / sec]', [ |
82
|
1 |
|
number_format($status->getValue()), |
83
|
1 |
|
$status->getIntervalElapsed()->format('%h:%I:%S'), |
84
|
1 |
|
number_format($status->getSpeed()), |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|