Fix configuration path resolution and add setup script

This commit is contained in:
netkas 2024-09-27 13:18:53 -04:00
parent 2c05ae8b3c
commit ff2296c786
4 changed files with 95 additions and 2 deletions

View file

@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.2] - 2024-09-27
This update fixes a critical bug where configuration files may not be found when using different user accounts,
especially when the configuration file is located in a directory that is not accessible by the user account running the
application. This was fixed by changing the way the configuration file path is resolved including by adding a setup
execution unit that will be executed post-installation to ensure that the configuration file is accessible by the user
account running the application.
## [1.1.1] - 2024-09-26 ## [1.1.1] - 2024-09-26
This update introduces a minor bug fix This update introduces a minor bug fix

View file

@ -27,15 +27,29 @@
"working_directory": "%CWD%", "working_directory": "%CWD%",
"tty": true "tty": true
} }
},
{
"name": "setup",
"runner": "php",
"execute": {
"target": "setup",
"working_directory": "%CWD%",
"tty": true
}
} }
], ],
"installer": {
"post_install": [
"setup"
]
},
"assembly": { "assembly": {
"name": "ConfigLib", "name": "ConfigLib",
"package": "net.nosial.configlib", "package": "net.nosial.configlib",
"company": "Nosial", "company": "Nosial",
"copyright": "Copyright (c) 2022-2023 Nosial", "copyright": "Copyright (c) 2022-2023 Nosial",
"description": "ConfigLib is a library for reading and writing configuration files via the NCC Runtime API", "description": "ConfigLib is a library for reading and writing configuration files via the NCC Runtime API",
"version": "1.1.1", "version": "1.1.2",
"uuid": "9347259e-8e4d-11ed-85a7-fd07cf28ef35" "uuid": "9347259e-8e4d-11ed-85a7-fd07cf28ef35"
}, },
"build": { "build": {

14
setup Normal file
View file

@ -0,0 +1,14 @@
<?php
$global_directory = $path = DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'configlib';
//try creating the directory if it doesn't exist
if(!file_exists($global_directory))
{
if(!mkdir($global_directory, 0777, true))
{
exit('Failed to create global directory');
}
chmod($global_directory, 0777);
}

View file

@ -64,9 +64,15 @@
} }
} }
$filePath = $name . '.conf';
$globalDir = self::getGlobalDirectory();
if(is_dir($globalDir) && is_writable($globalDir))
{
$this->path = $globalDir . DIRECTORY_SEPARATOR . $name . '.conf';
}
if ($this->path === null) if ($this->path === null)
{ {
$filePath = $name . '.conf';
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{ {
$configDir = getenv('APPDATA') ?: getenv('LOCALAPPDATA'); $configDir = getenv('APPDATA') ?: getenv('LOCALAPPDATA');
@ -514,4 +520,54 @@
$fs->dumpFile($path, $this->toYaml()); $fs->dumpFile($path, $this->toYaml());
$fs->chmod($path, 0777); $fs->chmod($path, 0777);
} }
/**
* Retrieves the global directory path for configuration files.
*
* @return string The global directory path for configuration files.
*/
public static function getGlobalDirectory(): string
{
$path = DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'configlib';
if(file_exists($path) && is_writable($path))
{
return $path;
}
if((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'))
{
$variables = [
'SYSTEMDRIVE',
'CSIDL_APPDATA',
'CSIDL_PROGRAM_FILES'
];
foreach($variables as $variable)
{
$environment_variable = getenv($variable);
if($environment_variable)
{
return $environment_variable . DIRECTORY_SEPARATOR . 'configlib';
}
}
}
else
{
$variables = [
'HOME',
'XDG_CONFIG_HOME',
'XDG_DATA_HOME'
];
foreach($variables as $variable)
{
$environment_variable = getenv($variable);
if($environment_variable)
{
return $environment_variable . DIRECTORY_SEPARATOR . 'configlib';
}
}
}
}
} }