Skip to content

Commit

Permalink
fix: revert workdir after run end
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 12, 2021
1 parent 6ff6fda commit 594725f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function chdir;
use function exec;
use function function_exists;
use function getcwd;
use function implode;
use function ob_get_clean;
use function ob_start;
Expand Down Expand Up @@ -57,7 +58,9 @@ public static function exec(string $command, string $workDir = '', bool $outAsSt
*/
public static function system(string $command, string $workDir = '', bool $allReturn = false): array
{
$curDir = '';
if ($workDir) {
$curDir = getcwd();
chdir($workDir);
}

Expand All @@ -70,6 +73,11 @@ public static function system(string $command, string $workDir = '', bool $allRe
$output = system($command, $status);
}

// fix: revert workdir after run end.
if ($curDir) {
chdir($curDir);
}

return [$status, $output];
}

Expand All @@ -81,11 +89,19 @@ public static function system(string $command, string $workDir = '', bool $allRe
*/
public static function shellExec(string $command, string $workDir = ''): ?string
{
$curDir = '';
if ($workDir) {
$curDir = getcwd();
chdir($workDir);
}

return shell_exec($command);
$ret = shell_exec($command);
// fix: revert workdir after run end.
if ($curDir) {
chdir($curDir);
}

return $ret;
}

/**
Expand Down Expand Up @@ -143,8 +159,10 @@ public static function run(string $command, string $cwd = ''): array
public static function auto(string $command, bool $returnStatus = true, string $cwd = '')
{
$status = 1;
$curDir = '';

if ($cwd) {
$curDir = getcwd();
chdir($cwd);
}

Expand All @@ -166,6 +184,11 @@ public static function auto(string $command, bool $returnStatus = true, string $
$output = 'Command execution not possible on this system';
}

// fix: revert workdir after run end.
if ($curDir) {
chdir($curDir);
}

if ($returnStatus) {
return [
'output' => trim($output),
Expand Down
12 changes: 12 additions & 0 deletions src/Proc/ProcWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use InvalidArgumentException;
use RuntimeException;
use function array_keys;
use function chdir;
use function fclose;
use function getcwd;
use function proc_open;
use function stream_get_contents;
use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -226,9 +228,14 @@ public function open(): self
throw new InvalidArgumentException('The want execute command is cannot be empty');
}

$curDir = '';
$workDir = $this->workDir ?: null;
$options = $this->options;

if ($workDir) {
$curDir = getcwd();
}

$options['suppress_errors'] = true;
if ('\\' === DIRECTORY_SEPARATOR) { // windows
$options['bypass_shell'] = true;
Expand All @@ -248,6 +255,11 @@ public function open(): self
throw new RuntimeException("Can't open resource with proc_open.");
}

// fix: revert workdir after run end.
if ($curDir) {
chdir($curDir);
}

$this->process = $process;
return $this;
}
Expand Down

0 comments on commit 594725f

Please sign in to comment.