Completed
Push — master ( 65a442...e3692b )
by Sebastian
08:39
created

ProcOpen::run()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Cli\Processor;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command\Result;
14
use SebastianFeldmann\Cli\Processor;
15
16
/**
17
 * Class ProcOpen
18
 *
19
 * @package SebastianFeldmann\Cli
20
 */
21
class ProcOpen implements Processor
22
{
23
    /**
24
     * Execute the command.
25
     *
26
     * @param  string $cmd
27
     * @return \SebastianFeldmann\Cli\Command\Result
28
     */
29
    public function run(string $cmd) : Result
30
    {
31
        $old            = error_reporting(0);
32
        $descriptorSpec = [
33
            ['pipe', 'r'],
34
            ['pipe', 'w'],
35
            ['pipe', 'w'],
36
        ];
37
38
        $process = proc_open($cmd, $descriptorSpec, $pipes);
39
        if (!is_resource($process)) {
40
            throw new RuntimeException('can\'t execute \'proc_open\'');
41
        }
42
43
        $stdOut = stream_get_contents($pipes[1]);
44
        fclose($pipes[1]);
45
46
        $stdErr = stream_get_contents($pipes[2]);
47
        fclose($pipes[2]);
48
49
        $code = proc_close($process);
50
        error_reporting($old);
51
52
        return new Result($cmd, $code, $stdOut, $stdErr);
53
    }
54
}
55