Completed
Push — master ( b76e56...aeab41 )
by Moshe
268:47 queued 216:30
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 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->io()->section('Start: ' . $cmd);
110
            $this->getLogger()->info('Executing: ' . $cmd);
111
            $this->io()->section('End: ' . $cmd);
112
        }
113
        parent::start($callback);
114
        // Set command back to original value in case anyone asks.
115
        if ($this->getSimulated()) {
116
            $this->setCommandLine($cmd);
117
        }
118
    }
119
120
    /**
121
     * Helper method when you want real-time output from a Process call. See
122
     * @param $type
123
     * @param $buffer
124
     */
125
    public static function realTime($type, $buffer)
126
    {
127
        if (Process::ERR === $type) {
128
            echo $buffer;
129
        } else {
130
            echo $buffer;
131
        }
132
    }
133
134
    /**
135
     * Should return a closure. For now return a callable.
136
     *
137
     * @return callable
138
     */
139
    public static function showRealtime() {
140
        return '\Consolidation\SiteProcess\ProcessBase::realTime';
141
    }
142
}
143