Completed
Push — master ( e081f2...158b72 )
by Greg
03:09
created

Exec::setInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 285.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Robo\Task\Base;
3
4
use Robo\Contract\CommandInterface;
5
use Robo\Contract\PrintedInterface;
6
use Robo\Contract\SimulatedInterface;
7
use Robo\Task\BaseTask;
8
use Symfony\Component\Process\Process;
9
use Robo\Result;
10
11
/**
12
 * Executes shell script. Closes it when running in background mode.
13
 *
14
 * ``` php
15
 * <?php
16
 * $this->taskExec('compass')->arg('watch')->run();
17
 * // or use shortcut
18
 * $this->_exec('compass watch');
19
 *
20
 * $this->taskExec('compass watch')->background()->run();
21
 *
22
 * if ($this->taskExec('phpunit .')->run()->wasSuccessful()) {
23
 *  $this->say('tests passed');
24
 * }
25
 *
26
 * ?>
27
 * ```
28
 */
29
class Exec extends BaseTask implements CommandInterface, PrintedInterface, SimulatedInterface
30
{
31
    use \Robo\Common\CommandReceiver;
32
    use \Robo\Common\ExecOneCommand;
33
34
    /**
35
     * @var static[]
36
     */
37
    protected static $instances = [];
38
39
    /**
40
     * @var string|\Robo\Contract\CommandInterface
41
     */
42
    protected $command;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $background = false;
48
49
    /**
50
     * @var null|int
51
     */
52
    protected $timeout = null;
53
54
    /**
55
     * @var null|int
56
     */
57
    protected $idleTimeout = null;
58
59
    /**
60
     * @var null|array
61
     */
62
    protected $env = null;
63
64
    /**
65
     * @var Process
66
     */
67
    protected $process;
68
69
    /**
70
     * @var resource|string
71
     */
72
    protected $input;
73
74
    /**
75
     * @var boolean
76
     */
77
    protected $interactive;
78
79
    /**
80
     * @param string|\Robo\Contract\CommandInterface $command
81
     */
82
    public function __construct($command)
83
    {
84
        $this->command = $this->receiveCommand($command);
85
        if (!isset($this->interactive) && function_exists('posix_isatty')) {
86
            $this->interactive = posix_isatty(STDOUT);
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getCommand()
94
    {
95
        return trim($this->command . $this->arguments);
96
    }
97
98
    /**
99
     * Executes command in background mode (asynchronously)
100
     *
101
     * @return $this
102
     */
103
    public function background()
104
    {
105
        self::$instances[] = $this;
106
        $this->background = true;
107
        return $this;
108
    }
109
110
    /**
111
     * Stop command if it runs longer then $timeout in seconds
112
     *
113
     * @param int $timeout
114
     *
115
     * @return $this
116
     */
117
    public function timeout($timeout)
118
    {
119
        $this->timeout = $timeout;
120
        return $this;
121
    }
122
123
    /**
124
     * Stops command if it does not output something for a while
125
     *
126
     * @param int $timeout
127
     *
128
     * @return $this
129
     */
130
    public function idleTimeout($timeout)
131
    {
132
        $this->idleTimeout = $timeout;
133
        return $this;
134
    }
135
136
    /**
137
     * Sets the environment variables for the command
138
     *
139
     * @param array $env
140
     *
141
     * @return $this
142
     */
143
    public function env(array $env)
144
    {
145
        $this->env = $env;
146
        return $this;
147
    }
148
149
    /**
150
     * Pass an input to the process. Can be resource created with fopen() or string
151
     *
152
     * @param resource|string $input
153
     *
154
     * @return $this
155
     */
156
    public function setInput($input)
157
    {
158
        $this->input = $input;
159
        return $this;
160
    }
161
162
    /**
163
     * Attach tty to process for interactive input
164
     *
165
     * @param $interactive bool
166
     *
167
     * @return $this
168
     */
169
    public function interactive($interactive)
170
    {
171
        $this->interactive = $interactive;
172
        return $this;
173
    }
174
175
    public function __destruct()
176
    {
177
        $this->stop();
178
    }
179
180
    protected function stop()
181
    {
182
        if ($this->background && $this->process->isRunning()) {
183
            $this->process->stop();
184
            $this->printTaskInfo("Stopped {command}", ['command' => $this->getCommand()]);
185
        }
186
    }
187
188
    /**
189
     * @param array $context
190
     */
191
    protected function printAction($context = [])
192
    {
193
        $command = $this->getCommand();
194
        $dir = $this->workingDirectory ? " in {dir}" : "";
195
        $this->printTaskInfo("Running {command}$dir", ['command' => $command, 'dir' => $this->workingDirectory] + $context);
196
    }
197
198
    /**
199
     * Gets the data array to be passed to Result().
200
     *
201
     * @return array
202
     *   The data array passed to Result().
203
     */
204
    protected function getResultData()
205
    {
206
        if ($this->isMetadataPrinted) {
207
            return ['time' => $this->getExecutionTime()];
208
        }
209
210
        return [];
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function run()
217
    {
218
        if ($this->isMetadataPrinted) {
219
            $this->printAction();
220
        }
221
        $this->process = new Process($this->getCommand());
222
        $this->process->setTimeout($this->timeout);
223
        $this->process->setIdleTimeout($this->idleTimeout);
224
        $this->process->setWorkingDirectory($this->workingDirectory);
225
226
        if ($this->input) {
227
            $this->process->setInput($this->input);
228
        }
229
230
        if ($this->interactive) {
231
            $this->process->setTty(true);
232
        }
233
234
        if (isset($this->env)) {
235
            $this->process->setEnv($this->env);
236
        }
237
238
        if (!$this->background and !$this->isPrinted) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
239
            $this->startTimer();
240
            $this->process->run();
241
            $this->stopTimer();
242
            return new Result($this, $this->process->getExitCode(), $this->process->getOutput(), $this->getResultData());
243
        }
244
245
        if (!$this->background and $this->isPrinted) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
246
            $this->startTimer();
247
            $this->process->run(
248
                function ($type, $buffer) {
249
                    $progressWasVisible = $this->hideTaskProgress();
250
                    print($buffer);
251
                    $this->showTaskProgress($progressWasVisible);
252
                }
253
            );
254
            $this->stopTimer();
255
            return new Result($this, $this->process->getExitCode(), $this->process->getOutput(), $this->getResultData());
256
        }
257
258
        try {
259
            $this->process->start();
260
        } catch (\Exception $e) {
261
            return Result::fromException($this, $e);
262
        }
263
        return Result::success($this);
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function simulate($context)
270
    {
271
        $this->printAction($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by parameter $context on line 269 can also be of type null; however, Robo\Task\Base\Exec::printAction() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
272
    }
273
274
    public static function stopRunningJobs()
275
    {
276
        foreach (self::$instances as $instance) {
277
            if ($instance) {
278
                unset($instance);
279
            }
280
        }
281
    }
282
}
283
284
if (function_exists('pcntl_signal')) {
285
    pcntl_signal(SIGTERM, ['Robo\Task\Base\Exec', 'stopRunningJobs']);
286
}
287
288
register_shutdown_function(['Robo\Task\Base\Exec', 'stopRunningJobs']);
289