44namespace SOS \MultiProcess \Classes ;
55
66
7+ use Symfony \Component \Process \Exception \ProcessFailedException ;
8+ use Symfony \Component \Process \PhpProcess ;
79use Symfony \Component \Process \Process ;
810
911/**
@@ -32,6 +34,7 @@ class MultiProcess
3234 'workingDirectory ' => null ,
3335 'enableOutput ' => true ,
3436 'processTime ' => 3 ,
37+ 'throwIfTaskNotSuccess ' => false ,
3538 ];
3639 /**
3740 *
@@ -165,6 +168,22 @@ public function displayOutputMessage($type, $buffer)
165168 }
166169 }
167170
171+ /**
172+ * run the php codes
173+ *
174+ * @return \SOS\MultiProcess\Classes\MultiProcess
175+ * @throws \Exception
176+ * @author karam mustafa
177+ */
178+ public function runPHP ()
179+ {
180+
181+ $ this ->phpProcess ($ this ->higherOrderRun ());
182+
183+ $ this ->resolveNotRunningProcess ($ this ->higherOrderRun ());
184+
185+ return $ this ;
186+ }
168187
169188 /**
170189 * this function will execute run function in symfony component
@@ -175,19 +194,9 @@ public function displayOutputMessage($type, $buffer)
175194 */
176195 public function run ()
177196 {
178- $ callback = function (Process $ process ) {
179- return $ process ->run (function ($ type , $ buffer ) {
197+ $ this ->process ($ this ->higherOrderRun ());
180198
181- // if we enable the output, then display this message depending on it type.
182- if ($ this ->getOptions ('enableOutput ' )) {
183- $ this ->displayOutputMessage ($ type , $ buffer );
184- }
185- });
186- };
187-
188- $ this ->process ($ callback );
189-
190- $ this ->resolveNotRunningProcess ($ callback );
199+ $ this ->resolveNotRunningProcess ($ this ->higherOrderRun ());
191200
192201 return $ this ;
193202 }
@@ -213,6 +222,29 @@ public function start()
213222
214223 }
215224
225+ /**
226+ * run the php codes from tasks, and each task must be a callback function.
227+ *
228+ * @param $callback
229+ *
230+ * @author karam mustafa
231+ */
232+ private function phpProcess ($ callback )
233+ {
234+ while ($ task = $ this ->checkIfCanProcess ()) {
235+
236+ $ process = new PhpProcess ("<?php {$ task [$ this ->getCommandKey ()]()} ?> " );
237+
238+ // Add the process to the processing property
239+ $ this ->processing [] = $ process ;
240+
241+ $ callback ($ process );
242+
243+ if (!$ process ->isSuccessful () && $ this ->getOptions ('throwIfTaskNotSuccess ' )) {
244+ throw new ProcessFailedException ($ process );
245+ }
246+ }
247+ }
216248
217249 /**
218250 * this function will set the require config to a symfony process component.
@@ -239,6 +271,11 @@ private function process($callback)
239271 // this callback could be a start or run function in symfony component
240272 // or might be any callback that accept Process parameter as a dependency.
241273 $ callback ($ process );
274+
275+ if (!$ process ->isSuccessful () && $ this ->getOptions ('throwIfTaskNotSuccess ' )) {
276+ throw new ProcessFailedException ($ process );
277+ }
278+
242279 }
243280 }
244281
@@ -338,4 +375,23 @@ private function checkIfCanProcess()
338375 : false ;
339376 }
340377
378+ /**
379+ * return a callback that execute run function inside process component.
380+ *
381+ * @return \Closure
382+ * @author karam mustafa
383+ */
384+ private function higherOrderRun ()
385+ {
386+ return function (Process $ process ) {
387+ return $ process ->run (function ($ type , $ buffer ) {
388+
389+ // if we enable the output, then display this message depending on it type.
390+ if ($ this ->getOptions ('enableOutput ' )) {
391+ $ this ->displayOutputMessage ($ type , $ buffer );
392+ }
393+ });
394+ };
395+ }
396+
341397}
0 commit comments