Skip to content

Commit 709bb6f

Browse files
committed
refactor: clarify class and interface names
1 parent 6b11bbd commit 709bb6f

12 files changed

+140
-135
lines changed

packages/process/src/GenericProcessExecutor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
final class GenericProcessExecutor implements ProcessExecutor
99
{
10-
public function start(array|string|PendingProcess $command): InvokedProcess
10+
public function start(array|string|PendingProcess $command): InvokedSystemProcess
1111
{
1212
$pending = $this->createPendingProcess($command);
1313
$command = $this->createSymfonyProcess($pending);
1414
$command->start();
1515

16-
return new InvokedProcess($command);
16+
return new InvokedSystemProcess($command);
1717
}
1818

1919
public function run(array|string|PendingProcess $command): ProcessResult

packages/process/src/InvokedProcess.php

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,55 @@
22

33
namespace Tempest\Process;
44

5-
use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException;
6-
use Symfony\Component\Process\Process as SymfonyProcess;
75
use Tempest\DateTime\Duration;
8-
use Tempest\Process\Exceptions\ProcessExecutionHasTimedOut;
96

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
148
{
159
/**
1610
* Gets the process identifier.
1711
*/
1812
public ?int $pid {
19-
get => $this->process->getPid();
13+
get;
2014
}
2115

2216
/**
2317
* Whether the process is running.
2418
*/
2519
public bool $running {
26-
get => $this->process->isRunning();
20+
get;
2721
}
2822

2923
/**
3024
* Gets the output of the process.
3125
*/
3226
public string $output {
33-
get => $this->process->getOutput();
27+
get;
3428
}
3529

3630
/**
3731
* Gets the error output of the process.
3832
*/
3933
public string $errorOutput {
40-
get => $this->process->getErrorOutput();
34+
get;
4135
}
4236

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;
7141

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;
7349

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;
8256
}

packages/process/src/InvokedProcessInterface.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/process/src/InvokedProcessPool.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ final class InvokedProcessPool implements Countable
1515
*/
1616
public ImmutableArray $running {
1717
get => $this->processes
18-
->filter(fn (InvokedProcessInterface $process) => $process->running)
18+
->filter(fn (InvokedProcess $process) => $process->running)
1919
->toImmutableArray();
2020
}
2121

2222
/**
2323
* All processes in the pool.
2424
*
25-
* @var ImmutableArray<InvokedProcessInterface>
25+
* @var ImmutableArray<InvokedProcess>
2626
*/
2727
public ImmutableArray $all {
2828
get => $this->processes->toImmutableArray();
@@ -38,15 +38,15 @@ public function __construct(
3838
*/
3939
public function signal(int $signal): ImmutableArray
4040
{
41-
return $this->running->each(fn (InvokedProcessInterface $process) => $process->signal($signal));
41+
return $this->running->each(fn (InvokedProcess $process) => $process->signal($signal));
4242
}
4343

4444
/**
4545
* Stops all processes that are currently running.
4646
*/
4747
public function stop(float|int|Duration $timeout = 10, ?int $signal = null): ImmutableArray
4848
{
49-
return $this->running->each(fn (InvokedProcessInterface $process) => $process->stop($timeout, $signal));
49+
return $this->running->each(fn (InvokedProcess $process) => $process->stop($timeout, $signal));
5050
}
5151

5252
/**
@@ -55,7 +55,7 @@ public function stop(float|int|Duration $timeout = 10, ?int $signal = null): Imm
5555
public function wait(): ProcessPoolResults
5656
{
5757
return new ProcessPoolResults(
58-
$this->running->map(fn (InvokedProcessInterface $process) => $process->wait()),
58+
$this->running->map(fn (InvokedProcess $process) => $process->wait()),
5959
);
6060
}
6161

@@ -64,7 +64,7 @@ public function wait(): ProcessPoolResults
6464
*/
6565
public function forEachRunning(\Closure $callback): self
6666
{
67-
$this->running->each(fn (InvokedProcessInterface $process) => $callback($process));
67+
$this->running->each(fn (InvokedProcess $process) => $callback($process));
6868

6969
return $this;
7070
}
@@ -74,7 +74,7 @@ public function forEachRunning(\Closure $callback): self
7474
*/
7575
public function forEach(\Closure $callback): self
7676
{
77-
$this->processes->each(fn (InvokedProcessInterface $process) => $callback($process));
77+
$this->processes->each(fn (InvokedProcess $process) => $callback($process));
7878

7979
return $this;
8080
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Tempest\Process;
4+
5+
use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException;
6+
use Symfony\Component\Process\Process as SymfonyProcess;
7+
use Tempest\DateTime\Duration;
8+
use Tempest\Process\Exceptions\ProcessExecutionHasTimedOut;
9+
10+
/**
11+
* Represents a process that has been invoked and is currently running or has completed.
12+
*/
13+
final class InvokedSystemProcess implements InvokedProcess
14+
{
15+
/**
16+
* Gets the process identifier.
17+
*/
18+
public ?int $pid {
19+
get => $this->process->getPid();
20+
}
21+
22+
/**
23+
* Whether the process is running.
24+
*/
25+
public bool $running {
26+
get => $this->process->isRunning();
27+
}
28+
29+
/**
30+
* Gets the output of the process.
31+
*/
32+
public string $output {
33+
get => $this->process->getOutput();
34+
}
35+
36+
/**
37+
* Gets the error output of the process.
38+
*/
39+
public string $errorOutput {
40+
get => $this->process->getErrorOutput();
41+
}
42+
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;
71+
72+
$this->process->wait($callback);
73+
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+
}
82+
}

packages/process/src/ProcessExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function run(array|string|PendingProcess $command): ProcessResult;
1616
*
1717
* @param string[]|string|PendingProcess $command
1818
*/
19-
public function start(array|string|PendingProcess $command): InvokedProcessInterface;
19+
public function start(array|string|PendingProcess $command): InvokedProcess;
2020

2121
/**
2222
* Returns a pool of processes, which can be executed.

packages/process/src/Testing/TestingInvokedProcess.php renamed to packages/process/src/Testing/InvokedTestingProcess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Tempest\Process\Testing;
44

55
use Tempest\DateTime\Duration;
6-
use Tempest\Process\InvokedProcessInterface;
6+
use Tempest\Process\InvokedProcess;
77
use Tempest\Process\OutputChannel;
88
use Tempest\Process\ProcessResult;
99

10-
final class TestingInvokedProcess implements InvokedProcessInterface
10+
final class InvokedTestingProcess implements InvokedProcess
1111
{
1212
public ?int $pid {
1313
get {

packages/process/src/Testing/ProcessTester.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use Closure;
66
use PHPUnit\Framework\Assert;
77
use PHPUnit\Framework\ExpectationFailedException;
8-
use PHPUnit\Framework\GeneratorNotSupportedException;
9-
use RuntimeException;
108
use Tempest\Container\Container;
11-
use Tempest\Container\GenericContainer;
129
use Tempest\Process\GenericProcessExecutor;
1310
use Tempest\Process\PendingProcess;
1411
use Tempest\Process\ProcessExecutor;
@@ -103,6 +100,14 @@ public function disableProcessExecution(): void
103100
$this->container->singleton(ProcessExecutor::class, new RestrictedProcessExecutor());
104101
}
105102

103+
/**
104+
* Describes how an asynchronous process is expected to behave.
105+
*/
106+
public function describe(): InvokedProcessDescription
107+
{
108+
return new InvokedProcessDescription();
109+
}
110+
106111
/**
107112
* Asserts that the given command has been ran. Alternatively, a callback may be passed.
108113
*

packages/process/src/Testing/RestrictedProcessExecutor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Tempest\Process\Testing;
44

5-
use Tempest\Process\InvokedProcess;
5+
use Tempest\Process\InvokedSystemProcess;
66
use Tempest\Process\PendingPool;
77
use Tempest\Process\PendingProcess;
88
use Tempest\Process\Pool;
@@ -17,7 +17,7 @@ public function run(array|string|PendingProcess $command): ProcessResult
1717
throw ProcessExecutionWasForbidden::forPendingProcess($command);
1818
}
1919

20-
public function start(array|string|PendingProcess $command): InvokedProcess
20+
public function start(array|string|PendingProcess $command): InvokedSystemProcess
2121
{
2222
throw ProcessExecutionWasForbidden::forPendingProcess($command);
2323
}

0 commit comments

Comments
 (0)