Skip to content

Commit 9a50f74

Browse files
committed
add php convenience methof
1 parent 31d78ae commit 9a50f74

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/ChildProcess.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,9 @@ public function start(
5959
bool $persistent = false
6060
): static {
6161

62-
if (is_string($cmd)) {
63-
// When a string is passed, explode it on the space
64-
$cmd = array_values(array_filter(explode(' ', $cmd)));
65-
}
66-
6762
$process = $this->client->post('child-process/start', [
6863
'alias' => $alias,
69-
'cmd' => $cmd,
64+
'cmd' => $this->explodeCommand($cmd),
7065
'cwd' => $cwd ?? base_path(),
7166
'env' => $env,
7267
'persistent' => $persistent,
@@ -75,9 +70,16 @@ public function start(
7570
return $this->fromRuntimeProcess($process);
7671
}
7772

73+
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
74+
{
75+
$cmd = [PHP_BINARY, ...$this->explodeCommand($cmd)];
76+
77+
return $this->start($cmd, $alias, env: $env, persistent: $persistent);
78+
}
79+
7880
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
7981
{
80-
$cmd = [PHP_BINARY, 'artisan', ...(array) $cmd];
82+
$cmd = [PHP_BINARY, 'artisan', ...$this->explodeCommand($cmd)];
8183

8284
return $this->start($cmd, $alias, env: $env, persistent: $persistent);
8385
}
@@ -124,4 +126,13 @@ protected function fromRuntimeProcess($process): static
124126

125127
return $this;
126128
}
129+
130+
private function explodeCommand(string|array $cmd): array
131+
{
132+
if (is_iterable($cmd)) {
133+
return $cmd;
134+
}
135+
136+
return array_values(array_filter(explode(' ', $cmd)));
137+
}
127138
}

tests/ChildProcess/ChildProcessTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
});
3333

3434
it('can start a php command', function () {
35-
ChildProcess::artisan("-r 'sleep(5);'", 'some-alias', ['baz' => 'zah']);
35+
ChildProcess::php("-r 'sleep(5);'", 'some-alias', ['baz' => 'zah']);
3636

3737
Http::assertSent(function (Request $request) {
3838
return $request->url() === 'http://localhost:4000/api/child-process/start' &&
@@ -41,15 +41,15 @@
4141
$request['cwd'] === base_path() &&
4242
$request['env'] === ['baz' => 'zah'];
4343
});
44-
})->todo();
44+
});
4545

4646
it('can start a artisan command', function () {
47-
ChildProcess::artisan('foo:bar', 'some-alias', ['baz' => 'zah']);
47+
ChildProcess::artisan('foo:bar --verbose', 'some-alias', ['baz' => 'zah']);
4848

4949
Http::assertSent(function (Request $request) {
5050
return $request->url() === 'http://localhost:4000/api/child-process/start' &&
5151
$request['alias'] === 'some-alias' &&
52-
$request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar'] &&
52+
$request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar', '--verbose'] &&
5353
$request['cwd'] === base_path() &&
5454
$request['env'] === ['baz' => 'zah'];
5555
});
@@ -64,12 +64,12 @@
6464
});
6565

6666
it('accepts either a string or a array as php command argument', function () {
67-
ChildProcess::artisan(" 'sleep(5);'", 'some-alias');
67+
ChildProcess::php("-r 'sleep(5);'", 'some-alias');
6868
Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, '-r', "'sleep(5);'"]);
6969

7070
ChildProcess::artisan(['-r', "'sleep(5);'"], 'some-alias');
7171
Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, '-r', "'sleep(5);'"]);
72-
})->todo();
72+
});
7373

7474
it('accepts either a string or a array as artisan command argument', function () {
7575
ChildProcess::artisan('foo:bar', 'some-alias');
@@ -119,7 +119,7 @@
119119
it('can mark a php command as persistent', function () {
120120
ChildProcess::php("-r 'sleep(5);'", 'some-alias', persistent: true);
121121
Http::assertSent(fn (Request $request) => $request['persistent'] === true);
122-
})->todo();
122+
});
123123

124124
it('can mark a artisan command as persistent', function () {
125125
ChildProcess::artisan('foo:bar', 'some-alias', persistent: true);

0 commit comments

Comments
 (0)