diff --git a/Processor/Launcher.php b/Processor/Launcher.php index 6cced77..b95375c 100644 --- a/Processor/Launcher.php +++ b/Processor/Launcher.php @@ -13,12 +13,12 @@ use Async\Processor\Process; use Async\Processor\ProcessorError; use Async\Processor\SerializableException; -use Async\Processor\ProcessInterface; +use Async\Processor\LauncherInterface; /** * Launcher runs a command/script/application/callable in an independent process. */ -class Launcher implements ProcessInterface +class Launcher implements LauncherInterface { protected $timeout = null; protected $process; @@ -31,6 +31,7 @@ class Launcher implements ProcessInterface protected $realTimeOutput; protected $startTime; + protected $showOutput = false; protected $successCallbacks = []; protected $errorCallbacks = []; @@ -54,7 +55,7 @@ public function start(): self $this->startTime = \microtime(true); $this->process->start(function ($type, $buffer) { - $this->realTimeOutput .= $buffer; + $this->realTimeOutput = $buffer; }); $this->pid = $this->process->getPid(); @@ -144,7 +145,19 @@ public function isTimedOut(): bool public function isRunning(): bool { - return $this->process->isRunning(); + $isRunning = $this->process->isRunning(); + if ($isRunning && $this->showOutput) { + echo $this->getRealOutput(); + } + + return $isRunning; + } + + public function showOutput(): self + { + $this->showOutput = true; + + return $this; } public function isSuccessful(): bool diff --git a/Processor/ProcessInterface.php b/Processor/LauncherInterface.php similarity index 88% rename from Processor/ProcessInterface.php rename to Processor/LauncherInterface.php index f5c2c1c..42c7c17 100644 --- a/Processor/ProcessInterface.php +++ b/Processor/LauncherInterface.php @@ -4,7 +4,7 @@ namespace Async\Processor; -interface ProcessInterface +interface LauncherInterface { /** * Gets PHP's process ID @@ -16,14 +16,14 @@ public function getId(): int; /** * Start the process * - * @return ProcessInterface + * @return LauncherInterface */ public function start(); /** * Restart the process * - * @return ProcessInterface + * @return LauncherInterface */ public function restart(); @@ -57,7 +57,7 @@ public function wait($waitTimer = 1000, bool $useYield = false); * @param callable $failCallback * @param callable $progressCallback * - * @return ProcessInterface + * @return LauncherInterface */ public function then(callable $doneCallback, callable $failCallback = null, callable $progressCallback = null); @@ -66,7 +66,7 @@ public function then(callable $doneCallback, callable $failCallback = null, call * * @param callable $callback * - * @return ProcessInterface + * @return LauncherInterface */ public function done(callable $callback); @@ -75,7 +75,7 @@ public function done(callable $callback); * * @param callable $progressCallback * - * @return ProcessInterface + * @return LauncherInterface */ public function progress(callable $progressCallback); @@ -84,7 +84,7 @@ public function progress(callable $progressCallback); * * @param mixed $update * - * @return ProcessInterface + * @return LauncherInterface */ public function triggerOutput($update = null); @@ -93,7 +93,7 @@ public function triggerOutput($update = null); * * @param callable $callback * - * @return ProcessInterface + * @return LauncherInterface */ public function catch(callable $callback); @@ -102,7 +102,7 @@ public function catch(callable $callback); * * @param callable $callback * - * @return ProcessInterface + * @return LauncherInterface */ public function timeout(callable $callback); @@ -136,7 +136,7 @@ public function getPid(): ?int; /** * Stops the running process. * - * @return ProcessInterface + * @return LauncherInterface */ public function stop(); @@ -167,4 +167,11 @@ public function isTerminated(): bool; * @return bool true if the process ended successfully, false otherwise */ public function isSuccessful(): bool; + + /** + * Set process to display output of parent process. + * + * @return LauncherInterface + */ + public function showOutput(); } diff --git a/Processor/Processor.php b/Processor/Processor.php index cb7e8cb..c4df60d 100644 --- a/Processor/Processor.php +++ b/Processor/Processor.php @@ -7,7 +7,7 @@ use Closure; use Async\Processor\Launcher; use Async\Processor\Process; -use Async\Processor\ProcessInterface; +use Async\Processor\LauncherInterface; use Opis\Closure\SerializableClosure; class Processor @@ -62,9 +62,9 @@ public static function init(string $autoload = null) * * @param mixed $task * - * @return ProcessInterface + * @return LauncherInterface */ - public static function create($task, int $timeout = 300, $input = null): ProcessInterface + public static function create($task, int $timeout = 300, $input = null): LauncherInterface { if (!self::$isInitialized) { self::init(); @@ -99,9 +99,9 @@ public static function phpPath(string $executable): void * * @param string $task daemon * - * @return ProcessInterface + * @return LauncherInterface */ - public static function daemon($task, $channel = null): ProcessInterface + public static function daemon($task, $channel = null): LauncherInterface { if (\is_string($task)) { $shadow = (('\\' === \DIRECTORY_SEPARATOR) ? 'start /b ' : 'nohup ') . $task; diff --git a/Processor/functions.php b/Processor/functions.php index 9df9083..d47d262 100644 --- a/Processor/functions.php +++ b/Processor/functions.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Async\Processor\Processor; -use Async\Processor\ProcessInterface; +use Async\Processor\LauncherInterface; if (!\function_exists('spawn')) { /** @@ -13,14 +13,14 @@ * @param int $timeout * @param mixed $processChannel * - * @return ProcessInterface + * @return LauncherInterface */ - function spawn($shellCallable, int $timeout = 300, $processChannel = null): ProcessInterface + function spawn($shellCallable, int $timeout = 300, $processChannel = null): LauncherInterface { return Processor::create($shellCallable, $timeout, $processChannel); } - function await_spawn(ProcessInterface $process) + function await_spawn(LauncherInterface $process) { return $process->run(); } diff --git a/composer.json b/composer.json index abd435d..0cd9d30 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ } ], "require": { - "php": ">7.1", + "php": ">7.2", "opis/closure": "^3.5.1", - "symfony/process": "^5.0.0" + "symfony/process": "^5.0.4" }, "autoload": { "files": [ diff --git a/tests/ProcessorTest.php b/tests/ProcessorTest.php index 770d08a..16bc9d9 100644 --- a/tests/ProcessorTest.php +++ b/tests/ProcessorTest.php @@ -98,6 +98,16 @@ public function testStart() $this->assertTrue($process->isTerminated()); } + public function testLiveOutput() + { + $process = Processor::create(function () { + echo 'hello child'; + }); + $this->expectOutputRegex('/hello child/'); + $process->showOutput()->start();; + $process->wait(); + } + public function testGetOutputShell() { if ('\\' === \DIRECTORY_SEPARATOR) {