14
14
use function chdir ;
15
15
use function exec ;
16
16
use function function_exists ;
17
+ use function getcwd ;
17
18
use function implode ;
18
19
use function ob_get_clean ;
19
20
use function ob_start ;
@@ -57,7 +58,9 @@ public static function exec(string $command, string $workDir = '', bool $outAsSt
57
58
*/
58
59
public static function system (string $ command , string $ workDir = '' , bool $ allReturn = false ): array
59
60
{
61
+ $ curDir = '' ;
60
62
if ($ workDir ) {
63
+ $ curDir = getcwd ();
61
64
chdir ($ workDir );
62
65
}
63
66
@@ -70,6 +73,11 @@ public static function system(string $command, string $workDir = '', bool $allRe
70
73
$ output = system ($ command , $ status );
71
74
}
72
75
76
+ // fix: revert workdir after run end.
77
+ if ($ curDir ) {
78
+ chdir ($ curDir );
79
+ }
80
+
73
81
return [$ status , $ output ];
74
82
}
75
83
@@ -81,11 +89,19 @@ public static function system(string $command, string $workDir = '', bool $allRe
81
89
*/
82
90
public static function shellExec (string $ command , string $ workDir = '' ): ?string
83
91
{
92
+ $ curDir = '' ;
84
93
if ($ workDir ) {
94
+ $ curDir = getcwd ();
85
95
chdir ($ workDir );
86
96
}
87
97
88
- return shell_exec ($ command );
98
+ $ ret = shell_exec ($ command );
99
+ // fix: revert workdir after run end.
100
+ if ($ curDir ) {
101
+ chdir ($ curDir );
102
+ }
103
+
104
+ return $ ret ;
89
105
}
90
106
91
107
/**
@@ -143,8 +159,10 @@ public static function run(string $command, string $cwd = ''): array
143
159
public static function auto (string $ command , bool $ returnStatus = true , string $ cwd = '' )
144
160
{
145
161
$ status = 1 ;
162
+ $ curDir = '' ;
146
163
147
164
if ($ cwd ) {
165
+ $ curDir = getcwd ();
148
166
chdir ($ cwd );
149
167
}
150
168
@@ -166,6 +184,11 @@ public static function auto(string $command, bool $returnStatus = true, string $
166
184
$ output = 'Command execution not possible on this system ' ;
167
185
}
168
186
187
+ // fix: revert workdir after run end.
188
+ if ($ curDir ) {
189
+ chdir ($ curDir );
190
+ }
191
+
169
192
if ($ returnStatus ) {
170
193
return [
171
194
'output ' => trim ($ output ),
0 commit comments