Add support for CONFIGLIB_PATH environment variable

This commit is contained in:
netkas 2024-12-27 15:20:36 -05:00
parent 489df9f79a
commit 579813315b

View file

@ -66,54 +66,62 @@
if ($this->path === null) if ($this->path === null)
{ {
$filePath = $name . '.conf'; // If the CONFIGLIB_PATH environment variable is set, use it as the configuration path
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') if(getenv('CONFIGLIB_PATH'))
{ {
$configDir = getenv('APPDATA') ?: getenv('LOCALAPPDATA'); $configDir = getenv('CONFIGLIB_PATH');
if (!$configDir)
{
// Fallback to system temporary directory
$configDir = sys_get_temp_dir();
}
$configDir .= DIRECTORY_SEPARATOR . 'ConfigLib';
} }
else else
{ {
$homeDir = getenv('HOME') ?: ''; $filePath = $name . '.conf';
$configDirs = []; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
if ($homeDir)
{ {
$configDirs[] = $homeDir . DIRECTORY_SEPARATOR . '.configlib'; $configDir = getenv('APPDATA') ?: getenv('LOCALAPPDATA');
$configDirs[] = $homeDir . DIRECTORY_SEPARATOR . '.config' . DIRECTORY_SEPARATOR . 'configlib';
}
$configDirs[] = '/etc/configlib'; if (!$configDir)
$configDirs[] = '/var/lib/configlib';
$configDir = null;
// Iterate over the list of directories and select the first one that can be created or written to
foreach ($configDirs as $dir)
{
if (file_exists($dir) && is_writable($dir))
{ {
$configDir = $dir; // Fallback to system temporary directory
break; $configDir = sys_get_temp_dir();
} }
elseif (!file_exists($dir) && mkdir($dir, 0755, true))
{
$configDir = $dir;
break;
}
}
if (!$configDir) $configDir .= DIRECTORY_SEPARATOR . 'ConfigLib';
}
else
{ {
Log::warning('net.nosial.configlib', sprintf('Unable to find a proper directory to store configuration paths in, using temporary directory instead: %s', sys_get_temp_dir())); $homeDir = getenv('HOME') ?: '';
$configDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'configlib'; $configDirs = [];
if ($homeDir)
{
$configDirs[] = $homeDir . DIRECTORY_SEPARATOR . '.configlib';
$configDirs[] = $homeDir . DIRECTORY_SEPARATOR . '.config' . DIRECTORY_SEPARATOR . 'configlib';
}
$configDirs[] = '/etc/configlib';
$configDirs[] = '/var/lib/configlib';
$configDir = null;
// Iterate over the list of directories and select the first one that can be created or written to
foreach ($configDirs as $dir)
{
if (file_exists($dir) && is_writable($dir))
{
$configDir = $dir;
break;
}
elseif (!file_exists($dir) && mkdir($dir, 0755, true))
{
$configDir = $dir;
break;
}
}
if (!$configDir)
{
Log::warning('net.nosial.configlib', sprintf('Unable to find a proper directory to store configuration paths in, using temporary directory instead: %s', sys_get_temp_dir()));
$configDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'configlib';
}
} }
} }