Completed
Push — master ( 934fa6...2d00e0 )
by Moshe
361:22 queued 312:43
created

ProcessBase::showRealtime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 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 string
31
     */
32
    private $alias;
33
34
    /**
35
     * @return string
36
     */
37
    public function getAlias()
38
    {
39
        return $this->alias;
40
    }
41
42
    /**
43
     * @param string $aliasName
44
     */
45
    public function setAlias($aliasName)
46
    {
47
        $this->alias = $aliasName;
48
    }
49
50
    /**
51
     * @var LoggerInterface
52
     */
53
    private $logger;
54
55
    /**
56
     * @return SymfonyStyle $io
57
     */
58
    public function io()
59
    {
60
        return $this->io;
61
    }
62
63
    /**
64
     * @param $io
65
     */
66
    public function setIo($io)
67
    {
68
        $this->io = $io;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function getVerbose()
75
    {
76
        return $this->verbose;
77
    }
78
79
    /**
80
     * @param bool $verbose
81
     */
82
    public function setVerbose($verbose)
83
    {
84
        $this->verbose = $verbose;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function getSimulated()
91
    {
92
        return $this->simulated;
93
    }
94
95
    /**
96
     * @param bool $simulated
97
     */
98
    public function setSimulated($simulated)
99
    {
100
        $this->simulated = $simulated;
101
    }
102
103
    /**
104
     * @return LoggerInterface
105
     */
106
    public function getLogger()
107
    {
108
        return $this->logger;
109
    }
110
111
    /**
112
     * @param LoggerInterface $logger
113
     */
114
    public function setLogger($logger)
115
    {
116
        $this->logger = $logger;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function start(callable $callback = null)
123
    {
124
        $cmd = $this->getCommandLine();
125
        if ($this->getSimulated()) {
126
            $this->getLogger()->notice('Simulating: ' . $cmd);
127
            // Run a command that always succeeds.
128
            $this->setCommandLine('exit 0');
129
        } elseif ($this->getVerbose()) {
130
            if ($this->getAlias()) {
131
                $this->io()->section('Start: ' . $cmd);
132
            }
133
            $this->getLogger()->info('Executing: ' . $cmd);
134
        }
135
        parent::start($callback);
136
        // Set command back to original value in case anyone asks.
137
        if ($this->getSimulated()) {
138
            $this->setCommandLine($cmd);
139
        }
140
        if ($this->getAlias() && $this->getVerbose()) {
141
            $this->io()->section('End: ' . $cmd);
142
        }
143
    }
144
145
    /**
146
     * Helper method when you want real-time output from a Process call. See
147
     * @param $type
148
     * @param $buffer
149
     */
150
    public static function realTime($type, $buffer)
151
    {
152
        if (Process::ERR === $type) {
153
            echo $buffer;
154
        } else {
155
            echo $buffer;
156
        }
157
    }
158
159
    /**
160
     * Should return a closure. For now return a callable.
161
     *
162
     * @return callable
163
     */
164
    public static function showRealtime() {
165
        return '\Consolidation\SiteProcess\ProcessBase::realTime';
166
    }
167
}
168