Skip to content

Commit 594725f

Browse files
committed
fix: revert workdir after run end
1 parent 6ff6fda commit 594725f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Exec.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function chdir;
1515
use function exec;
1616
use function function_exists;
17+
use function getcwd;
1718
use function implode;
1819
use function ob_get_clean;
1920
use 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),

src/Proc/ProcWrapper.php

+12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use InvalidArgumentException;
1313
use RuntimeException;
1414
use function array_keys;
15+
use function chdir;
1516
use function fclose;
17+
use function getcwd;
1618
use function proc_open;
1719
use function stream_get_contents;
1820
use const DIRECTORY_SEPARATOR;
@@ -226,9 +228,14 @@ public function open(): self
226228
throw new InvalidArgumentException('The want execute command is cannot be empty');
227229
}
228230

231+
$curDir = '';
229232
$workDir = $this->workDir ?: null;
230233
$options = $this->options;
231234

235+
if ($workDir) {
236+
$curDir = getcwd();
237+
}
238+
232239
$options['suppress_errors'] = true;
233240
if ('\\' === DIRECTORY_SEPARATOR) { // windows
234241
$options['bypass_shell'] = true;
@@ -248,6 +255,11 @@ public function open(): self
248255
throw new RuntimeException("Can't open resource with proc_open.");
249256
}
250257

258+
// fix: revert workdir after run end.
259+
if ($curDir) {
260+
chdir($curDir);
261+
}
262+
251263
$this->process = $process;
252264
return $this;
253265
}

0 commit comments

Comments
 (0)