|
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
|
|
|
|
|
11
|
|
|
class _Lib |
|
12
|
|
|
{ |
|
13
|
|
|
protected $container; |
|
14
|
|
|
protected $image; |
|
15
|
|
|
protected $workdir; |
|
16
|
|
|
protected $systemOs; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->workdir = realpath(__DIR__ . '/../..'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getSystemOs() |
|
24
|
|
|
{ |
|
25
|
|
|
if (!$this->systemOs) { |
|
26
|
|
|
$this->systemOs = php_uname('s'); |
|
27
|
|
|
if (preg_match('/[Dd]arwin/', $this->systemOs)) { |
|
28
|
|
|
$this->systemOs = 'Darwin'; |
|
29
|
|
|
} elseif (preg_match('/[Ww]in/', $this->systemOs)) { |
|
30
|
|
|
$this->systemOs = 'Windows'; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->systemOs; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function fixDir($command) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->getSystemOs() === "Windows") { |
|
40
|
|
|
return str_replace('/', '\\', $command); |
|
41
|
|
|
} |
|
42
|
|
|
return $command; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Execute the given command by displaying console output live to the user. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|array $cmd : command to be executed |
|
49
|
|
|
* @return array exit_status : exit status of the executed command |
|
50
|
|
|
* output : console output of the executed command |
|
51
|
|
|
* @throws ConfigNotFoundException |
|
52
|
|
|
* @throws EnvironmentException |
|
53
|
|
|
* @throws KeyNotFoundException |
|
54
|
|
|
* @throws InvalidArgumentException |
|
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(); |
|
|
|
|
|
|
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 KeyNotFoundException |
|
108
|
|
|
* @throws InvalidArgumentException |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function replaceVariables($variableValue) |
|
111
|
|
|
{ |
|
112
|
|
|
$args = []; |
|
113
|
|
|
if (preg_match_all("/%[\\w\\d]+%/", $variableValue, $args)) { |
|
114
|
|
|
foreach ($args[0] as $arg) { |
|
115
|
|
|
$variableValue = str_replace( |
|
116
|
|
|
$arg, |
|
117
|
|
|
Psr11::container()->get(substr($arg,1, -1)), |
|
118
|
|
|
$variableValue |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
}; |
|
122
|
|
|
|
|
123
|
|
|
return $variableValue; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|