Completed
Push — master ( fb685a...a194ad )
by Moshe
399:40 queued 375:30
created

ProcessBase::start()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 6
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 Robo\Contract\IOAwareInterface;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
use Symfony\Component\Process\Process;
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 SymfonyStyle
22
     */
23
    protected $io;
24
25
    private $simulated = false;
26
27
    private $verbose = false;
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * @return SymfonyStyle $io
36
     */
37
    public function io()
38
    {
39
        return $this->io;
40
    }
41
42
    /**
43
     * @param $io
44
     */
45
    public function setIo($io)
46
    {
47
        $this->io = $io;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function getVerbose()
54
    {
55
        return $this->verbose;
56
    }
57
58
    /**
59
     * @param bool $verbose
60
     */
61
    public function setVerbose($verbose)
62
    {
63
        $this->verbose = $verbose;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function getSimulated()
70
    {
71
        return $this->simulated;
72
    }
73
74
    /**
75
     * @param bool $simulated
76
     */
77
    public function setSimulated($simulated)
78
    {
79
        $this->simulated = $simulated;
80
    }
81
82
    /**
83
     * @return LoggerInterface
84
     */
85
    public function getLogger()
86
    {
87
        return $this->logger;
88
    }
89
90
    /**
91
     * @param LoggerInterface $logger
92
     */
93
    public function setLogger($logger)
94
    {
95
        $this->logger = $logger;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function start(callable $callback = null)
102
    {
103
        $cmd = $this->getCommandLine();
104
        if ($this->getSimulated()) {
105
            $this->getLogger()->notice('Simulating: ' . $cmd);
106
            // Run a command that always succeeds.
107
            $this->setCommandLine('exit 0');
108
        } elseif ($this->getVerbose()) {
109
            $this->getLogger()->info('Executing: ' . $cmd);
110
        }
111
        parent::start($callback);
112
        // Set command back to original value in case anyone asks.
113
        if ($this->getSimulated()) {
114
            $this->setCommandLine($cmd);
115
        }
116
    }
117
118
    /**
119
     * Helper method when you want real-time output from a Process call. See
120
     * @param $type
121
     * @param $buffer
122
     */
123
    public static function realTime($type, $buffer)
124
    {
125
        if (Process::ERR === $type) {
126
            echo $buffer;
127
        } else {
128
            echo $buffer;
129
        }
130
    }
131
132
    /**
133
     * Should return a closure. For now return a callable.
134
     *
135
     * @return callable
136
     */
137
    public static function showRealtime() {
138
        return '\Consolidation\SiteProcess\ProcessBase::realTime';
139
    }
140
}
141