From 02ed3ed4cd0a8c64c372c4e1453a36a7a5083d5e Mon Sep 17 00:00:00 2001 From: techno-express Date: Sat, 25 Apr 2020 20:54:49 -0400 Subject: [PATCH] Reduced required PHP version to 7.1 for possible future `proc_open` removal, add shortcut functions This package is no longer `proc_open` focused as such **symfony/process** changes will not be tracked. Any bug fixes for the fallback methods based on `proc_open` will be addressed within this package. - add function to handle base64 being returned by child process - add function to return Channeled objects --- README.md | 4 ++-- Spawn/Core.php | 26 ++++++++++++++++++++++++++ composer.json | 4 ++-- tests/ChanneledTest.php | 2 +- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2b1561f..f6d3d3f 100644 --- a/README.md +++ b/README.md @@ -93,10 +93,10 @@ $process->displayOn()->run(); ```php include 'vendor/autoload.php'; -use Async\Spawn\Channeled; use Async\Spawn\ChanneledInterface; -$ipc = new Channeled(); +// return a new `Channeled` instance. +$ipc = \spawn_channel();; $process = spawn(function (ChanneledInterface $channel) { // Setup the channel resources if needed, defaults to `STDIN`, `STDOUT`, and `STDERR`. diff --git a/Spawn/Core.php b/Spawn/Core.php index c468f90..43dca99 100644 --- a/Spawn/Core.php +++ b/Spawn/Core.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Async\Spawn\Channeled; +use Async\Spawn\ChanneledInterface; use Async\Spawn\Spawn; use Async\Spawn\LauncherInterface; @@ -233,6 +234,16 @@ function spawn_result(LauncherInterface $process) return $process->getResult(); } + /** + * return a new **Spawn** based `Channel` instance. + * + * @return ChanneledInterface + */ + function spawn_channel(): ChanneledInterface + { + return new Channeled; + } + /** * Check if a string is base64 valid, or has `encoded` mixed data. * @@ -266,6 +277,21 @@ function is_base64($input): ?bool return false; } + /** + * Check if base64 valid, if so decodes and creates a `PHP` value from the + * **serialized** decoded data representation. + * + * @param string $input + * + * @return mixed + * + * @codeCoverageIgnore + */ + function deserialize($input) + { + return \is_base64($input) ? \unserialize(\base64_decode($input)) : $input; + } + /** * For use when/before calling the actual `return` keyword, will write some `INVALID` data to standard * output and flush, then sleep for `microsecond`, and return the to be encoded `data/result`. diff --git a/composer.json b/composer.json index 4c3cbff..397dddd 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ } ], "require": { - "php": ">7.2", + "php": ">7.1", "opis/closure": "^3.5.1", - "symfony/process": "^5.0.7" + "symfony/process": ">4.0.0" }, "autoload": { "files": [ diff --git a/tests/ChanneledTest.php b/tests/ChanneledTest.php index ceab3e4..0529d6f 100644 --- a/tests/ChanneledTest.php +++ b/tests/ChanneledTest.php @@ -16,7 +16,7 @@ protected function setUp(): void public function testSimpleChanneled() { - $ipc = new Channeled(); + $ipc = \spawn_channel(); $process = \spawn(function (ChanneledInterface $channel) { $channel->write('ping');