2025-04-13 10:43:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Loom\Spinner\Classes\Config;
|
|
|
|
|
|
|
|
use Loom\Spinner\Classes\Collection\FilePathCollection;
|
2025-04-13 12:59:44 +01:00
|
|
|
use Loom\Spinner\Classes\File\Interface\DataPathInterface;
|
|
|
|
use Loom\Spinner\Classes\File\SpinnerFilePath;
|
|
|
|
use Loom\Utility\FilePath\FilePath;
|
2025-04-13 10:43:30 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
class Config
|
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
private FilePathCollection $filePaths;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->setFilePaths();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilePaths(): FilePathCollection
|
|
|
|
{
|
|
|
|
return $this->filePaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilePath(string $key): ?FilePath
|
|
|
|
{
|
|
|
|
return $this->filePaths->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addFilePath(FilePath $filePath, string $key): void
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->filePaths->add($filePath, $key);
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 12:59:44 +01:00
|
|
|
public function getPhpVersion(InputInterface $input): ?float
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
if ($input->getOption('php')) {
|
|
|
|
return (float) $input->getOption('php');
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
return (float) $this->getEnvironmentOption('php', 'version');
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 12:59:44 +01:00
|
|
|
public function isNodeEnabled(InputInterface $input): bool
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 20:07:48 +01:00
|
|
|
if ($input->getOption('disable-node')) {
|
2025-04-13 12:59:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getEnvironmentOption('node', 'enabled');
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 20:07:48 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function isServerEnabled(InputInterface $input): bool
|
|
|
|
{
|
|
|
|
if ($input->getOption('disable-server')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getEnvironmentOption('server', 'enabled');
|
|
|
|
}
|
|
|
|
|
2025-04-13 23:33:55 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function isXDebugEnabled(InputInterface $input): bool
|
|
|
|
{
|
|
|
|
if ($input->getOption('disable-xdebug')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getEnvironmentOption('php', 'xdebug');
|
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 12:59:44 +01:00
|
|
|
public function getNodeVersion(InputInterface $input): ?int
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
if ($input->getOption('node')) {
|
|
|
|
return (int) $input->getOption('node');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $this->getEnvironmentOption('node','version');
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 12:59:44 +01:00
|
|
|
public function getEnvironmentOption(string $service, string $option): mixed
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
$projectCustomConfig = $this->filePaths->get('projectCustomConfig');
|
|
|
|
|
|
|
|
if ($projectCustomConfig->exists()) {
|
|
|
|
$customConfig = Yaml::parseFile($projectCustomConfig->getAbsolutePath())['options']['environment']
|
|
|
|
?? null;
|
|
|
|
|
|
|
|
if ($customConfig) {
|
|
|
|
if (isset($customConfig[$service][$option])) {
|
|
|
|
return $customConfig[$service][$option];
|
|
|
|
}
|
|
|
|
}
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
return $this->getDefaultConfig()['environment'][$service][$option] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function getDefaultConfig(): ?array
|
|
|
|
{
|
|
|
|
return Yaml::parseFile($this->filePaths->get('defaultSpinnerConfig')?->getAbsolutePath())['options']
|
|
|
|
?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setFilePaths(): void
|
|
|
|
{
|
|
|
|
$this->filePaths = new FilePathCollection([
|
|
|
|
'config' => new SpinnerFilePath('config'),
|
|
|
|
'defaultSpinnerConfig' => new SpinnerFilePath('config/spinner.yaml'),
|
|
|
|
'envTemplate' => new SpinnerFilePath('config/.template.env'),
|
|
|
|
'data' => new SpinnerFilePath('data'),
|
|
|
|
'phpYamlTemplate' => new SpinnerFilePath('config/php.yaml'),
|
2025-04-13 20:07:48 +01:00
|
|
|
'nginxYamlTemplate' => new SpinnerFilePath('config/nginx.yaml'),
|
2025-04-13 12:59:44 +01:00
|
|
|
'phpFpmDataDirectory' => new SpinnerFilePath('config/php-fpm'),
|
2025-04-13 20:07:48 +01:00
|
|
|
DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE),
|
|
|
|
DataPathInterface::CONFIG_NGINX_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_NGINX_DOCKERFILE),
|
2025-04-13 12:59:44 +01:00
|
|
|
'nodeDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/Node.Dockerfile'),
|
2025-04-13 23:33:55 +01:00
|
|
|
'xdebugIniTemplate' => new SpinnerFilePath('config/php-fpm/xdebug.ini'),
|
2025-04-14 02:52:39 +01:00
|
|
|
'opcacheIniTemplate' => new SpinnerFilePath('config/php-fpm/opcache.ini'),
|
2025-04-13 23:33:55 +01:00
|
|
|
'xdebugDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/XDebug.Dockerfile'),
|
2025-04-13 12:59:44 +01:00
|
|
|
]);
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
}
|