Skip to content

Commit c061e67

Browse files
author
Greg Bowler
authored
Merge pull request #9 from PhpGt/subprocess
Allow execution in subprocess
2 parents 2691be9 + dbe9728 commit c061e67

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/Process.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,23 @@ public function __destruct() {
3434
* Runs the command in a concurrent thread.
3535
* Sets the input, output and errors streams.
3636
*/
37-
public function exec(bool $blocking = false) {
37+
public function exec(bool $exec = true, bool $blocking = false) {
3838
$descriptor = [
3939
0 => ["pipe", "r"],
4040
1 => ["pipe", "w"],
4141
2 => ["pipe", "w"],
4242
];
4343

44+
$cmd = $this->command;
45+
if($exec) {
46+
$cmd = "exec " . $cmd;
47+
}
48+
4449
$oldCwd = getcwd();
4550
chdir($this->cwd);
4651

4752
$this->process = proc_open(
48-
$this->command,
53+
$cmd,
4954
$descriptor,
5055
$this->pipes
5156
);
@@ -68,11 +73,12 @@ public function exec(bool $blocking = false) {
6873
public function isRunning():bool {
6974
// Special care has to be taken to not call proc_get_status more than once
7075
// after the process has ended. See https://php.net/manual/function.proc-get-status.php
71-
if($this->status["running"]) {
76+
if($this->status["running"] ?? null) {
7277
$this->status = proc_get_status($this->process);
7378
}
7479

75-
return (bool)$this->status["running"];
80+
$running = $this->status["running"] ?? false;
81+
return (bool)$running;
7682
}
7783

7884
public function getCommand():string {
@@ -112,7 +118,7 @@ public function getPid():?int {
112118
return null;
113119
}
114120

115-
return $this->status["pid"];
121+
return $this->status["pid"] ?? null;
116122
}
117123

118124
/** Closes the thread and the streams then returns the return code of the command. */

test/unit/ProcessTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testExec() {
7474

7575
public function testExecFailure() {
7676
$sut = new Process("/this/does/not/exist/" . uniqid());
77-
$sut->exec(true);
77+
$sut->exec(true, true);
7878
self::assertEquals(127, $sut->getExitCode());
7979
}
8080

@@ -167,7 +167,7 @@ public function testExecBlocking() {
167167
self::assertTrue($sut->isRunning());
168168

169169
$sut = new Process("sleep 0.1");
170-
$sut->exec(true);
170+
$sut->exec(true, true);
171171
self::assertFalse($sut->isRunning());
172172
}
173173
}

0 commit comments

Comments
 (0)