Completed
Push — master ( 67f6cd...e8ef9b )
by Greg
01:15
created

RealtimeOutputHandler::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace Consolidation\SiteProcess\Util;
3
4
use Consolidation\SiteAlias\AliasRecord;
5
use Symfony\Component\Process\Process;
6
use Consolidation\Config\Util\Interpolator;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Output\NullOutput;
9
10
/**
11
 * RealtimeOutput can be provided to a process object when you want
12
 * to display the output of the running command as it is being produced.
13
 */
14
class RealtimeOutputHandler
15
{
16
    protected $stdout;
17
    protected $stderr;
18
    protected $stdoutMarker = '';
19
    protected $stderrMarker = '';
20
21
    /**
22
     * Provide the output streams to use for stdout and stderr
23
     */
24
    const MARKER_ERR = '> ';
25
26
    public function __construct(OutputInterface $stdout, OutputInterface $stderr)
27
    {
28
        $this->stdout = $stdout;
29
        $this->stderr = $stderr;
30
31
        $this->stdoutMarker = '';
32
        $this->stderrMarker = self::MARKER_ERR;
33
    }
34
35
    /**
36
     * This gives us an opportunity to adapt to the settings of the
37
     * process object (e.g. do we need to do anything differently if
38
     * it is in tty mode, etc.)
39
     */
40
    public function configure(Process $process)
0 ignored issues
show
Unused Code introduced by
The parameter $process is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        return $this;
43
    }
44
45
    /**
46
     * setStderrMarker defines the string that should be added at
47
     * the beginning of every line of stderr that is printed.
48
     */
49
    public function setStderrMarker($marker)
50
    {
51
        $this->stderrMarker = $marker;
52
        return $this;
53
    }
54
55
    /**
56
     * setStdoutMarker defines the string that should be added at
57
     * the beginning of every line of stdout that is printed.
58
     */
59
    public function setStdoutMarker($marker)
60
    {
61
        $this->$stdoutMarker = $marker;
0 ignored issues
show
Bug introduced by
The variable $stdoutMarker does not exist. Did you mean $marker?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
62
        return $this;
63
    }
64
65
    /**
66
     * hideStdout overrides whatever was formerly stored in $this->stdout
67
     * with a null output buffer so that none of the standard output data
68
     * is visible.
69
     */
70
    public function hideStdout()
71
    {
72
        $this->stdout = new NullOutput();
73
        $this->stdoutMarker = '';
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
    }
87
88
    /**
89
     * If this object is used as a callable, then run 'handleOutput'.
90
     */
91
    public function __invoke($type, $buffer)
92
    {
93
        $this->handleOutput($type, $buffer);
94
    }
95
96
    /**
97
     * Helper method when you want real-time output from a Process call.
98
     * @param string $type
99
     * @param string $buffer
100
     */
101
    public function handleOutput($type, $buffer)
102
    {
103
        if (Process::ERR === $type) {
104
            $this->stderr->write($this->addMarker($buffer, $this->stderrMarker));
105
        } else {
106
            $this->stdout->write($this->addMarker($buffer, $this->stdoutMarker));
107
        }
108
    }
109
110
    /**
111
     * Make sure that every line in $buffer begins with a MARKER_ERR.
112
     */
113
    protected function addMarker($buffer, $marker)
114
    {
115
        // Exit early if there is no marker to add
116
        if (empty($marker)) {
117
            return $buffer;
118
        }
119
        // Add a marker on the beginning of every line.
120
        return $marker . rtrim(implode("\n" . $marker, explode("\n", $buffer)), $marker);
121
    }
122
}
123