Skip to content

Commit cd8d109

Browse files
author
Sushil Gupta
committed
refactor: minor refactor on messages + add isWindows() method using DIRECTORY_SEPARATOR check to set pipes
1 parent d88e33b commit cd8d109

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Helper/Shell.php

Lines changed: 15 additions & 10 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,7 +150,7 @@ 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
}
150155

151156
// @codeCoverageIgnoreStart
@@ -166,7 +171,7 @@ public function setOptions(string $cwd = null, array $env = null, float $timeout
166171
public function execute(bool $async = false): self
167172
{
168173
if ($this->isRunning()) {
169-
throw new RuntimeException('Process is already running');
174+
throw new RuntimeException('Process is already running.');
170175
}
171176

172177
$this->descriptors = $this->getDescriptors();
@@ -264,6 +269,6 @@ public function kill()
264269
public function __destruct()
265270
{
266271
// 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
272+
// Otherwise, waited already till it ends itself or timeout occurs, in which case kill it
268273
}
269274
}

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)