|
2 | 2 |
|
3 | 3 | namespace Tempest\Process; |
4 | 4 |
|
5 | | -use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException; |
6 | | -use Symfony\Component\Process\Process as SymfonyProcess; |
7 | 5 | use Tempest\DateTime\Duration; |
8 | | -use Tempest\Process\Exceptions\ProcessExecutionHasTimedOut; |
9 | 6 |
|
10 | | -/** |
11 | | - * Represents a process that has been invoked and is currently running or has completed. |
12 | | - */ |
13 | | -final class InvokedProcess implements InvokedProcessInterface |
| 7 | +interface InvokedProcess |
14 | 8 | { |
15 | 9 | /** |
16 | 10 | * Gets the process identifier. |
17 | 11 | */ |
18 | 12 | public ?int $pid { |
19 | | - get => $this->process->getPid(); |
| 13 | + get; |
20 | 14 | } |
21 | 15 |
|
22 | 16 | /** |
23 | 17 | * Whether the process is running. |
24 | 18 | */ |
25 | 19 | public bool $running { |
26 | | - get => $this->process->isRunning(); |
| 20 | + get; |
27 | 21 | } |
28 | 22 |
|
29 | 23 | /** |
30 | 24 | * Gets the output of the process. |
31 | 25 | */ |
32 | 26 | public string $output { |
33 | | - get => $this->process->getOutput(); |
| 27 | + get; |
34 | 28 | } |
35 | 29 |
|
36 | 30 | /** |
37 | 31 | * Gets the error output of the process. |
38 | 32 | */ |
39 | 33 | public string $errorOutput { |
40 | | - get => $this->process->getErrorOutput(); |
| 34 | + get; |
41 | 35 | } |
42 | 36 |
|
43 | | - public function __construct( |
44 | | - private readonly SymfonyProcess $process, |
45 | | - ) {} |
46 | | - |
47 | | - public function signal(int $signal): self |
48 | | - { |
49 | | - $this->process->signal($signal); |
50 | | - |
51 | | - return $this; |
52 | | - } |
53 | | - |
54 | | - public function stop(float|int|Duration $timeout = 10, ?int $signal = null): self |
55 | | - { |
56 | | - if ($timeout instanceof Duration) { |
57 | | - $timeout = $timeout->getTotalSeconds(); |
58 | | - } |
59 | | - |
60 | | - $this->process->stop((float) $timeout, $signal); |
61 | | - |
62 | | - return $this; |
63 | | - } |
64 | | - |
65 | | - public function wait(?callable $output = null): ProcessResult |
66 | | - { |
67 | | - try { |
68 | | - $callback = $output |
69 | | - ? fn (string $type, mixed $data) => $output(OutputChannel::fromSymfonyOutputType($type), $data) |
70 | | - : null; |
| 37 | + /** |
| 38 | + * Sends a signal to the process. |
| 39 | + */ |
| 40 | + public function signal(int $signal): self; |
71 | 41 |
|
72 | | - $this->process->wait($callback); |
| 42 | + /** |
| 43 | + * Stops the process if it is currently running. |
| 44 | + * |
| 45 | + * @param float|int|Duration $timeout The maximum time to wait for the process to stop. |
| 46 | + * @param int|null $signal A POSIX signal to send to the process. |
| 47 | + */ |
| 48 | + public function stop(float|int|Duration $timeout = 10, ?int $signal = null): self; |
73 | 49 |
|
74 | | - return ProcessResult::fromSymfonyProcess($this->process); |
75 | | - } catch (SymfonyTimeoutException $exception) { |
76 | | - throw new ProcessExecutionHasTimedOut( |
77 | | - result: ProcessResult::fromSymfonyProcess($this->process), |
78 | | - original: $exception, |
79 | | - ); |
80 | | - } |
81 | | - } |
| 50 | + /** |
| 51 | + * Waits for the process to finish. |
| 52 | + * |
| 53 | + * @param null|callable(OutputChannel,string) $output The callback receives the type of output (out or err) and some bytes from the output in real-time while writing the standard input to the process. It allows to have feedback from the independent process during execution. |
| 54 | + */ |
| 55 | + public function wait(?callable $output = null): ProcessResult; |
82 | 56 | } |
0 commit comments