From daaa8469026902d0cff6f90c6cbf16b9f06dc610 Mon Sep 17 00:00:00 2001 From: Thomas Schneider Date: Fri, 27 Mar 2026 16:07:38 +0100 Subject: [PATCH] =?UTF-8?q?Logger-Service=20hinzugef=C3=BCgt=20und=20Docke?= =?UTF-8?q?r-Compose-Option=20`wait`=20implementiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Service/Deployment/DeploymentService.php | 4 +- src/Service/Docker/DockerCompose.php | 45 +++++++++++++++++++- src/Service/Docker/Logger/LoggerService.php | 39 +++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/Service/Docker/Logger/LoggerService.php diff --git a/src/Service/Deployment/DeploymentService.php b/src/Service/Deployment/DeploymentService.php index ab4d35e..854767c 100644 --- a/src/Service/Deployment/DeploymentService.php +++ b/src/Service/Deployment/DeploymentService.php @@ -1,6 +1,6 @@ * Alle Rechte vorbehalten. @@ -41,7 +41,7 @@ abstract class DeploymentService implements DeploymentInterface ->setName($project->name) ->setWorkingDir($project->projectDir) ->setComposeFiles([$this->filesDir . '/compose.yaml', $this->filesDir . '/compose.prod.yaml']) - ->daemonize(true) + ->wait(true) ->up(['--build']); } diff --git a/src/Service/Docker/DockerCompose.php b/src/Service/Docker/DockerCompose.php index 4f4c64b..ccb1e94 100644 --- a/src/Service/Docker/DockerCompose.php +++ b/src/Service/Docker/DockerCompose.php @@ -8,6 +8,7 @@ namespace App\Service\Docker; +use App\Service\Docker\Logger\LoggerService; use App\Service\Process\CleanProcess; use Dotenv\Dotenv; use Exception; @@ -24,6 +25,8 @@ class DockerCompose protected bool $daemonize = false; + protected bool $wait = false; + protected string $dockerHost = ''; protected string $workingDir = ''; @@ -41,10 +44,14 @@ class DockerCompose */ public function up(array $optionalArgs = []): void { + $log = new LoggerService($this->workingDir); + $log->clearLog(); + $log->log('Starting Docker Compose up command at ' . date('Y-m-d H:i:s') . ' with optional arguments: ' . implode(', ', $this->getExtraOptions($optionalArgs))); + $process = $this->process('up', $optionalArgs); - $process->run(function ($type, $buffer): void + $process->run(function ($type, $buffer) use ($log): void { - echo $buffer; + $log->log($buffer); }); // if(!$process->isSuccessful()) @@ -161,6 +168,24 @@ class DockerCompose $extraOptions[] = '-d'; } + if($this->wait) + { + if(($key = array_search('-d', $extraOptions, true)) !== false) + { + unset($extraOptions[$key]); + } + + if(($key = array_search('--detach', $extraOptions, true)) !== false) + { + unset($extraOptions[$key]); + } + + if(!in_array('--wait', $extraOptions, true)) + { + $extraOptions[] = '--wait'; + } + } + return $extraOptions; } @@ -248,6 +273,22 @@ class DockerCompose } + /** + * Sets the wait state for the current instance. + * + * This method allows enabling or disabling the wait behavior by updating + * the internal wait property. It returns the instance to allow method chaining. + * + * @param bool $wait The value to set for the wait property. + * @return self The current instance with the updated wait state. + */ + public function wait(bool $wait): self + { + $this->wait = $wait; + return $this; + } + + /** * Sets and merges Docker Compose files. * diff --git a/src/Service/Docker/Logger/LoggerService.php b/src/Service/Docker/Logger/LoggerService.php new file mode 100644 index 0000000..ab55b14 --- /dev/null +++ b/src/Service/Docker/Logger/LoggerService.php @@ -0,0 +1,39 @@ + + * Alle Rechte vorbehalten. + */ + +namespace App\Service\Docker\Logger; + +use Symfony\Component\Filesystem\Filesystem; + +class LoggerService +{ + const string LOG_FILE = 'docker.log'; + + + public function __construct( + protected string $logDir, + ){} + + + public function log(string $message): void + { + new Filesystem()->appendToFile($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE, $message . PHP_EOL); + } + + + public function clearLog(): void + { + new Filesystem()->remove($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE); + } + + + public function getLog(): string + { + return new Filesystem()->readFile($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE); + } +}