6
6
7
7
class ChildProcess
8
8
{
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
+
9
21
public function __construct (protected Client $ client ) {}
10
22
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
12
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 |array $ cmd
56
+ string $ alias ,
57
+ ?string $ cwd = null ,
58
+ ?array $ env = null ,
59
+ bool $ persistent = false
60
+ ): static {
13
61
$ cwd = $ cwd ?? base_path ();
14
62
15
63
if (is_string ($ cmd )) {
16
64
// When a string is passed, explode it on the space
17
65
$ cmd = array_values (array_filter (explode (' ' , $ cmd )));
18
66
}
19
67
20
- $ this ->client ->post ('child-process/start ' , [
68
+ $ process = $ this ->client ->post ('child-process/start ' , [
21
69
'alias ' => $ alias ,
22
70
'cmd ' => $ cmd ,
23
71
'cwd ' => $ cwd ,
24
72
'env ' => $ env ,
25
73
'persistent ' => $ persistent ,
26
74
])->json ();
27
75
28
- return $ this ;
76
+ return $ this -> fromRuntimeProcess ( $ process ) ;
29
77
}
30
78
31
79
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
35
83
return $ this ->start ($ cmd , $ alias , env: $ env , persistent: $ persistent );
36
84
}
37
85
38
- public function stop (string $ alias ): void
86
+ public function stop (? string $ alias = null ): void
39
87
{
40
88
$ 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 ,
42
97
])->json ();
98
+
99
+ if (! $ process ) {
100
+ return null ;
101
+ }
102
+
103
+ return $ this ->fromRuntimeProcess ($ process );
43
104
}
44
105
45
- public function message (mixed $ message , string $ alias ): void
106
+ public function message (string $ message , ? string $ alias = null ): static
46
107
{
47
108
$ this ->client ->post ('child-process/message ' , [
48
- 'message ' => json_encode ( $ message ) ,
49
- 'alias ' => $ alias ,
109
+ 'alias ' => $ alias ?? $ this -> alias ,
110
+ 'message ' => $ message ,
50
111
])->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 ;
51
127
}
52
128
}
0 commit comments