|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\PSR3\Stdio; |
|
4
|
|
|
|
|
5
|
|
|
use Clue\React\Stdio\Stdio; |
|
6
|
|
|
use Psr\Log\AbstractLogger; |
|
7
|
|
|
use React\EventLoop\LoopInterface; |
|
8
|
|
|
use React\Stream\WritableResourceStream; |
|
9
|
|
|
use React\Stream\WritableStreamInterface; |
|
10
|
|
|
use function WyriHaximus\PSR3\checkCorrectLogLevel; |
|
11
|
|
|
use function WyriHaximus\PSR3\processPlaceHolders; |
|
12
|
|
|
|
|
13
|
|
|
final class StdioLogger extends AbstractLogger |
|
14
|
|
|
{ |
|
15
|
|
|
const NEW_LINE = PHP_EOL; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Stdio |
|
19
|
|
|
*/ |
|
20
|
|
|
private $stdio; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
private $hideLevel = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private $newLine = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param WritableStreamInterface $stream |
|
34
|
|
|
* |
|
35
|
|
|
* @internal |
|
36
|
|
|
*/ |
|
37
|
19 |
|
public function __construct(WritableStreamInterface $stream) |
|
38
|
|
|
{ |
|
39
|
19 |
|
$this->stdio = $stream; |
|
|
|
|
|
|
40
|
19 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param LoopInterface $loop |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public static function create(LoopInterface $loop): StdioLogger |
|
46
|
|
|
{ |
|
47
|
2 |
|
return new self(new WritableResourceStream(STDOUT, $loop)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function withHideLevel(bool $hideLevel): StdioLogger |
|
51
|
|
|
{ |
|
52
|
2 |
|
$clone = clone $this; |
|
53
|
2 |
|
$clone->hideLevel = $hideLevel; |
|
54
|
|
|
|
|
55
|
2 |
|
return $clone; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function withNewLine(bool $newLine): StdioLogger |
|
59
|
|
|
{ |
|
60
|
2 |
|
$clone = clone $this; |
|
61
|
2 |
|
$clone->newLine = $newLine; |
|
62
|
|
|
|
|
63
|
2 |
|
return $clone; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
18 |
|
public function log($level, $message, array $context = []) |
|
67
|
|
|
{ |
|
68
|
18 |
|
checkCorrectLogLevel($level); |
|
69
|
17 |
|
$message = (string)$message; |
|
70
|
17 |
|
$message = processPlaceHolders($message, $context); |
|
71
|
17 |
|
if ($this->hideLevel === false) { |
|
72
|
15 |
|
$message = $level . ' ' . $message; |
|
73
|
|
|
} |
|
74
|
17 |
|
if ($this->newLine === true) { |
|
75
|
2 |
|
$message .= self::NEW_LINE; |
|
76
|
|
|
} |
|
77
|
17 |
|
$this->stdio->write($message); |
|
78
|
17 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..