Skip to content

Commit fea498a

Browse files
authored
Merge pull request #43 from adhocore/develop
refactor: minor refactor on messages + add isWindows() method using D…
2 parents d88e33b + f390b6b commit fea498a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/Helper/Shell.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,23 @@ class Shell
6464
/** @var resource The actual process resource returned from proc_open */
6565
protected $process = null;
6666

67-
/** @var array Status of the process as returned from proc_get_status */
68-
protected $processStatus = null;
69-
7067
/** @var int Process starting time in unix timestamp */
7168
protected $processStartTime;
7269

73-
/** @var string Current state of the shell execution */
74-
protected $state = self::STATE_READY;
70+
/** @var array Status of the process as returned from proc_get_status */
71+
protected $processStatus = null;
7572

7673
/** @var float Default timeout for the process in seconds with microseconds */
7774
protected $processTimeout = null;
7875

76+
/** @var string Current state of the shell execution, set from this class, NOT for proc_get_status */
77+
protected $state = self::STATE_READY;
78+
7979
public function __construct(string $command, string $input = null)
8080
{
8181
// @codeCoverageIgnoreStart
8282
if (!\function_exists('proc_open')) {
83-
throw new RuntimeException('Required proc_open could not be found in your PHP setup');
83+
throw new RuntimeException('Required proc_open could not be found in your PHP setup.');
8484
}
8585
// @codeCoverageIgnoreEnd
8686

@@ -90,7 +90,7 @@ public function __construct(string $command, string $input = null)
9090

9191
protected function getDescriptors(): array
9292
{
93-
$out = '\\' === \DIRECTORY_SEPARATOR ? ['file', 'NUL', 'w'] : ['pipe', 'w'];
93+
$out = $this->isWindows() ? ['file', 'NUL', 'w'] : ['pipe', 'w'];
9494

9595
return [
9696
self::STDIN_DESCRIPTOR_KEY => ['pipe', 'r'],
@@ -99,6 +99,11 @@ protected function getDescriptors(): array
9999
];
100100
}
101101

102+
protected function isWindows(): bool
103+
{
104+
return '\\' === \DIRECTORY_SEPARATOR;
105+
}
106+
102107
protected function setInput()
103108
{
104109
\fwrite($this->pipes[self::STDIN_DESCRIPTOR_KEY], $this->input);
@@ -145,12 +150,8 @@ protected function checkTimeout()
145150
if ($executionDuration > $this->processTimeout) {
146151
$this->kill();
147152

148-
throw new RuntimeException('Process timeout occurred, terminated');
153+
throw new RuntimeException('Timeout occurred, process terminated.');
149154
}
150-
151-
// @codeCoverageIgnoreStart
152-
153-
// @codeCoverageIgnoreEnd
154155
}
155156

156157
public function setOptions(string $cwd = null, array $env = null, float $timeout = null, array $otherOptions = []): self
@@ -166,7 +167,7 @@ public function setOptions(string $cwd = null, array $env = null, float $timeout
166167
public function execute(bool $async = false): self
167168
{
168169
if ($this->isRunning()) {
169-
throw new RuntimeException('Process is already running');
170+
throw new RuntimeException('Process is already running.');
170171
}
171172

172173
$this->descriptors = $this->getDescriptors();
@@ -264,6 +265,6 @@ public function kill()
264265
public function __destruct()
265266
{
266267
// If async (run in background) => we don't care if it ever closes
267-
// Otherwise, waited already till it runs or timeout occurs, in which case kill it
268+
// Otherwise, waited already till it ends itself or timeout occurs, in which case kill it
268269
}
269270
}

tests/Helper/ShellTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function test_async_stop()
5353

5454
/**
5555
* @expectedException \RuntimeException
56-
* @expectedExceptionMessage Process timeout occurred
56+
* @expectedExceptionMessage Timeout occurred, process terminated.
5757
*/
5858
public function test_timeout()
5959
{

0 commit comments

Comments
 (0)