From 3c9bda0738c86a766a0b90334f47eece0bdabfb1 Mon Sep 17 00:00:00 2001 From: Volodymyr Shandak Date: Wed, 30 Jun 2021 18:57:44 +0300 Subject: [PATCH 1/2] Fix output buffering issue --- lib/functions.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/functions.php b/lib/functions.php index 9945bc4b..cbab5745 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -25,7 +25,7 @@ function await(Promise|array $promise): mixed $promise = Promise\all($promise); } - $fiber = \Fiber::this(); + $fiber = \Fiber::getCurrent(); $resolved = false; if ($fiber) { // Awaiting from within a fiber. @@ -33,7 +33,15 @@ function await(Promise|array $promise): mixed throw new \Error(\sprintf('Cannot call %s() within an event loop callback', __FUNCTION__)); } - $promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved, $fiber): void { + $hash = spl_object_hash($fiber); + $level = ob_get_level(); + + if ($level) + { + Loop::setState($hash . '-key', $level); + } + + $promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved, $fiber, $hash): void { $resolved = true; if ($exception) { @@ -45,8 +53,32 @@ function await(Promise|array $promise): mixed }); try { + if (Loop::getState($hash . '-key')) + { + $content = ob_get_contents(); + + if ($content !== '') + { + Loop::setState($hash . '-content', $content); + } + + ob_end_clean(); + } + // Suspend the current fiber until the promise is resolved. $value = \Fiber::suspend(); + + if (Loop::getState($hash . '-key')) + { + ob_start(); + $content = Loop::getState($hash . '-content'); + + if (!is_null($content)) + { + echo $content; + Loop::setState($hash . '-content', null); + } + } } finally { if (!$resolved) { // $resolved should only be false if the fiber was manually resumed outside of the callback above. From b7a4f9d1dd6d38df8ac52ce94864c850b44d6ce1 Mon Sep 17 00:00:00 2001 From: Volodymyr Shandak Date: Wed, 30 Jun 2021 19:01:45 +0300 Subject: [PATCH 2/2] formatting code --- lib/functions.php | 48 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/functions.php b/lib/functions.php index cbab5745..7725b762 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -33,13 +33,13 @@ function await(Promise|array $promise): mixed throw new \Error(\sprintf('Cannot call %s() within an event loop callback', __FUNCTION__)); } - $hash = spl_object_hash($fiber); + $hash = spl_object_hash($fiber); $level = ob_get_level(); if ($level) - { - Loop::setState($hash . '-key', $level); - } + { + Loop::setState($hash . '-key', $level); + } $promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved, $fiber, $hash): void { $resolved = true; @@ -53,32 +53,32 @@ function await(Promise|array $promise): mixed }); try { - if (Loop::getState($hash . '-key')) - { - $content = ob_get_contents(); + if (Loop::getState($hash . '-key')) + { + $content = ob_get_contents(); - if ($content !== '') - { - Loop::setState($hash . '-content', $content); - } + if ($content !== '') + { + Loop::setState($hash . '-content', $content); + } - ob_end_clean(); - } + ob_end_clean(); + } // Suspend the current fiber until the promise is resolved. $value = \Fiber::suspend(); - if (Loop::getState($hash . '-key')) - { - ob_start(); - $content = Loop::getState($hash . '-content'); - - if (!is_null($content)) - { - echo $content; - Loop::setState($hash . '-content', null); - } - } + if (Loop::getState($hash . '-key')) + { + ob_start(); + $content = Loop::getState($hash . '-content'); + + if (!is_null($content)) + { + echo $content; + Loop::setState($hash . '-content', null); + } + } } finally { if (!$resolved) { // $resolved should only be false if the fiber was manually resumed outside of the callback above.