loom-spinner-cli/src/Classes/File/DockerComposeFileBuilder.php

69 lines
1.9 KiB
PHP
Raw Normal View History

2025-04-13 13:11:35 +01:00
<?php
declare(strict_types=1);
namespace Loom\Spinner\Classes\File;
use Loom\Spinner\Classes\Config\Config;
use Loom\Utility\FilePath\FilePath;
2025-04-13 13:11:35 +01:00
use Symfony\Component\Console\Input\InputInterface;
class DockerComposeFileBuilder extends AbstractFileBuilder
{
/**
* @throws \Exception
*/
public function __construct(Config $config)
{
2025-04-13 13:35:39 +01:00
$projectDockerCompose = $config->getFilePaths()->get('projectDockerCompose');
2025-04-13 13:11:35 +01:00
if (!$projectDockerCompose instanceof FilePath) {
2025-04-13 13:11:35 +01:00
throw new \Exception('Project Docker Compose file path not found.');
}
return parent::__construct($projectDockerCompose, $config);
}
2025-04-13 20:07:48 +01:00
/**
* @throws \Exception
*/
2025-04-13 13:11:35 +01:00
public function build(InputInterface $input): DockerComposeFileBuilder
{
$this->content = file_get_contents(
$this->config->getFilePaths()->get('phpYamlTemplate')->getAbsolutePath()
);
if ($this->config->isDatabaseEnabled($input) && in_array($this->config->getDatabaseDriver($input), ['sqlite3', 'sqlite'])) {
$this->addSqliteDatabaseConfig();
}
2025-04-13 20:07:48 +01:00
if ($this->config->isServerEnabled($input)) {
$this->addNginxConfig();
}
2025-04-13 13:11:35 +01:00
return $this;
}
2025-04-13 20:07:48 +01:00
private function addNginxConfig(): void
{
$this->content .= str_replace(
'services:',
'',
file_get_contents(
$this->config->getFilePaths()->get('nginxYamlTemplate')->getAbsolutePath()
)
);
$this->content = str_replace(
'./nginx/conf.d',
(new FilePath('config/nginx/conf.d'))->getAbsolutePath(),
2025-04-13 20:07:48 +01:00
$this->content
);
}
private function addSqliteDatabaseConfig(): void
{
$sqlLiteConfig = file_get_contents((new FilePath('config/sqlite.yaml'))->getAbsolutePath());
$sqlLiteConfig = str_replace('volumes:', '', $sqlLiteConfig);
$this->content .= $sqlLiteConfig;
}
2025-04-13 13:11:35 +01:00
}