Skip to content

Commit 205daed

Browse files
committed
Refactored to add another state variable to store actual state of the shell execution vs the process status
1 parent 62acd2d commit 205daed

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/Helper/Shell.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Shell
2424
const STDOUT_DESCRIPTOR_KEY = 1;
2525
const STDERR_DESCRIPTOR_KEY = 2;
2626

27+
const STATE_READY = 'ready';
28+
const STATE_STARTED = 'started';
29+
const STATE_TERMINATED = 'terminated';
30+
2731
/** @var string Command to be executed */
2832
protected $command;
2933

@@ -42,8 +46,11 @@ class Shell
4246
/** @var resource The actual process resource returned from proc_open */
4347
protected $process = null;
4448

49+
/** @var string Current state of the shell execution */
50+
protected $state = self::STATE_READY;
51+
4552
/** @var string Status of the process as returned from proc_get_status */
46-
protected $status = null;
53+
protected $processStatus = null;
4754

4855
public function __construct(string $command, string $input = null)
4956
{
@@ -71,12 +78,16 @@ protected function setInput()
7178
\fwrite($this->pipes[self::STDIN_DESCRIPTOR_KEY], $this->input);
7279
}
7380

74-
protected function updateStatus()
81+
protected function updateProcessStatus()
7582
{
76-
$this->status = \proc_get_status($this->process);
83+
if (self::STATE_STARTED !== $this->state) {
84+
return;
85+
}
86+
87+
$this->processStatus = \proc_get_status($this->process);
7788

78-
if ($this->status['running'] === false && $this->exitCode === null) {
79-
$this->exitCode = $this->status['exitcode'];
89+
if ($this->processStatus['running'] === false && $this->exitCode === null) {
90+
$this->exitCode = $this->processStatus['exitcode'];
8091
}
8192
}
8293

@@ -101,15 +112,17 @@ public function execute()
101112
throw new RuntimeException('Bad program could not be started.');
102113
}
103114

115+
$this->state = self::STATE_STARTED;
116+
104117
$this->setInput();
105-
$this->updateStatus();
118+
$this->updateProcessStatus();
106119
}
107120

108-
public function getStatus()
121+
public function getState()
109122
{
110-
$this->updateStatus();
123+
$this->updateProcessStatus();
111124

112-
return $this->status;
125+
return $this->state;
113126
}
114127

115128
public function getOutput()
@@ -124,21 +137,25 @@ public function getErrorOutput()
124137

125138
public function getExitCode()
126139
{
127-
$this->updateStatus();
140+
$this->updateProcessStatus();
128141

129142
return $this->exitCode;
130143
}
131144

132145
public function isRunning()
133146
{
134-
$this->updateStatus();
147+
if (self::STATE_STARTED !== $this->state) {
148+
return false;
149+
}
150+
151+
$this->updateProcessStatus();
135152

136-
return $this->status['running'];
153+
return $this->processStatus['running'];
137154
}
138155

139156
public function getProcessId()
140157
{
141-
return $this->isRunning() ? $this->status['pid'] : null;
158+
return $this->isRunning() ? $this->processStatus['pid'] : null;
142159
}
143160

144161
public function stop()
@@ -149,7 +166,8 @@ public function stop()
149166
\proc_close($this->process);
150167
}
151168

152-
$this->exitCode = $this->status['exitcode'];
169+
$this->state = self::STATE_TERMINATED;
170+
$this->exitCode = $this->processStatus['exitcode'];
153171

154172
return $this->exitCode;
155173
}

0 commit comments

Comments
 (0)