Skip to content

Commit f81237e

Browse files
committed
Added set options method
1 parent 2d44553 commit f81237e

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Helper/Shell.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ class Shell
3434
/** @var string Command to be executed */
3535
protected $command;
3636

37+
/** @var string Current working directory */
38+
protected $cwd = null;
39+
3740
/** @var array Descriptor to be passed for proc_open */
3841
protected $descriptors;
3942

43+
/** @var array An array of environment variables */
44+
protected $env = null;
45+
4046
/** @var int Exit code of the process once it has been terminated */
4147
protected $exitCode = null;
4248

4349
/** @var string Input for stdin */
4450
protected $input;
4551

52+
/** @var array Other options to be passed for proc_open */
53+
protected $otherOptions = [];
54+
4655
/** @var array Pointers to stdin, stdout & stderr */
4756
protected $pipes = null;
4857

@@ -59,7 +68,7 @@ class Shell
5968
protected $state = self::STATE_READY;
6069

6170
/** @var float Default timeout for the process in seconds with microseconds */
62-
protected $processTimeoutPeriod = 10;
71+
protected $processTimeoutPeriod = null;
6372

6473
public function __construct(string $command, string $input = null)
6574
{
@@ -110,7 +119,7 @@ protected function closePipes()
110119
public function wait()
111120
{
112121
while ($this->isRunning()) {
113-
usleep(500);
122+
usleep(5000);
114123
$this->checkTimeout();
115124
}
116125

@@ -123,12 +132,28 @@ public function checkTimeout()
123132
return;
124133
}
125134

135+
if($this->processTimeoutPeriod === null) {
136+
return;
137+
}
138+
126139
$execution_duration = \microtime(true) - $this->processStartTime;
127140

128141
if ($execution_duration > $this->processTimeoutPeriod) {
129142
$this->stop();
130143
throw new RuntimeException("Process timeout occurred");
131144
}
145+
146+
return;
147+
}
148+
149+
public function setOptions(string $cwd = null, array $env = null, float $timeout = null, $otherOptions = [])
150+
{
151+
$this->cwd = $cwd;
152+
$this->env = $env;
153+
$this->processTimeoutPeriod = $timeout;
154+
$this->otherOptions = $otherOptions;
155+
156+
return $this;
132157
}
133158

134159
public function execute($async = false)
@@ -139,7 +164,7 @@ public function execute($async = false)
139164

140165
$this->descriptors = $this->getDescriptors();
141166

142-
$this->process = \proc_open($this->command, $this->descriptors, $this->pipes);
167+
$this->process = \proc_open($this->command, $this->descriptors, $this->pipes, $this->cwd, $this->env, $this->otherOptions);
143168

144169
if (!\is_resource($this->process)) {
145170
throw new RuntimeException('Bad program could not be started.');

0 commit comments

Comments
 (0)