Completed
Push — master ( eb9a89...bc6a95 )
by Greg
438:19 queued 421:28
created

ProcessBase::setRealtimeOutput()   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 1
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\RealtimeOutput;
11
12
/**
13
 * A wrapper around Symfony Process.
14
 *
15
 * - Supports simulated mode. Typically enabled via a --simulate option.
16
 * - Supports verbose mode - logs all runs.
17
 */
18
class ProcessBase extends Process
19
{
20
    /**
21
     * @var OutputStyle
22
     */
23
    protected $output;
24
25
    private $simulated = false;
26
27
    private $verbose = false;
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * realtimeOutput returns the output stream that realtime output
36
     * should be sent to (if applicable)
37
     *
38
     * @return OutputStyle $output
39
     */
40
    public function realtimeOutput()
41
    {
42
        return $this->output;
43
    }
44
45
    /**
46
     * setRealtimeOutput allows the caller to inject an OutputStyle object
47
     * that will be used to stream realtime output if applicable.
48
     *
49
     * @param OutputStyle $output
50
     */
51
    public function setRealtimeOutput($output)
52
    {
53
        $this->output = $output;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function getVerbose()
60
    {
61
        return $this->verbose;
62
    }
63
64
    /**
65
     * @param bool $verbose
66
     */
67
    public function setVerbose($verbose)
68
    {
69
        $this->verbose = $verbose;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function getSimulated()
76
    {
77
        return $this->simulated;
78
    }
79
80
    /**
81
     * @param bool $simulated
82
     */
83
    public function setSimulated($simulated)
84
    {
85
        $this->simulated = $simulated;
86
    }
87
88
    /**
89
     * @return LoggerInterface
90
     */
91
    public function getLogger()
92
    {
93
        return $this->logger;
94
    }
95
96
    /**
97
     * @param LoggerInterface $logger
98
     */
99
    public function setLogger($logger)
100
    {
101
        $this->logger = $logger;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function start(callable $callback = null)
108
    {
109
        $cmd = $this->getCommandLine();
110
        if ($this->getSimulated()) {
111
            $this->getLogger()->notice('Simulating: ' . $cmd);
112
            // Run a command that always succeeds.
113
            $this->setCommandLine('exit 0');
114
        } elseif ($this->getVerbose()) {
115
            $this->getLogger()->info('Executing: ' . $cmd);
116
        }
117
        parent::start($callback);
118
        // Set command back to original value in case anyone asks.
119
        if ($this->getSimulated()) {
120
            $this->setCommandLine($cmd);
121
        }
122
    }
123
124
    /**
125
     * Return a realTime output object.
126
     *
127
     * @return callable
128
     */
129
    public function showRealtime()
130
    {
131
        $realTimeOutput = new RealtimeOutputHandler($this->realtimeOutput(), $this->realtimeOutput()->getErrorOutput());
0 ignored issues
show
Bug introduced by
The method getErrorOutput() cannot be called from this context as it is declared protected in class Symfony\Component\Console\Style\OutputStyle.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
132
        $realTimeOutput->configure($this);
133
        return [$realTimeOutput, 'handleOutput'];
134
    }
135
}
136