From 83b3dc83ded1ee789ab34d65837eb51353ee5cbc Mon Sep 17 00:00:00 2001 From: techno-express Date: Tue, 13 Apr 2021 12:04:46 -0400 Subject: [PATCH] bc - rename/reorder `return_in` to `flush_value`, corrections, insure child output only happen for `stdout` not return values/data --- README.md | 8 ++++---- Spawn/Core.php | 11 +++++------ Spawn/Launcher.php | 11 +++++------ Spawn/Spawn.php | 2 +- examples/progress.php | 2 +- examples/signal.php | 2 +- tests/ChanneledFallbackTest.php | 16 ++++++++-------- tests/ChanneledTest.php | 2 +- 8 files changed, 26 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a50f5d3..24c582c 100644 --- a/README.md +++ b/README.md @@ -108,10 +108,10 @@ $process = spawn(function (ChanneledInterface $channel) { echo $channel->read(); // same as echo fgets(STDIN); echo $channel->read(); - // The `return_in` is needed otherwise last output will be mixed in with the encoded return data. + // The `flush_value` is needed otherwise last output will be mixed in with the encoded return data. // Or some other processing could be done instead to make this unnecessary. // All returned `data/results` are encoded, then decode by the parent. - return \return_in(50, 'return whatever'); // same as echo '___uv_spawn___'; usleep(50); return 'return whatever'; + return \flush_value('return whatever', 50); // same as echo '___uv_spawn___'; usleep(50); return 'return whatever'; }, 0, $ipc) ->progress(function ($type, $data) use ($ipc) { if ('ping' === $data) { @@ -151,9 +151,9 @@ $process = Spawn::create(function () { ///////////////////////////////////////////////////////////// // This following statement is needed or some other processing performed before returning data. - \return_in($delay, $with); // will print '___uv_spawn___' and sleep for 50 microseconds with data; + return \flush_value($with, $delay); // will print '___uv_spawn___' and sleep for 50 microseconds with data; ///////////////////////////////////////////////////////////// - + // Or Just return `result`; // `result` will be encoded, then decoded by parent. }, int $timeout = 0 , $input = null) ->then(function ($result) { diff --git a/Spawn/Core.php b/Spawn/Core.php index 16799a7..555d2f3 100644 --- a/Spawn/Core.php +++ b/Spawn/Core.php @@ -391,7 +391,7 @@ function is_base64($input): ?bool */ function serializer($input) { - return \base64_encode(\serialize($input)); + return \base64_encode(@\serialize($input)); } /** @@ -439,23 +439,22 @@ function deserialize($input) * - The parent is given no time to read data stream before the `return`, there was no * delay or processing preformed between child last output and the `return` statement. * + * @param mixed $with to return to parent process. * @param int $microsecond - `50` when using `uv_spawn`, otherwise `1500` or so higher with `proc_open`. * - * @param mixed $withData to return to parent process. - * * @return void|mixed * * @codeCoverageIgnore */ - function return_in(int $microsecond = 50, $withData = null) + function flush_value($with = null, int $microsecond = 50) { \fwrite(\STDOUT, LauncherInterface::INVALID[1]); \fflush(\STDOUT); \usleep($microsecond); \fflush(\STDOUT); - if (!\is_null($withData)) - return $withData; + if (!\is_null($with)) + return $with; } /** diff --git a/Spawn/Launcher.php b/Spawn/Launcher.php index 571e8fe..2169d61 100644 --- a/Spawn/Launcher.php +++ b/Spawn/Launcher.php @@ -457,16 +457,15 @@ public function displayOff(): LauncherInterface */ protected function display($buffer = null) { + $output = $this->decoded($buffer); if ($this->showOutput) { - if (\IS_CLI) { - $output = $this->decoded($buffer); - } else { + if (\is_string($output)) { // @codeCoverageIgnoreStart - $output = \htmlspecialchars((string) $this->decoded($buffer), ENT_COMPAT, 'UTF-8'); + if (!\IS_CLI) + $output = \htmlspecialchars($output, \ENT_COMPAT, 'UTF-8'); // @codeCoverageIgnoreEnd + \printf('%s', $output); } - - \printf('%s', $output); } } diff --git a/Spawn/Spawn.php b/Spawn/Spawn.php index fce71da..5ddd3f7 100644 --- a/Spawn/Spawn.php +++ b/Spawn/Spawn.php @@ -107,7 +107,7 @@ public static function create( $useYield = ($isYield === null) ? self::$isYield : $isYield; - if (\function_exists('uv_default_loop') && self::$useUv) { + if (\IS_UV && self::$useUv) { return Launcher::add( $task, (int) self::getId(), diff --git a/examples/progress.php b/examples/progress.php index d9147ec..85e4092 100644 --- a/examples/progress.php +++ b/examples/progress.php @@ -14,7 +14,7 @@ function (ChanneledInterface $channel) { $channel->write('ping'); echo $channel->read(); echo $channel->read(); - return \return_in(50, 'The game!'); + return \flush_value('The game!'); } )->progress(function ($type, $data) use ($ipc) { if ('ping' === $data) { diff --git a/examples/signal.php b/examples/signal.php index 3a4d4b2..dd4d78d 100644 --- a/examples/signal.php +++ b/examples/signal.php @@ -14,7 +14,7 @@ function (ChanneledInterface $channel) { $channel->write('ping'); echo $channel->read(); echo $channel->read(); - return \return_in(50, 'The game!'); + return \flush_value('The game!'); } )->signal(\SIGKILL, function ($signal) { echo "the process has been terminated with 'SIGKILL - " . $signal . "' signal!" . \PHP_EOL; diff --git a/tests/ChanneledFallbackTest.php b/tests/ChanneledFallbackTest.php index aef25d7..d7eec91 100644 --- a/tests/ChanneledFallbackTest.php +++ b/tests/ChanneledFallbackTest.php @@ -9,7 +9,7 @@ class ChanneledFallbackTest extends TestCase { - protected function setUp(): void + protected function setUp(): void { Spawn::setup(null, false, false, false); } @@ -22,7 +22,7 @@ public function testSimpleChanneled() $channel->write('ping'); echo $channel->read(); echo $channel->read(); - return \return_in(1500, 9); + return \flush_value(9, 1500); }, 10, $ipc) ->progress( function ($type, $data) use ($ipc) { @@ -55,7 +55,7 @@ public function testSimpleChanneledError() function ($type, $data) use ($ipc) { if ('ping' === $data) { $ipc->close() - ->send('pang' . \PHP_EOL); + ->send('pang' . \PHP_EOL); } } ); @@ -232,11 +232,11 @@ public function testLiveStreamAsInput() $p = spawn(function (ChanneledInterface $ipc) { $ipc->passthru(); }, 10, $stream) - ->progress(function ($type, $data) use ($stream) { - if ('hello' === $data) { - fclose($stream); - } - }); + ->progress(function ($type, $data) use ($stream) { + if ('hello' === $data) { + fclose($stream); + } + }); $p->run(); diff --git a/tests/ChanneledTest.php b/tests/ChanneledTest.php index 7003e39..3792f2e 100644 --- a/tests/ChanneledTest.php +++ b/tests/ChanneledTest.php @@ -25,7 +25,7 @@ public function testSimpleChanneled() echo $channel->read(); echo $channel->read(); - return \return_in(50, 9); + return \flush_value(9); }, 6) ->progress( function ($type, $data) use ($ipc) {