Completed
Pull Request — master (#2)
by Joao
04:25
created

_Lib::getDockerVariables()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Builder;
4
5
class _Lib
6
{
7
    protected $container;
8
    protected $image;
9
    protected $workdir;
10
    protected $os;
11
12
    public function __construct()
13
    {
14
        $this->workdir = realpath(__DIR__ . '/../..');
15
    }
16
17
    public function getOs()
18
    {
19
        if (!$this->os) {
20
            $this->os = php_uname('s');
21
            if (preg_match('/[Ww]in/', $this->os)) {
22
                $this->os = 'Windows';
23
            }
24
        }
25
26
        return $this->os;
27
    }
28
29
    public function fixDir($command)
30
    {
31
        if ($this->getOs() === "Windows") {
32
            return str_replace('/', '\\', $command);
33
        }
34
        return $command;
35
    }
36
37
    /**
38
     * Execute the given command by displaying console output live to the user.
39
     *  @param  string|array  $cmd          :  command to be executed
40
     *  @return array   exit_status  :  exit status of the executed command
41
     *                  output       :  console output of the executed command
42
     */
43
    protected function liveExecuteCommand($cmd)
44
    {
45
        // while (@ ob_end_flush()); // end all output buffers if any
46
47
        if (is_array($cmd)) {
48
            foreach ($cmd as $item) {
49
                $this->liveExecuteCommand($item);
50
            }
51
            return null;
52
        }
53
54
        $cmd = $this->replaceVariables($cmd);
55
        echo "\n>> $cmd\n";
56
57
        $complement = " 2>&1 ; echo Exit status : $?";
58
        if ($this->getOs() === "Windows") {
59
            $complement = ' & echo Exit status : %errorlevel%';
60
        }
61
        $proc = popen("$cmd $complement", 'r');
62
63
        $completeOutput = "";
64
65
        while (!feof($proc)) {
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        while (!feof(/** @scrutinizer ignore-type */ $proc)) {
Loading history...
66
            $liveOutput     = fread($proc, 4096);
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $liveOutput     = fread(/** @scrutinizer ignore-type */ $proc, 4096);
Loading history...
67
            $completeOutput = $completeOutput . $liveOutput;
68
            echo "$liveOutput";
69
            @ flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

69
            /** @scrutinizer ignore-unhandled */ @ flush();

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...
Bug introduced by
Are you sure the usage of flush() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
        }
71
72
        pclose($proc);
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of pclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        pclose(/** @scrutinizer ignore-type */ $proc);
Loading history...
73
74
        // get exit status
75
        preg_match('/[0-9]+$/', $completeOutput, $matches);
76
77
        $exitStatus = intval($matches[0]);
78
        // if ($exitStatus !== 0) {
79
        //     exit($exitStatus);
80
        // }
81
82
        // return exit status and intended output
83
        return array (
84
            'exit_status'  => $exitStatus,
85
            'output'       => str_replace("Exit status : " . $matches[0], '', $completeOutput)
86
        );
87
    }
88
89
    protected $dockerVariables = null;
90
    protected function getDockerVariables()
91
    {
92
        // Builder Variables
93
        if ($this->dockerVariables === null) {
94
            $this->dockerVariables = [
95
                '%env%'     => Psr11::environment()->getCurrentEnv(),
96
                '%workdir%' => $this->workdir
97
            ];
98
99
            // Get User Variables
100
            $variables = Psr11::container()->get('BUILDER_VARIABLES');
101
            foreach ((array)$variables as $variable => $value) {
102
                $this->dockerVariables["%$variable%"] = $this->replaceVariables($value);
103
            }
104
        }
105
106
        return $this->dockerVariables;
107
    }
108
109
    /**
110
     * @param string|\Closure $variableValue
111
     * @return mixed
112
     */
113
    protected function replaceVariables($variableValue)
114
    {
115
        // Builder Variables
116
        $args = $this->getDockerVariables();
117
118
        if ($variableValue instanceof \Closure) {
119
            $string = $variableValue($args);
120
        } else {
121
            $string = $variableValue;
122
        }
123
124
        // Replace variables into string
125
        foreach ($args as $arg => $value) {
126
            $string = str_replace($arg, $value, $string);
127
        }
128
129
        return $string;
130
    }
131
}
132