Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
88ec33ff1a | |||
bf71326090 | |||
![]() |
936485dde7 | ||
![]() |
6966fd39d0 |
5 changed files with 85 additions and 27 deletions
3
.idea/php.xml
generated
3
.idea/php.xml
generated
|
@ -11,9 +11,10 @@
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpIncludePathManager">
|
<component name="PhpIncludePathManager">
|
||||||
<include_path>
|
<include_path>
|
||||||
|
<path value="/var/ncc/packages/net.nosial.optslib=1.1.2" />
|
||||||
|
<path value="/var/ncc/packages/net.nosial.loglib2=1.0.2" />
|
||||||
<path value="/var/ncc/packages/com.symfony.yaml=v7.1.5" />
|
<path value="/var/ncc/packages/com.symfony.yaml=v7.1.5" />
|
||||||
<path value="/var/ncc/packages/net.nosial.loglib=2.0.4" />
|
<path value="/var/ncc/packages/net.nosial.loglib=2.0.4" />
|
||||||
<path value="/var/ncc/packages/net.nosial.optslib=1.1.2" />
|
|
||||||
<path value="/var/ncc/packages/com.symfony.polyfill_mbstring=v1.31.0" />
|
<path value="/var/ncc/packages/com.symfony.polyfill_mbstring=v1.31.0" />
|
||||||
<path value="/var/ncc/packages/com.symfony.polyfill_ctype=v1.31.0" />
|
<path value="/var/ncc/packages/com.symfony.polyfill_ctype=v1.31.0" />
|
||||||
<path value="/usr/share/ncc" />
|
<path value="/usr/share/ncc" />
|
||||||
|
|
23
CHANGELOG.md
23
CHANGELOG.md
|
@ -5,6 +5,29 @@ 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.7] - 2025-03-14
|
||||||
|
|
||||||
|
This update introduces minor changes
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Updated remote references for dependencies
|
||||||
|
- Updated Library to use net.nosial.loglib2 instead of the now deprecated net.nosial.loglib
|
||||||
|
|
||||||
|
|
||||||
|
## [1.1.6] - 2025-01-07
|
||||||
|
|
||||||
|
This update introduces minor improvements
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Changed properties to become typed properties
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Added a new constructor parameter called `path` which is an optional parameter that allows you to specify the path to
|
||||||
|
the configuration files directory. If not specified the library will proceed with resolving
|
||||||
|
the path to the configuration files directory using the default method. This will override
|
||||||
|
the `CONFIGLIB_PATH` environment variable if it is set.
|
||||||
|
|
||||||
|
|
||||||
## [1.1.5] - 2024-12-27
|
## [1.1.5] - 2024-12-27
|
||||||
|
|
||||||
This update introduces minor improvements
|
This update introduces minor improvements
|
||||||
|
|
15
README.md
15
README.md
|
@ -9,10 +9,25 @@ be configured more easily by following the documented instructions on how to alt
|
||||||
could use a builtin editor to edit the configuration file manually.
|
could use a builtin editor to edit the configuration file manually.
|
||||||
|
|
||||||
|
|
||||||
|
## Community
|
||||||
|
|
||||||
|
This project and many others from Nosial are available on multiple publicly available and free git repositories at
|
||||||
|
|
||||||
|
- [n64](https://git.n64.cc/nosial/configlib)
|
||||||
|
- [GitHub](https://github.com/nosial/configlib)
|
||||||
|
- [Codeberg](https://codeberg.org/nosial/configlib)
|
||||||
|
|
||||||
|
Issues & Pull Requests are frequently checked and to be referenced accordingly in commits and changes, Nosial remains
|
||||||
|
dedicated to keep these repositories up to date when possible.
|
||||||
|
|
||||||
|
For questions & discussions see the public Telegram community at [@NosialDiscussions](https://t.me/NosialDiscussions).
|
||||||
|
We do encourage community support and discussions, please be respectful and follow the rules of the community.
|
||||||
|
|
||||||
## Table of contents
|
## Table of contents
|
||||||
|
|
||||||
<!-- TOC -->
|
<!-- TOC -->
|
||||||
* [ConfigLib](#configlib)
|
* [ConfigLib](#configlib)
|
||||||
|
* [Community](#community)
|
||||||
* [Table of contents](#table-of-contents)
|
* [Table of contents](#table-of-contents)
|
||||||
* [Installation](#installation)
|
* [Installation](#installation)
|
||||||
* [Compile from source](#compile-from-source)
|
* [Compile from source](#compile-from-source)
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
"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",
|
||||||
"company": "Nosial",
|
"company": "Nosial",
|
||||||
"copyright": "Copyright (c) 2022-2023 Nosial",
|
"copyright": "Copyright (c) 2022-2023 Nosial",
|
||||||
"version": "1.1.5",
|
"version": "1.1.7",
|
||||||
"uuid": "9347259e-8e4d-11ed-85a7-fd07cf28ef35"
|
"uuid": "9347259e-8e4d-11ed-85a7-fd07cf28ef35"
|
||||||
},
|
},
|
||||||
"build": {
|
"build": {
|
||||||
|
@ -38,12 +38,12 @@
|
||||||
{
|
{
|
||||||
"name": "net.nosial.optslib",
|
"name": "net.nosial.optslib",
|
||||||
"version": "latest",
|
"version": "latest",
|
||||||
"source": "nosial/libs.opts=latest@n64"
|
"source": "nosial/optslib=latest@github"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "net.nosial.loglib",
|
"name": "net.nosial.loglib2",
|
||||||
"version": "latest",
|
"version": "latest",
|
||||||
"source": "nosial/libs.log=latest@n64"
|
"source": "nosial/loglib2=latest@github"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "com.symfony.filesystem",
|
"name": "com.symfony.filesystem",
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
|
||||||
|
|
||||||
namespace ConfigLib;
|
namespace ConfigLib;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use LogLib\Log;
|
use LogLib2\Logger;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Symfony\Component\Filesystem\Filesystem;
|
use Symfony\Component\Filesystem\Filesystem;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
@ -13,45 +11,51 @@
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The name of the configuration
|
* The logger of the class
|
||||||
*
|
* @var Logger
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private $name;
|
private Logger $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the configuration
|
||||||
|
* @var string|array
|
||||||
|
*/
|
||||||
|
private string|array $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The path to the configuration file
|
* The path to the configuration file
|
||||||
*
|
* @var string|null
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private $path;
|
private ?string $path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The configuration data
|
* The configuration data
|
||||||
*
|
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $configuration;
|
private array $configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates if the current instance is modified
|
* Indicates if the current instance is modified
|
||||||
*
|
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $modified;
|
private bool $modified;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public Constructor
|
* Public Constructor
|
||||||
*
|
*
|
||||||
* @param string $name The name of the configuration (e.g. "MyApp" or "net.example.myapp")
|
* @param string $name The name of the configuration (e.g. "MyApp" or "net.example.myapp")
|
||||||
|
* @param string|null $path The directory where the configuration file will be stored
|
||||||
*/
|
*/
|
||||||
public function __construct(string $name='default')
|
public function __construct(string $name='default', ?string $path=null)
|
||||||
{
|
{
|
||||||
|
$this->logger = new Logger('net.nosial.configlib');
|
||||||
|
|
||||||
// Sanitize $name for a file path
|
// Sanitize $name for a file path
|
||||||
$name = strtolower($name);
|
$name = strtolower($name);
|
||||||
$name = str_replace(array('/', '\\', '.'), '_', $name);
|
$name = str_replace(array('/', '\\', '.'), '_', $name);
|
||||||
|
|
||||||
$env = getenv(sprintf("CONFIGLIB_%s", strtoupper($name)));
|
$env = getenv(sprintf("CONFIGLIB_%s", strtoupper($name)));
|
||||||
|
$this->path = null;
|
||||||
|
|
||||||
if($env !== false)
|
if($env !== false)
|
||||||
{
|
{
|
||||||
if(file_exists($env))
|
if(file_exists($env))
|
||||||
|
@ -60,10 +64,25 @@
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log::warning('net.nosial.configlib', sprintf('Environment variable "%s" points to a non-existent file, resorting to default/builtin configuration', $env));
|
$this->logger->warning(sprintf('Environment variable "%s" points to a non-existent file, resorting to default/builtin configuration', $env));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($path !== null)
|
||||||
|
{
|
||||||
|
if(!is_dir(dirname($path)))
|
||||||
|
{
|
||||||
|
throw new RuntimeException(sprintf('Directory "%s" does not exist', dirname($path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!is_writable(dirname($path)))
|
||||||
|
{
|
||||||
|
throw new RuntimeException(sprintf('Directory "%s" is not writable', dirname($path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->path = $path;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->path === null)
|
if ($this->path === null)
|
||||||
{
|
{
|
||||||
$filePath = $name . '.conf';
|
$filePath = $name . '.conf';
|
||||||
|
@ -120,7 +139,7 @@
|
||||||
|
|
||||||
if (!$configDir)
|
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()));
|
$this->logger->warning(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';
|
$configDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'configlib';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,7 +171,7 @@
|
||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
Log::error('net.nosial.configlib', sprintf('Unable to load configuration "%s", %s', $this->name, $e->getMessage()));
|
$this->logger->error(sprintf('Unable to load configuration "%s", %s', $this->name, $e->getMessage()), $e);
|
||||||
throw new RuntimeException(sprintf('Unable to load configuration "%s"', $this->name), $e);
|
throw new RuntimeException(sprintf('Unable to load configuration "%s"', $this->name), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -344,7 +363,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->modified = false;
|
$this->modified = false;
|
||||||
Log::debug('net.nosial.configlib', sprintf('Configuration "%s" saved', $this->name));
|
$this->logger->debug(sprintf('Configuration "%s" saved', $this->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -426,7 +445,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->modified = false;
|
$this->modified = false;
|
||||||
Log::debug('net.nosial.configlib', 'Loaded configuration file: ' . $this->path);
|
$this->logger->debug('Loaded configuration file: ' . $this->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -484,7 +503,7 @@
|
||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
Log::error('net.nosial.configlib', sprintf('Unable to save configuration "%s" to disk, %s', $this->name, $e->getMessage()));
|
$this->logger->error(sprintf('Unable to save configuration "%s" to disk, %s', $this->name, $e->getMessage()), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue