Passed
Branch master (ecaff5)
by Joao
02:24
created

_Lib::fixDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Builder;
4
5
use ByJG\Config\Exception\ConfigNotFoundException;
6
use ByJG\Config\Exception\EnvironmentException;
7
use ByJG\Config\Exception\KeyNotFoundException;
8
use Closure;
9
use Psr\SimpleCache\InvalidArgumentException;
10
use ReflectionException;
11
12
class _Lib
13
{
14
    protected $workdir;
15
    protected $systemOs;
16
17
    public function __construct()
18
    {
19
        $this->workdir = realpath(__DIR__ . '/../..');
20
    }
21
22
    public function getSystemOs()
23
    {
24
        if (!$this->systemOs) {
25
            $this->systemOs = php_uname('s');
26
            if (preg_match('/[Dd]arwin/', $this->systemOs)) {
27
                $this->systemOs = 'Darwin';
28
            } elseif (preg_match('/[Ww]in/', $this->systemOs)) {
29
                $this->systemOs = 'Windows';
30
            }
31
        }
32
33
        return $this->systemOs;
34
    }
35
36
    public function fixDir($command)
37
    {
38
        if ($this->getSystemOs() === "Windows") {
39
            return str_replace('/', '\\', $command);
40
        }
41
        return $command;
42
    }
43
44
    /**
45
     * Execute the given command by displaying console output live to the user.
46
     *
47
     * @param string|array $cmd :  command to be executed
48
     * @return array   exit_status  :  exit status of the executed command
49
     *                  output       :  console output of the executed command
50
     * @throws ConfigNotFoundException
51
     * @throws EnvironmentException
52
     * @throws InvalidArgumentException
53
     * @throws KeyNotFoundException
54
     * @throws ReflectionException
55
     */
56
    protected function liveExecuteCommand($cmd)
57
    {
58
        // while (@ ob_end_flush()); // end all output buffers if any
59
60
        if (is_array($cmd)) {
61
            foreach ($cmd as $item) {
62
                $this->liveExecuteCommand($item);
63
            }
64
            return null;
65
        }
66
67
        $cmd = $this->replaceVariables($cmd);
68
        echo "\n>> $cmd\n";
69
70
        $complement = " 2>&1 ; echo Exit status : $?";
71
        if ($this->getSystemOs() === "Windows") {
72
            $complement = ' & echo Exit status : %errorlevel%';
73
        }
74
        $proc = popen("$cmd $complement", 'r');
75
76
        $completeOutput = "";
77
78
        while (!feof($proc)) {
79
            $liveOutput     = fread($proc, 4096);
80
            $completeOutput = $completeOutput . $liveOutput;
81
            echo "$liveOutput";
82
            @ flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
83
        }
84
85
        pclose($proc);
86
87
        // get exit status
88
        preg_match('/[0-9]+$/', $completeOutput, $matches);
89
90
        $exitStatus = intval($matches[0]);
91
        // if ($exitStatus !== 0) {
92
        //     exit($exitStatus);
93
        // }
94
95
        // return exit status and intended output
96
        return array (
97
            'exit_status'  => $exitStatus,
98
            'output'       => str_replace("Exit status : " . $matches[0], '', $completeOutput)
99
        );
100
    }
101
102
    /**
103
     * @param string|Closure $variableValue
104
     * @return mixed
105
     * @throws ConfigNotFoundException
106
     * @throws EnvironmentException
107
     * @throws InvalidArgumentException
108
     * @throws KeyNotFoundException
109
     * @throws ReflectionException
110
     */
111
    protected function replaceVariables($variableValue)
112
    {
113
        $args = [];
114
        if (preg_match_all("/%[\\w\\d]+%/", $variableValue, $args)) {
115
            foreach ($args[0] as $arg) {
116
                $variableValue = str_replace(
117
                    $arg,
118
                    Psr11::container()->get(substr($arg,1, -1)),
119
                    $variableValue
120
                );
121
            }
122
        }
123
124
        return $variableValue;
125
    }
126
}
127