Skip to content

Commit e446a66

Browse files
authored
Merge branch 'feature/child-processes' into feature/child-processes
2 parents 58a5539 + 62ce895 commit e446a66

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

src/ChildProcess.php

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,74 @@
66

77
class ChildProcess
88
{
9+
public readonly int $pid;
10+
11+
public readonly string $alias;
12+
13+
public readonly array $cmd;
14+
15+
public readonly ?string $cwd;
16+
17+
public readonly ?array $env;
18+
19+
public readonly bool $persistent;
20+
921
public function __construct(protected Client $client) {}
1022

11-
public function start(string|array $cmd, string $alias, ?string $cwd = null, ?array $env = null, ?bool $persistent = false): self
23+
public function get(?string $alias = null): ?static
24+
{
25+
$alias = $alias ?? $this->alias;
26+
27+
$process = $this->client->get("child-process/get/{$alias}")->json();
28+
29+
if (! $process) {
30+
return null;
31+
}
32+
33+
return $this->fromRuntimeProcess($process);
34+
}
35+
36+
public function all(): array
1237
{
38+
$processes = $this->client->get('child-process/')->json();
39+
40+
if (empty($processes)) {
41+
return [];
42+
}
43+
44+
$hydrated = [];
45+
46+
foreach ($processes as $alias => $process) {
47+
$hydrated[$alias] = (new static($this->client))
48+
->fromRuntimeProcess($process);
49+
}
50+
51+
return $hydrated;
52+
}
53+
54+
public function start(
55+
string|array $cmd
56+
string $alias,
57+
?string $cwd = null,
58+
?array $env = null,
59+
bool $persistent = false
60+
): static {
1361
$cwd = $cwd ?? base_path();
1462

1563
if (is_string($cmd)) {
1664
// When a string is passed, explode it on the space
1765
$cmd = array_values(array_filter(explode(' ', $cmd)));
1866
}
1967

20-
$this->client->post('child-process/start', [
68+
$process = $this->client->post('child-process/start', [
2169
'alias' => $alias,
2270
'cmd' => $cmd,
2371
'cwd' => $cwd,
2472
'env' => $env,
2573
'persistent' => $persistent,
2674
])->json();
2775

28-
return $this;
76+
return $this->fromRuntimeProcess($process);
2977
}
3078

3179
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
@@ -35,18 +83,46 @@ public function artisan(string|array $cmd, string $alias, ?array $env = null, ?b
3583
return $this->start($cmd, $alias, env: $env, persistent: $persistent);
3684
}
3785

38-
public function stop(string $alias): void
86+
public function stop(?string $alias = null): void
3987
{
4088
$this->client->post('child-process/stop', [
41-
'alias' => $alias,
89+
'alias' => $alias ?? $this->alias,
90+
])->json();
91+
}
92+
93+
public function restart(?string $alias = null): ?static
94+
{
95+
$process = $this->client->post('child-process/restart', [
96+
'alias' => $alias ?? $this->alias,
4297
])->json();
98+
99+
if (! $process) {
100+
return null;
101+
}
102+
103+
return $this->fromRuntimeProcess($process);
43104
}
44105

45-
public function message(mixed $message, string $alias): void
106+
public function message(string $message, ?string $alias = null): static
46107
{
47108
$this->client->post('child-process/message', [
48-
'message' => json_encode($message),
49-
'alias' => $alias,
109+
'alias' => $alias ?? $this->alias,
110+
'message' => $message,
50111
])->json();
112+
113+
return $this;
114+
}
115+
116+
private function fromRuntimeProcess($process): static
117+
{
118+
if (isset($process['pid'])) {
119+
$this->pid = $process['pid'];
120+
}
121+
122+
foreach ($process['settings'] as $key => $value) {
123+
$this->{$key} = $value;
124+
}
125+
126+
return $this;
51127
}
52128
}

src/Facades/ChildProcess.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @method static string start(string $alias, array $cmd, string $cwd = null, array $env = null)
9-
* @method static string stop(string $alias)
10-
* @method static string message(string $alias, mixed $message)
8+
* @method static \Native\Laravel\ChildProcess[] all()
9+
* @method static \Native\Laravel\ChildProcess get(string $alias)
10+
* @method static \Native\Laravel\ChildProcess message(string $message, string $alias = null)
11+
* @method static \Native\Laravel\ChildProcess restart(string $alias)
12+
* @method static \Native\Laravel\ChildProcess start(string $alias, array $cmd, string $cwd = null, array $env = null, bool $persistent = false)
13+
* @method static void stop(string $alias)
1114
*/
1215
class ChildProcess extends Facade
1316
{

0 commit comments

Comments
 (0)