Skip to content

Commit

Permalink
Use error handler instead of error suppression
Browse files Browse the repository at this point in the history
Fixes #200.
  • Loading branch information
trowski committed Mar 24, 2024
1 parent 051211a commit 73d293f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/Context/Internal/process-runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

// Doesn't exist in phpdbg...
if (\function_exists("cli_set_process_title")) {
@\cli_set_process_title("amp-process");
\set_error_handler(static fn () => true);
try {
\cli_set_process_title("amp-process");
} finally {
\restore_error_handler();
}
}

(function (): void {
Expand Down
26 changes: 16 additions & 10 deletions src/Context/ProcessContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ public static function start(
self::$pharCopy = \sys_get_temp_dir() . "/phar-" . \bin2hex(\random_bytes(10)) . ".phar";
\copy(\Phar::running(false), self::$pharCopy);

\register_shutdown_function(static function (): void {
if (self::$pharCopy !== null) {
@\unlink(self::$pharCopy);
}
});
\register_shutdown_function(static fn () => self::unlinkExternalCopy(self::$pharCopy));

$path = "phar://" . self::$pharCopy . "/" . \substr($path, \strlen(\Phar::running(true)));
}
Expand All @@ -121,11 +117,7 @@ public static function start(
self::$pharScriptPath = $scriptPath = \sys_get_temp_dir() . "/amp-process-runner-" . $suffix . ".php";
\file_put_contents($scriptPath, $contents);

\register_shutdown_function(static function (): void {
if (self::$pharScriptPath !== null) {
@\unlink(self::$pharScriptPath);
}
});
\register_shutdown_function(static fn () => self::unlinkExternalCopy(self::$pharScriptPath));
}

// Monkey-patch the script path in the same way, only supported if the command is given as array.
Expand Down Expand Up @@ -176,6 +168,20 @@ public static function start(
return new self($process, $ipcChannel, $resultChannel);
}

private static function unlinkExternalCopy(?string $filepath): void
{
if ($filepath === null) {
return;
}

\set_error_handler(static fn () => true);
try {
\unlink($filepath);
} finally {
\restore_error_handler();
}
}

/**
* @return non-empty-list<string>
*/
Expand Down

0 comments on commit 73d293f

Please sign in to comment.