Completed
Push — master ( af177e...94fe6d )
by Moshe
02:35
created

ProcessBase::setIo()   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 Robo\Contract\IOAwareInterface;
9
use Symfony\Component\Process\Process;
10
11
/**
12
 * A wrapper around Symfony Process.
13
 *
14
 * - Supports simulated mode. Typically enabled via a --simulate option.
15
 * - Supports verbose mode - logs all runs.
16
 */
17
class ProcessBase extends Process
18
{
19
    protected $io;
20
21
    private $simulated = false;
22
23
    private $verbose = false;
24
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30
    /**
31
     * @param $io
32
     */
33
    public function setIo($io)
34
    {
35
        $this->io = $io;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function getVerbose()
42
    {
43
        return $this->verbose;
44
    }
45
46
    /**
47
     * @param bool $verbose
48
     */
49
    public function setVerbose($verbose)
50
    {
51
        $this->verbose = $verbose;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function getSimulated()
58
    {
59
        return $this->simulated;
60
    }
61
62
    /**
63
     * @param bool $simulated
64
     */
65
    public function setSimulated($simulated)
66
    {
67
        $this->simulated = $simulated;
68
    }
69
70
    /**
71
     * @return LoggerInterface
72
     */
73
    public function getLogger()
74
    {
75
        return $this->logger;
76
    }
77
78
    /**
79
     * @param LoggerInterface $logger
80
     */
81
    public function setLogger($logger)
82
    {
83
        $this->logger = $logger;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function start(callable $callback = null)
90
    {
91
        $cmd = $this->getCommandLine();
92
        if ($this->getSimulated()) {
93
            $this->getLogger()->notice('Simulating: ' . $cmd);
94
            // Run a command that always succeeds.
95
            $this->setCommandLine('exit 0');
96
        } elseif ($this->getVerbose()) {
97
            $this->io()->section('Start: ' . $cmd);
0 ignored issues
show
Bug introduced by
The method io() does not seem to exist on object<Consolidation\SiteProcess\ProcessBase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
            $this->getLogger()->info('Executing: ' . $cmd);
99
            $this->io()->section('End: ' . $cmd);
0 ignored issues
show
Bug introduced by
The method io() does not seem to exist on object<Consolidation\SiteProcess\ProcessBase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        }
101
        parent::start($callback);
102
        // Set command back to original value in case anyone asks.
103
        if ($this->getSimulated()) {
104
            $this->setCommandLine($cmd);
105
        }
106
    }
107
108
    /**
109
     * Helper method when you want real-time output from a Process call. See
110
     * @param $type
111
     * @param $buffer
112
     */
113
    public static function realTime($type, $buffer)
114
    {
115
        if (Process::ERR === $type) {
116
            echo $buffer;
117
        } else {
118
            echo $buffer;
119
        }
120
    }
121
}
122