1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\SiteProcess\Util; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Process\Process; |
5
|
|
|
use Consolidation\Config\Util\Interpolator; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* RealtimeOutput can be provided to a process object when you want |
11
|
|
|
* to display the output of the running command as it is being produced. |
12
|
|
|
*/ |
13
|
|
|
class RealtimeOutputHandler |
14
|
|
|
{ |
15
|
|
|
protected $stdout; |
16
|
|
|
protected $stderr; |
17
|
|
|
protected $stdoutMarker = ''; |
18
|
|
|
protected $stderrMarker = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provide the output streams to use for stdout and stderr |
22
|
|
|
*/ |
23
|
|
|
const MARKER_ERR = '> '; |
24
|
|
|
|
25
|
|
|
public function __construct(OutputInterface $stdout, OutputInterface $stderr) |
26
|
|
|
{ |
27
|
|
|
$this->stdout = $stdout; |
28
|
|
|
$this->stderr = $stderr; |
29
|
|
|
|
30
|
|
|
$this->stdoutMarker = ''; |
31
|
|
|
$this->stderrMarker = self::MARKER_ERR; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This gives us an opportunity to adapt to the settings of the |
36
|
|
|
* process object (e.g. do we need to do anything differently if |
37
|
|
|
* it is in tty mode, etc.) |
38
|
|
|
*/ |
39
|
|
|
public function configure(Process $process) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* setStderrMarker defines the string that should be added at |
46
|
|
|
* the beginning of every line of stderr that is printed. |
47
|
|
|
*/ |
48
|
|
|
public function setStderrMarker($marker) |
49
|
|
|
{ |
50
|
|
|
$this->stderrMarker = $marker; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* setStdoutMarker defines the string that should be added at |
56
|
|
|
* the beginning of every line of stdout that is printed. |
57
|
|
|
*/ |
58
|
|
|
public function setStdoutMarker($marker) |
59
|
|
|
{ |
60
|
|
|
$this->stdoutMarker = $marker; |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* hideStdout overrides whatever was formerly stored in $this->stdout |
66
|
|
|
* with a null output buffer so that none of the standard output data |
67
|
|
|
* is visible. |
68
|
|
|
*/ |
69
|
|
|
public function hideStdout() |
70
|
|
|
{ |
71
|
|
|
$this->stdout = new NullOutput(); |
72
|
|
|
$this->stdoutMarker = ''; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* hideStderr serves the same function as hideStdout, but for the |
78
|
|
|
* standard error stream. Note that it is not useful to unconditionally |
79
|
|
|
* call both hideStdout and hideStderr; if no output is desired, then |
80
|
|
|
* the RealtimeOutputHandler should not be used. |
81
|
|
|
*/ |
82
|
|
|
public function hideStderr() |
83
|
|
|
{ |
84
|
|
|
$this->stderr = new NullOutput(); |
85
|
|
|
$this->stderrMarker = ''; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* If this object is used as a callable, then run 'handleOutput'. |
91
|
|
|
*/ |
92
|
|
|
public function __invoke($type, $buffer) |
93
|
|
|
{ |
94
|
|
|
$this->handleOutput($type, $buffer); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Helper method when you want real-time output from a Process call. |
99
|
|
|
* @param string $type |
100
|
|
|
* @param string $buffer |
101
|
|
|
*/ |
102
|
|
|
public function handleOutput($type, $buffer) |
103
|
|
|
{ |
104
|
|
|
if (Process::ERR === $type) { |
105
|
|
|
$this->stderr->write($this->addMarker($buffer, $this->stderrMarker), false, OutputInterface::OUTPUT_RAW); |
106
|
|
|
} else { |
107
|
|
|
$this->stdout->write($this->addMarker($buffer, $this->stdoutMarker), false, OutputInterface::OUTPUT_RAW); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Make sure that every line in $buffer begins with a MARKER_ERR. |
113
|
|
|
*/ |
114
|
|
|
protected function addMarker($buffer, $marker) |
115
|
|
|
{ |
116
|
|
|
// Exit early if there is no marker to add |
117
|
|
|
if (empty($marker)) { |
118
|
|
|
return $buffer; |
119
|
|
|
} |
120
|
|
|
// Add a marker on the beginning of every line. |
121
|
|
|
return $marker . rtrim(implode("\n" . $marker, explode("\n", $buffer)), $marker); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.