From 7741123e897cba170d372e96577eae793e0745a5 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 bae9bfd6..bfe38f1c 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); @@ -444,19 +442,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 831678cc..051c5b57 100644 --- a/php/src/Docker/DockerActionManager.php +++ b/php/src/Docker/DockerActionManager.php @@ -193,7 +193,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) { @@ -550,7 +550,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();