Skip to content

Commit ad8ef09

Browse files
committed
improvements
- ChildProcess instances can be used to interact with a process - get, all and restart are piped up
1 parent 233675a commit ad8ef09

File tree

2 files changed

+89
-14
lines changed

2 files changed

+89
-14
lines changed

src/ChildProcess.php

+83-11
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,110 @@
66

77
class ChildProcess
88
{
9-
private string $alias;
9+
public readonly int $pid;
1010

11-
private ?array $process;
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;
1220

1321
public function __construct(protected Client $client) {}
1422

15-
public function start(string $alias, array $cmd, ?string $cwd = null, ?array $env = null): object
23+
public function get(string $alias = null): ?static
1624
{
17-
$this->alias = $alias;
25+
$alias = $alias ?? $this->alias;
26+
27+
$process = $this->client->get("child-process/get/{$alias}")->json();
1828

19-
$this->process = $this->client->post('child-process/start', [
29+
if (! $process) {
30+
return null;
31+
}
32+
33+
return $this->fromRuntimeProcess($process);
34+
}
35+
36+
public function all(): array
37+
{
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 $alias,
56+
array $cmd,
57+
?string $cwd = null,
58+
?array $env = null,
59+
bool $persistent = false
60+
): static
61+
{
62+
$process = $this->client->post('child-process/start', [
2063
'alias' => $alias,
2164
'cmd' => $cmd,
2265
'cwd' => base_path(),
2366
'env' => $env,
67+
'persistent' => $persistent,
2468
])->json();
2569

26-
return $this;
70+
return $this->fromRuntimeProcess($process);
2771
}
2872

29-
public function stop(string $alias): void
73+
public function stop(string $alias = null): void
3074
{
3175
$this->client->post('child-process/stop', [
32-
'alias' => $alias,
76+
'alias' => $alias ?? $this->alias,
77+
])->json();
78+
}
79+
80+
public function restart(string $alias = null): ?static
81+
{
82+
$process = $this->client->post('child-process/restart', [
83+
'alias' => $alias ?? $this->alias,
3384
])->json();
85+
86+
if (! $process) {
87+
return null;
88+
}
89+
90+
return $this->fromRuntimeProcess($process);
3491
}
3592

36-
public function message(string $alias, mixed $message): void
93+
public function message(string $message, string $alias = null): static
3794
{
3895
$this->client->post('child-process/message', [
39-
'alias' => $alias,
40-
'message' => json_encode($message),
96+
'alias' => $alias ?? $this->alias,
97+
'message' => $message,
4198
])->json();
99+
100+
return $this;
101+
}
102+
103+
private function fromRuntimeProcess($process): static
104+
{
105+
if (isset($process['pid'])) {
106+
$this->pid = $process['pid'];
107+
}
108+
109+
foreach ($process['settings'] as $key => $value) {
110+
$this->{$key} = $value;
111+
}
112+
113+
return $this;
42114
}
43115
}

src/Facades/ChildProcess.php

+6-3
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)