Completed
Push — master ( 501ab6...26d05c )
by Greg
08:40
created

ProcessBase::getOutputAsJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Consolidation\SiteProcess;
4
5
use Drush\Drush;
6
use Psr\Log\LoggerInterface;
7
use Robo\Common\IO;
8
use Symfony\Component\Console\Style\OutputStyle;
9
use Symfony\Component\Process\Process;
10
use Consolidation\SiteProcess\Util\RealtimeOutputHandler;
11
use Symfony\Component\Console\Output\ConsoleOutputInterface;
12
13
/**
14
 * A wrapper around Symfony Process.
15
 *
16
 * - Supports simulated mode. Typically enabled via a --simulate option.
17
 * - Supports verbose mode - logs all runs.
18
 */
19
class ProcessBase extends Process
20
{
21
    /**
22
     * @var OutputStyle
23
     */
24
    protected $output;
25
26
    /**
27
     * @var OutputInterface
28
     */
29
    protected $stderr;
30
31
    private $simulated = false;
32
33
    private $verbose = false;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * realtimeStdout returns the output stream that realtime output
42
     * should be sent to (if applicable)
43
     *
44
     * @return OutputStyle $output
45
     */
46
    public function realtimeStdout()
47
    {
48
        return $this->output;
49
    }
50
51
    protected function realtimeStderr()
52
    {
53
        if ($this->stderr) {
54
            return $this->stderr;
55
        }
56
        if (method_exists($this->output, 'getErrorStyle')) {
57
            return $this->output->getErrorStyle();
58
        }
59
60
        return $this->realtimeStdout();
61
    }
62
63
    /**
64
     * setRealtimeOutput allows the caller to inject an OutputStyle object
65
     * that will be used to stream realtime output if applicable.
66
     *
67
     * @param OutputStyle $output
68
     */
69
    public function setRealtimeOutput($output, $stderr = null)
70
    {
71
        $this->output = $output;
72
        $this->stderr = $stderr instanceof ConsoleOutputInterface ? $stderr->getErrorOutput() : $stderr;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function getVerbose()
79
    {
80
        return $this->verbose;
81
    }
82
83
    /**
84
     * @param bool $verbose
85
     */
86
    public function setVerbose($verbose)
87
    {
88
        $this->verbose = $verbose;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function getSimulated()
95
    {
96
        return $this->simulated;
97
    }
98
99
    /**
100
     * @param bool $simulated
101
     */
102
    public function setSimulated($simulated)
103
    {
104
        $this->simulated = $simulated;
105
    }
106
107
    /**
108
     * @return LoggerInterface
109
     */
110
    public function getLogger()
111
    {
112
        return $this->logger;
113
    }
114
115
    /**
116
     * @param LoggerInterface $logger
117
     */
118
    public function setLogger($logger)
119
    {
120
        $this->logger = $logger;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function start(callable $callback = null)
127
    {
128
        $cmd = $this->getCommandLine();
129
        if ($this->getSimulated()) {
130
            $this->getLogger()->notice('Simulating: ' . $cmd);
131
            // Run a command that always succeeds.
132
            $this->setCommandLine('exit 0');
133
        } elseif ($this->getVerbose()) {
134
            $this->getLogger()->info('Executing: ' . $cmd);
135
        }
136
        parent::start($callback);
137
        // Set command back to original value in case anyone asks.
138
        if ($this->getSimulated()) {
139
            $this->setCommandLine($cmd);
140
        }
141
    }
142
143
    /**
144
     * Get Process output and decode its JSON.
145
     *
146
     * @return array
147
     *   An associative array.
148
     */
149
    public function getOutputAsJson()
150
    {
151
        if (!$output = $this->getOutput()) {
152
            throw new \InvalidArgumentException('Output is empty.');
153
        }
154
        // @todo strip any surrounding strings from the JSON.
155
        if (!$json = json_decode($output, true)) {
156
            throw new \InvalidArgumentException('Unable to decode output into JSON.');
157
        }
158
        return $json;
159
    }
160
161
    /**
162
     * Return a realTime output object.
163
     *
164
     * @return callable
165
     */
166
    public function showRealtime()
167
    {
168
        $realTimeOutput = new RealtimeOutputHandler($this->realtimeStdout(), $this->realtimeStderr());
169
        $realTimeOutput->configure($this);
170
        return [$realTimeOutput, 'handleOutput'];
171
    }
172
}
173