Skip to content

Commit b415615

Browse files
committed
feat(process): introduce process component
1 parent d0a4e1e commit b415615

33 files changed

+2218
-3
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"psr/http-message": "^1.0|^2.0",
3232
"psr/log": "^3.0.0",
3333
"symfony/cache": "^7.2",
34-
"symfony/process": "^7.1",
34+
"symfony/process": "^7.3",
3535
"symfony/uid": "^7.1",
3636
"symfony/var-dumper": "^7.1",
3737
"symfony/var-exporter": "^7.1",
@@ -87,6 +87,7 @@
8787
"tempest/intl": "self.version",
8888
"tempest/log": "self.version",
8989
"tempest/mapper": "self.version",
90+
"tempest/process": "self.version",
9091
"tempest/reflection": "self.version",
9192
"tempest/router": "self.version",
9293
"tempest/storage": "self.version",
@@ -123,6 +124,7 @@
123124
"Tempest\\Intl\\": "packages/intl/src",
124125
"Tempest\\Log\\": "packages/log/src",
125126
"Tempest\\Mapper\\": "packages/mapper/src",
127+
"Tempest\\Process\\": "packages/process/src",
126128
"Tempest\\Reflection\\": "packages/reflection/src",
127129
"Tempest\\Router\\": "packages/router/src",
128130
"Tempest\\Storage\\": "packages/storage/src",
@@ -183,6 +185,7 @@
183185
"Tempest\\Intl\\Tests\\": "packages/intl/tests",
184186
"Tempest\\Log\\Tests\\": "packages/log/tests",
185187
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",
188+
"Tempest\\Process\\Tests\\": "packages/process/tests",
186189
"Tempest\\Reflection\\Tests\\": "packages/reflection/tests",
187190
"Tempest\\Router\\Tests\\": "packages/router/tests",
188191
"Tempest\\Storage\\Tests\\": "packages/storage/tests",

packages/process/.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output
10+
*.view.php diff=html
11+
*.php diff=php
12+
*.css diff=css
13+
*.html diff=html
14+
*.md diff=markdown

packages/process/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/process/composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "tempest/process",
3+
"description": "A component for working with processes.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"symfony/process": "^7.3",
9+
"tempest/container": "dev-main",
10+
"tempest/support": "dev-main"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"Tempest\\Process\\": "src"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Tempest\\Process\\Tests\\": "tests"
20+
}
21+
}
22+
}

packages/process/phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
executionOrder="depends,defects"
7+
beStrictAboutOutputDuringTests="true"
8+
displayDetailsOnPhpunitDeprecations="true"
9+
failOnPhpunitDeprecation="false"
10+
failOnRisky="true"
11+
failOnWarning="true"
12+
>
13+
<testsuites>
14+
<testsuite name="Tempest Process">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
23+
</phpunit>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Tempest\Process\Exceptions;
4+
5+
use Throwable;
6+
7+
interface ProcessException extends Throwable
8+
{
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tempest\Process\Exceptions;
4+
5+
use Exception;
6+
use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyProcessTimedOutException;
7+
use Tempest\Process\ProcessResult;
8+
9+
final class ProcessExecutionHasTimedOut extends Exception implements ProcessException
10+
{
11+
public function __construct(
12+
public readonly ProcessResult $result,
13+
public readonly SymfonyProcessTimedOutException $original,
14+
) {
15+
parent::__construct($original->getMessage(), $original->getCode(), $original);
16+
}
17+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Tempest\Process;
4+
5+
use Symfony\Component\Process\Process as SymfonyProcess;
6+
use Tempest\Support\Arr\ImmutableArray;
7+
8+
final class GenericProcessExecutor implements ProcessExecutor
9+
{
10+
public function start(array|string|PendingProcess $command): InvokedProcess
11+
{
12+
$pending = $this->createPendingProcess($command);
13+
$command = $this->createSymfonyProcess($pending);
14+
$command->start();
15+
16+
return new InvokedProcess($command);
17+
}
18+
19+
public function run(array|string|PendingProcess $command): ProcessResult
20+
{
21+
return $this->start($command)->wait();
22+
}
23+
24+
public function pool(iterable $pool): Pool
25+
{
26+
return new Pool(
27+
pendingProcesses: new ImmutableArray($pool)->map($this->createPendingProcess(...)),
28+
processExecutor: $this,
29+
);
30+
}
31+
32+
public function concurrently(iterable $pool): ProcessPoolResults
33+
{
34+
return $this->pool($pool)->start()->wait();
35+
}
36+
37+
private function createPendingProcess(array|string|PendingProcess $processOrCommand): PendingProcess
38+
{
39+
if ($processOrCommand instanceof PendingProcess) {
40+
return $processOrCommand;
41+
}
42+
43+
return new PendingProcess(command: $processOrCommand);
44+
}
45+
46+
private function createSymfonyProcess(PendingProcess $pending): SymfonyProcess
47+
{
48+
$process = is_iterable($pending->command)
49+
? new SymfonyProcess($pending->command, env: $pending->environment)
50+
: SymfonyProcess::fromShellCommandline((string) $pending->command, env: $pending->environment);
51+
52+
$process->setWorkingDirectory((string) ($pending->path ?? getcwd()));
53+
$process->setTimeout($pending->timeout?->getTotalSeconds());
54+
55+
if ($pending->idleTimeout) {
56+
$process->setIdleTimeout($pending->idleTimeout->getTotalSeconds());
57+
}
58+
59+
if ($pending->input) {
60+
$process->setInput($pending->input);
61+
}
62+
63+
if ($pending->quietly) {
64+
$process->disableOutput();
65+
}
66+
67+
if ($pending->tty) {
68+
$process->setTty(true);
69+
}
70+
71+
if ($pending->options !== []) {
72+
$process->setOptions($pending->options);
73+
}
74+
75+
return $process;
76+
}
77+
}
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 InvokedProcess implements InvokedProcessInterface
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tempest\Process;
4+
5+
use Tempest\DateTime\Duration;
6+
7+
interface InvokedProcessInterface
8+
{
9+
/**
10+
* Gets the process identifier.
11+
*/
12+
public ?int $pid {
13+
get;
14+
}
15+
16+
/**
17+
* Whether the process is running.
18+
*/
19+
public bool $running {
20+
get;
21+
}
22+
23+
/**
24+
* Gets the output of the process.
25+
*/
26+
public string $output {
27+
get;
28+
}
29+
30+
/**
31+
* Gets the error output of the process.
32+
*/
33+
public string $errorOutput {
34+
get;
35+
}
36+
37+
/**
38+
* Sends a signal to the process.
39+
*/
40+
public function signal(int $signal): self;
41+
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;
49+
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;
56+
}

0 commit comments

Comments
 (0)