Skip to content

Commit 7ff8074

Browse files
committed
updates, simple functions for base64 un/serializing of return values, only when result
- `triggerSuccess` should handle actual returned values, no last output - keep `libuv` doc/stubs in sync with [WIP] PR amphp/ext-uv#77
1 parent 02ed3ed commit 7ff8074

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Spawn/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636

3737
$output = $task($channel);
3838

39-
\fwrite(\STDOUT, \base64_encode(\serialize($output)));
39+
\fwrite(\STDOUT, \serializer($output));
4040

4141
exit(0);
4242
} catch (\Throwable $exception) {
4343
$output = new SerializableException($exception);
4444

45-
\fwrite(\STDERR, \base64_encode(\serialize($output)));
45+
\fwrite(\STDERR, \serializer($output));
4646

4747
exit(1);
4848
}

Spawn/Core.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,32 @@ function is_base64($input): ?bool
277277
return false;
278278
}
279279

280+
/**
281+
* Create a encoded base64 valid string from a **serializable** data value.
282+
*
283+
* @param mixed $input
284+
*
285+
* @return string
286+
*
287+
* @codeCoverageIgnore
288+
*/
289+
function serializer($input)
290+
{
291+
return \base64_encode(\serialize($input));
292+
}
293+
294+
/**
295+
* Decodes and creates a `PHP` value from the **serialized** data.
296+
*
297+
* @param string $input
298+
*
299+
* @return mixed
300+
*/
301+
function deserializer($input)
302+
{
303+
return @\unserialize(\base64_decode($input));
304+
}
305+
280306
/**
281307
* Check if base64 valid, if so decodes and creates a `PHP` value from the
282308
* **serialized** decoded data representation.
@@ -289,7 +315,7 @@ function is_base64($input): ?bool
289315
*/
290316
function deserialize($input)
291317
{
292-
return \is_base64($input) ? \unserialize(\base64_decode($input)) : $input;
318+
return \is_base64($input) ? \deserializer($input) : $input;
293319
}
294320

295321
/**

Spawn/Launcher.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public function clean($output = null)
472472
protected function decode($output, $errorSet = false)
473473
{
474474
if (\is_string($output)) {
475-
$realOutput = @\unserialize(\base64_decode($output));
475+
$realOutput = \deserializer($output);
476476
if (!$realOutput) {
477477
$realOutput = $output;
478478
if ($errorSet) {
@@ -483,7 +483,6 @@ protected function decode($output, $errorSet = false)
483483
return $realOutput;
484484
}
485485

486-
487486
return $output;
488487
}
489488

@@ -665,28 +664,27 @@ public function triggerSuccess(bool $isYield = false)
665664
{
666665
$this->status = true;
667666

667+
$result = null;
668668
if ($this->getResult() && !$this->getErrorOutput()) {
669-
$output = $this->lastResult;
669+
$result = $this->lastResult;
670670
} elseif ($this->getErrorOutput()) {
671671
return $this->triggerError($isYield);
672-
} else {
673-
$output = $this->getOutput();
674672
}
675673

676-
if (\is_base64($output) !== false) {
674+
if (\is_base64($result) !== false) {
677675
// @codeCoverageIgnoreStart
678-
$this->rawLastResult = $this->clean($output);
679-
$output = $this->decoded($this->rawLastResult);
676+
$this->rawLastResult = $this->clean($result);
677+
$result = $this->decoded($this->rawLastResult);
680678
// @codeCoverageIgnoreEnd
681679
}
682680

683681
if ($isYield)
684-
return $this->yieldSuccess($output);
682+
return $this->yieldSuccess($result);
685683

686684
foreach ($this->successCallbacks as $callback)
687-
$callback($output);
685+
$callback($result);
688686

689-
return $output;
687+
return $result;
690688
}
691689

692690
public function triggerError(bool $isYield = false)

Spawn/UVFunctions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ function uv_queue_work(UVLoop $loop, callable $callback, callable $after_callbac
667667
/**
668668
* Initialize the `UVIdle` handle watcher.
669669
* Idle watchers get invoked every loop iteration.
670+
* This function always succeeds.
670671
*
671672
* @param UVLoop $loop uv_loop handle.
672673
*
@@ -678,6 +679,7 @@ function uv_idle_init(UVLoop $loop = null)
678679

679680
/**
680681
* Start the Idle handle with the given callback.
682+
* This function always succeeds, except when `callback` is `NULL`.
681683
*
682684
* The callbacks of idle handles are invoked once per event loop.
683685
*
@@ -702,6 +704,7 @@ function uv_idle_start(UVIdle $idle, callable $callback)
702704

703705
/**
704706
* Stop the Idle handle, the callback will no longer be called.
707+
* This function always succeeds.
705708
*
706709
* @param UVIdle $idle uv_idle handle.
707710
*/
@@ -711,6 +714,7 @@ function uv_idle_stop(UVIdle $idle)
711714

712715
/**
713716
* Initialize the `UVPrepare` handle watcher.
717+
* This function always succeeds.
714718
* Prepare watchers get invoked before polling for I/O events.
715719
*
716720
* Their main purpose is to integrate other event mechanisms into `libuv` and their
@@ -727,6 +731,7 @@ function uv_prepare_init(UVLoop $loop = null)
727731

728732
/**
729733
* Start the Prepare handle with the given callback.
734+
* This function always succeeds, except when `callback` is `NULL`.
730735
*
731736
* @param UVPrepare $handle UV handle (prepare)
732737
* @param callable $callback expects (UVPrepare $prepare, int $status).
@@ -737,6 +742,7 @@ function uv_prepare_start(UVPrepare $handle, callable $callback)
737742

738743
/**
739744
* Stop the Prepare handle, the callback will no longer be called.
745+
* This function always succeeds.
740746
*
741747
* @param UVPrepare $handle UV handle (prepare).
742748
*/
@@ -746,6 +752,7 @@ function uv_prepare_stop(UVPrepare $handle)
746752

747753
/**
748754
* Initialize the `UVCheck` handle watcher.
755+
* This function always succeeds.
749756
* Check watchers get invoked after polling for I/O events.
750757
*
751758
* Their main purpose is to integrate other event mechanisms into `libuv` and their
@@ -762,6 +769,7 @@ function uv_check_init(UVLoop $loop = null)
762769

763770
/**
764771
* Start the Check handle with the given callback.
772+
* This function always succeeds, except when `callback` is `NULL`.
765773
*
766774
* @param UVCheck $handle UV handle (check).
767775
* @param callable $callback expects (UVCheck $check, int $status).
@@ -772,6 +780,7 @@ function uv_check_start(UVCheck $handle, callable $callback)
772780

773781
/**
774782
* Stop the Check handle, the callback will no longer be called.
783+
* This function always succeeds.
775784
*
776785
* @param UVCheck $handle UV handle (check).
777786
*/

0 commit comments

Comments
 (0)