Skip to content

Commit 1176d51

Browse files
committed
wip
1 parent 4666245 commit 1176d51

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

src/ChildProcess/ChildProcess.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Native\Laravel\ChildProcess;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use Native\Laravel\Client\Client;
7+
use Native\Laravel\Events\ChildProcess\ErrorReceived;
8+
use Native\Laravel\Events\ChildProcess\MessageReceived;
9+
use Native\Laravel\Events\ChildProcess\ProcessSpawned;
10+
use Native\Laravel\Events\ChildProcess\ProcessExited;
11+
12+
class ChildProcess
13+
{
14+
private string $alias;
15+
private ?array $process;
16+
17+
public function __construct(protected Client $client) {}
18+
19+
public function start(string $alias, array $cmd, string $cwd = null, array $env = null): object
20+
{
21+
$this->alias = $alias;
22+
23+
$this->process = $this->client->post('child-process/start', [
24+
'alias' => $alias,
25+
'cmd' => $cmd,
26+
'cwd' => base_path(),
27+
'env' => $env,
28+
])->json();
29+
30+
return $this;
31+
}
32+
33+
public function stop(string $alias): void
34+
{
35+
$this->client->post('child-process/stop', [
36+
'alias' => $alias,
37+
])->json();
38+
}
39+
40+
public function message(string $alias, mixed $message): void
41+
{
42+
$this->client->post('child-process/message', [
43+
'alias' => $alias,
44+
'message' => json_encode($message),
45+
])->json();
46+
}
47+
48+
public function onMessage(\Closure $callback) {
49+
// Event::listen(function (MessageReceived $event) use ($callback) {
50+
// if ($event->alias !== $this->alias) {
51+
// return;
52+
// }
53+
//
54+
// $callback($event);
55+
// );
56+
return $this;
57+
}
58+
59+
public function onError(\Closure $callback) {
60+
// Event::listen(function (ErrorReceived $event) use ($callback) {
61+
// if ($event->alias !== $this->alias) {
62+
// return;
63+
// }
64+
//
65+
// $callback($event);
66+
// );
67+
return $this;
68+
}
69+
70+
public function onSpawn(\Closure $callback) {
71+
// Event::listen(function (ProcessSpawned $event) use ($callback) {
72+
// if ($event->alias !== $this->alias) {
73+
// return;
74+
// }
75+
//
76+
// $callback($event);
77+
// );
78+
return $this;
79+
}
80+
81+
public function onExit(\Closure $callback) {
82+
// Event::listen(function (ProcessExited $event) use ($callback) {
83+
// if ($event->alias !== $this->alias) {
84+
// return;
85+
// }
86+
//
87+
// $callback($event);
88+
// );
89+
return $this;
90+
}
91+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Native\Laravel\ChildProcess;
4+
5+
use Native\Laravel\Client\Client;
6+
7+
class ChildProcessManager
8+
{
9+
public function __construct(protected Client $client) {}
10+
11+
public function start(string $alias, array $cmd, string $cwd = null, array $env = null): object
12+
{
13+
$this->process = $this->client->post('child-process/start')->json([
14+
'alias' => $alias,
15+
'cmd' => $cmd,
16+
'cwd' => $cwd,
17+
'env' => $env,
18+
]);
19+
}
20+
21+
public function onMessage() {
22+
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\ChildProcess;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class ProcessSpawned implements ShouldBroadcast
11+
{
12+
use Dispatchable, SerializesModels;
13+
14+
public function __construct(public array $item) {}
15+
16+
public function broadcastOn()
17+
{
18+
return [
19+
new Channel('nativephp'),
20+
];
21+
}
22+
}

src/Facades/ChildProcess.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
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)
11+
*/
12+
class ChildProcess extends Facade
13+
{
14+
protected static function getFacadeAccessor()
15+
{
16+
return \Native\Laravel\ChildProcess\ChildProcess::class;
17+
}
18+
}

0 commit comments

Comments
 (0)