From af6016b17d1274e988c1699de876ac3091f42edb Mon Sep 17 00:00:00 2001 From: Pablo Zmdl Date: Thu, 28 May 2026 12:14:54 +0200 Subject: [PATCH] Refactor creating and using addToStreamingResponseBody() This way we stick to having one implementation of the function, not three. Signed-off-by: Pablo Zmdl --- php/src/Controller/DockerController.php | 21 ++++++++------------- php/src/Docker/DockerActionManager.php | 4 ++-- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/php/src/Controller/DockerController.php b/php/src/Controller/DockerController.php index 53546fef..a49ba3a6 100644 --- a/php/src/Controller/DockerController.php +++ b/php/src/Controller/DockerController.php @@ -299,7 +299,7 @@ readonly class DockerController { } if ($addToStreamingResponseBody !== null) { - $addToStreamingResponseBody($container, "Stopping container"); + $addToStreamingResponseBody("Stopping container", $container); } // Stop itself first and then all the dependencies @@ -339,7 +339,7 @@ readonly class DockerController { // Get streaming response start and closure $nonbufResp = $this->startStreamingResponse($response); - $addToStreamingResponseBody = $this->getPlainStreamingCallback($nonbufResp); + $addToStreamingResponseBody = $this->getAddToStreamingResponseBody($nonbufResp); $this->dockerActionManager->RunNextcloudUpgradeToLatestMajor($addToStreamingResponseBody); @@ -353,9 +353,7 @@ readonly class DockerController { $nonbufResp = $this->startStreamingResponse($response); $body = $nonbufResp->getBody(); - $addToStreamingResponseBody = function (string $message) use ($body) : void { - $body->write("
$message
"); - }; + $addToStreamingResponseBody = $this->getAddToStreamingResponseBody($nonbufResp); $this->dockerActionManager->SystemPrune($addToStreamingResponseBody); @@ -445,19 +443,16 @@ readonly class DockerController { // Create a closure to pass around to the code, which should to the logging (because it e.g. decides // if it'll actually pull an image), but which should not need to know anything about the // wanted markup or formatting. - $addToStreamingResponseBody = function (Container $container, string $message) use ($nonbufResp) : void { - $nonbufResp->getBody()->write("
" . htmlspecialchars("{$container->displayName}: {$message}", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . "
"); + $addToStreamingResponseBody = function (string $message, ?Container $container = null) use ($nonbufResp) : void { + if ($container) { + $message = "{$container->displayName}: {$message}"; + } + $nonbufResp->getBody()->write("
" . htmlspecialchars("{$message}", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . "
"); }; return $addToStreamingResponseBody; } - private function getPlainStreamingCallback(Response $nonbufResp) : \Closure { - return function (string $message) use ($nonbufResp) : void { - $nonbufResp->getBody()->write("
" . htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . "
"); - }; - } - private function finalizeStreamingResponse(Response $nonbufResp) : void { $nonbufResp->getBody()->write($this->getStreamingResponseHtmlEnd()); } diff --git a/php/src/Docker/DockerActionManager.php b/php/src/Docker/DockerActionManager.php index 529208cf..85fa45f6 100644 --- a/php/src/Docker/DockerActionManager.php +++ b/php/src/Docker/DockerActionManager.php @@ -194,7 +194,7 @@ readonly class DockerActionManager { $url = $this->BuildApiUrl(sprintf('containers/%s/start', urlencode($container->identifier))); try { if ($addToStreamingResponseBody !== null) { - $addToStreamingResponseBody($container, "Starting container"); + $addToStreamingResponseBody("Starting container", $container); } $this->guzzleClient->post($url); } catch (RequestException $e) { @@ -551,7 +551,7 @@ readonly class DockerActionManager { $imageIsThere = true; try { if ($addToStreamingResponseBody) { - $addToStreamingResponseBody($container, "Pulling image"); + $addToStreamingResponseBody("Pulling image", $container); } $imageUrl = $this->BuildApiUrl(sprintf('images/%s/json', $encodedImageName)); $this->guzzleClient->get($imageUrl)->getBody()->getContents();