@@ -41,13 +41,35 @@ public function __construct($command, $cwd = null, $input = null, $env = null, $
4141 $ this ->timeout = $ timeout ;
4242 }
4343
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 ()
4554 {
46- $ this ->start ();
47- $ this ->wait ();
55+ fwrite ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ], $ this ->input );
4856 }
4957
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 )
5173 {
5274 if ($ this ->isRunning ()) {
5375 throw new RuntimeException ('Process is already running ' );
@@ -65,20 +87,10 @@ private function start()
6587
6688 $ this ->startTime = microtime (true );
6789 $ this ->status = $ this ->updateStatus ();
68- }
6990
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+ }
8294 }
8395
8496 public function getOutput ()
@@ -109,13 +121,6 @@ public function checkTimeout()
109121 return $ this ->status ;
110122 }
111123
112- private function updateStatus ()
113- {
114- $ this ->status = proc_get_status ($ this ->process );
115-
116- return $ this ->status ;
117- }
118-
119124 public function wait ()
120125 {
121126 while ($ this ->isRunning ()) {
@@ -135,7 +140,7 @@ public function isRunning()
135140 public function stop ()
136141 {
137142 if (!$ this ->isRunning ()) {
138- throw new RuntimeException ( " No process to stop " );
143+ return $ this -> getExitCode ( );
139144 }
140145
141146 $ this ->closePipes ();
@@ -145,13 +150,6 @@ public function stop()
145150 return $ this ->getExitCode ();
146151 }
147152
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-
155153 public function kill ()
156154 {
157155 return proc_terminate ($ this ->process );
0 commit comments