Logger-Service hinzugefügt und Docker-Compose-Option wait implementiert
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/*
|
||||
* DeploymentService.php 2026-03-23 thomas
|
||||
* DeploymentService.php 2026-03-27 thomas
|
||||
*
|
||||
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
|
||||
* 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']);
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
39
src/Service/Docker/Logger/LoggerService.php
Normal file
39
src/Service/Docker/Logger/LoggerService.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/*
|
||||
* LoggerService.php 2026-03-27 thomas
|
||||
*
|
||||
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user