@@ -34,15 +34,24 @@ class Shell
34
34
/** @var string Command to be executed */
35
35
protected $ command ;
36
36
37
+ /** @var string Current working directory */
38
+ protected $ cwd = null ;
39
+
37
40
/** @var array Descriptor to be passed for proc_open */
38
41
protected $ descriptors ;
39
42
43
+ /** @var array An array of environment variables */
44
+ protected $ env = null ;
45
+
40
46
/** @var int Exit code of the process once it has been terminated */
41
47
protected $ exitCode = null ;
42
48
43
49
/** @var string Input for stdin */
44
50
protected $ input ;
45
51
52
+ /** @var array Other options to be passed for proc_open */
53
+ protected $ otherOptions = [];
54
+
46
55
/** @var array Pointers to stdin, stdout & stderr */
47
56
protected $ pipes = null ;
48
57
@@ -59,7 +68,7 @@ class Shell
59
68
protected $ state = self ::STATE_READY ;
60
69
61
70
/** @var float Default timeout for the process in seconds with microseconds */
62
- protected $ processTimeoutPeriod = 10 ;
71
+ protected $ processTimeoutPeriod = null ;
63
72
64
73
public function __construct (string $ command , string $ input = null )
65
74
{
@@ -110,7 +119,7 @@ protected function closePipes()
110
119
public function wait ()
111
120
{
112
121
while ($ this ->isRunning ()) {
113
- usleep (500 );
122
+ usleep (5000 );
114
123
$ this ->checkTimeout ();
115
124
}
116
125
@@ -123,12 +132,28 @@ public function checkTimeout()
123
132
return ;
124
133
}
125
134
135
+ if ($ this ->processTimeoutPeriod === null ) {
136
+ return ;
137
+ }
138
+
126
139
$ execution_duration = \microtime (true ) - $ this ->processStartTime ;
127
140
128
141
if ($ execution_duration > $ this ->processTimeoutPeriod ) {
129
142
$ this ->stop ();
130
143
throw new RuntimeException ("Process timeout occurred " );
131
144
}
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 ;
132
157
}
133
158
134
159
public function execute ($ async = false )
@@ -139,7 +164,7 @@ public function execute($async = false)
139
164
140
165
$ this ->descriptors = $ this ->getDescriptors ();
141
166
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 );
143
168
144
169
if (!\is_resource ($ this ->process )) {
145
170
throw new RuntimeException ('Bad program could not be started. ' );
0 commit comments