Skip to content

Commit da6f9e1

Browse files
committed
feat: use no timeout + add error handler
1 parent ec60cd4 commit da6f9e1

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/Sprout/Process.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,29 @@
1212
class Process
1313
{
1414
protected $process;
15+
protected $errorHandler;
1516

1617
public function __construct(string $command)
1718
{
1819
if (!\function_exists('proc_open')) {
1920
throw new \Exception('The Process class relies on proc_open, which is not available on your PHP installation.');
2021
}
2122

22-
$this->process = \Symfony\Component\Process\Process::fromShellCommandline($command);
23+
$this->process = \Symfony\Component\Process\Process::fromShellCommandline(
24+
$command,
25+
null,
26+
null,
27+
null,
28+
null
29+
);
2330
}
2431

2532
/**
2633
* Set an error action for current process
2734
*/
2835
public function onError(callable $errorHandler): Process
2936
{
37+
$this->errorHandler = $errorHandler;
3038
return $this;
3139
}
3240

@@ -37,13 +45,30 @@ public function onError(callable $errorHandler): Process
3745
*/
3846
public function run(?callable $callback = null): int
3947
{
40-
return $this->process->run(function ($type, $buffer) use ($callback) {
41-
if ($callback) {
42-
$callback($type, $buffer);
43-
} else {
44-
echo $buffer;
48+
try {
49+
return $this->process->run(function ($type, $buffer) use ($callback) {
50+
if ($callback) {
51+
$callback($type, $buffer);
52+
} else {
53+
echo $buffer;
54+
}
55+
});
56+
} catch (\Throwable $th) {
57+
if ($this->errorHandler) {
58+
\call_user_func($this->errorHandler, $th->getMessage(), $this->process);
4559
}
46-
});
60+
61+
throw $th;
62+
}
63+
}
64+
65+
/**
66+
* Check if the process is successful
67+
* @return bool
68+
*/
69+
public function isSuccessful(): bool
70+
{
71+
return $this->process->isSuccessful();
4772
}
4873

4974
/**

0 commit comments

Comments
 (0)