1414use function chdir ;
1515use function exec ;
1616use function function_exists ;
17+ use function getcwd ;
1718use function implode ;
1819use function ob_get_clean ;
1920use function ob_start ;
@@ -57,7 +58,9 @@ public static function exec(string $command, string $workDir = '', bool $outAsSt
5758 */
5859 public static function system (string $ command , string $ workDir = '' , bool $ allReturn = false ): array
5960 {
61+ $ curDir = '' ;
6062 if ($ workDir ) {
63+ $ curDir = getcwd ();
6164 chdir ($ workDir );
6265 }
6366
@@ -70,6 +73,11 @@ public static function system(string $command, string $workDir = '', bool $allRe
7073 $ output = system ($ command , $ status );
7174 }
7275
76+ // fix: revert workdir after run end.
77+ if ($ curDir ) {
78+ chdir ($ curDir );
79+ }
80+
7381 return [$ status , $ output ];
7482 }
7583
@@ -81,11 +89,19 @@ public static function system(string $command, string $workDir = '', bool $allRe
8189 */
8290 public static function shellExec (string $ command , string $ workDir = '' ): ?string
8391 {
92+ $ curDir = '' ;
8493 if ($ workDir ) {
94+ $ curDir = getcwd ();
8595 chdir ($ workDir );
8696 }
8797
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 ;
89105 }
90106
91107 /**
@@ -143,8 +159,10 @@ public static function run(string $command, string $cwd = ''): array
143159 public static function auto (string $ command , bool $ returnStatus = true , string $ cwd = '' )
144160 {
145161 $ status = 1 ;
162+ $ curDir = '' ;
146163
147164 if ($ cwd ) {
165+ $ curDir = getcwd ();
148166 chdir ($ cwd );
149167 }
150168
@@ -166,6 +184,11 @@ public static function auto(string $command, bool $returnStatus = true, string $
166184 $ output = 'Command execution not possible on this system ' ;
167185 }
168186
187+ // fix: revert workdir after run end.
188+ if ($ curDir ) {
189+ chdir ($ curDir );
190+ }
191+
169192 if ($ returnStatus ) {
170193 return [
171194 'output ' => trim ($ output ),
0 commit comments