@@ -41,13 +41,35 @@ public function __construct($command, $cwd = null, $input = null, $env = null, $
41
41
$ this ->timeout = $ timeout ;
42
42
}
43
43
44
- public function execute ()
44
+ private function getDescriptors ()
45
+ {
46
+ return array (
47
+ self ::STDIN_DESCRIPTOR_KEY => array ("pipe " , "r " ),
48
+ self ::STDOUT_DESCRIPTOR_KEY => array ("pipe " , "w " ),
49
+ self ::STDERR_DESCRIPTOR_KEY => array ("pipe " , "r " )
50
+ );
51
+ }
52
+
53
+ private function setInput ()
45
54
{
46
- $ this ->start ();
47
- $ this ->wait ();
55
+ fwrite ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ], $ this ->input );
48
56
}
49
57
50
- private function start ()
58
+ private function updateStatus ()
59
+ {
60
+ $ this ->status = proc_get_status ($ this ->process );
61
+
62
+ return $ this ->status ;
63
+ }
64
+
65
+ private function closePipes ()
66
+ {
67
+ fclose ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ]);
68
+ fclose ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]);
69
+ fclose ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ]);
70
+ }
71
+
72
+ public function execute (bool $ blocking = false )
51
73
{
52
74
if ($ this ->isRunning ()) {
53
75
throw new RuntimeException ('Process is already running ' );
@@ -65,20 +87,10 @@ private function start()
65
87
66
88
$ this ->startTime = microtime (true );
67
89
$ this ->status = $ this ->updateStatus ();
68
- }
69
90
70
- private function getDescriptors ()
71
- {
72
- return array (
73
- self ::STDIN_DESCRIPTOR_KEY => array ("pipe " , "r " ),
74
- self ::STDOUT_DESCRIPTOR_KEY => array ("pipe " , "w " ),
75
- self ::STDERR_DESCRIPTOR_KEY => array ("pipe " , "r " )
76
- );
77
- }
78
-
79
- private function setInput ()
80
- {
81
- fwrite ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ], $ this ->input );
91
+ if ($ blocking ) {
92
+ $ this ->wait ();
93
+ }
82
94
}
83
95
84
96
public function getOutput ()
@@ -109,13 +121,6 @@ public function checkTimeout()
109
121
return $ this ->status ;
110
122
}
111
123
112
- private function updateStatus ()
113
- {
114
- $ this ->status = proc_get_status ($ this ->process );
115
-
116
- return $ this ->status ;
117
- }
118
-
119
124
public function wait ()
120
125
{
121
126
while ($ this ->isRunning ()) {
@@ -135,7 +140,7 @@ public function isRunning()
135
140
public function stop ()
136
141
{
137
142
if (!$ this ->isRunning ()) {
138
- throw new RuntimeException ( " No process to stop " );
143
+ return $ this -> getExitCode ( );
139
144
}
140
145
141
146
$ this ->closePipes ();
@@ -145,13 +150,6 @@ public function stop()
145
150
return $ this ->getExitCode ();
146
151
}
147
152
148
- private function closePipes ()
149
- {
150
- fclose ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ]);
151
- fclose ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]);
152
- fclose ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ]);
153
- }
154
-
155
153
public function kill ()
156
154
{
157
155
return proc_terminate ($ this ->process );
0 commit comments