1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyBuilder\Cronos\Formatter; |
4
|
|
|
|
5
|
|
|
class Output |
6
|
|
|
{ |
7
|
|
|
public const NO_FILE = '/dev/null'; |
8
|
|
|
|
9
|
|
|
/** @var bool */ |
10
|
|
|
private $noOutput = false; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
private $stdOutFile; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
private $stdOutAppend; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $stdErrFile; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
private $stdErrAppend; |
23
|
|
|
|
24
|
|
|
public function suppressOutput(): self |
25
|
|
|
{ |
26
|
|
|
$this->noOutput = true; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setStandardOutFile(string $filePath): self |
32
|
|
|
{ |
33
|
|
|
$this->stdOutFile = $filePath; |
34
|
|
|
$this->stdOutAppend = false; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function appendStandardOutToFile(string $filePath): self |
40
|
|
|
{ |
41
|
|
|
$this->stdOutFile = $filePath; |
42
|
|
|
$this->stdOutAppend = true; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param bool $append Either append or rewrite log file |
49
|
|
|
*/ |
50
|
|
|
public function setStandardErrorFile(string $filePath, bool $append = false): self |
51
|
|
|
{ |
52
|
|
|
$this->stdErrFile = $filePath; |
53
|
|
|
$this->stdErrAppend = $append; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function appendStandardErrorToFile(string $filePath): self |
59
|
|
|
{ |
60
|
|
|
$this->stdErrFile = $filePath; |
61
|
|
|
$this->stdErrAppend = true; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function format(): string |
67
|
|
|
{ |
68
|
|
|
if ($this->noOutput) { |
69
|
|
|
return $this->redirectStandardOutTo(self::NO_FILE) . $this->redirectStandardErrorTo(self::NO_FILE); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->createOutput(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function redirectStandardOutTo($filePath): string |
76
|
|
|
{ |
77
|
|
|
return $this->redirectOutputTo('', $this->stdOutAppend, $filePath); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function redirectOutputTo($out, $isAppend, $filePath): string |
81
|
|
|
{ |
82
|
|
|
$operator = $isAppend ? '>>' : '>'; |
83
|
|
|
|
84
|
|
|
return ' ' . $out . $operator . ' ' . $filePath; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function redirectStandardErrorTo($filePath): string |
88
|
|
|
{ |
89
|
|
|
return $this->redirectOutputTo('2', $this->stdErrAppend, $filePath); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function createOutput(): string |
93
|
|
|
{ |
94
|
|
|
$out = ''; |
95
|
|
|
|
96
|
|
|
if ($this->stdOutFile) { |
97
|
|
|
$out .= $this->redirectStandardOutTo($this->stdOutFile); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->stdErrFile) { |
101
|
|
|
$out .= $this->redirectStandardErrorTo($this->stdErrFile); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $out; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|