- 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.
 - Changed properties to become typed properties
This commit is contained in:
netkas 2025-01-07 21:14:03 -05:00
parent 6966fd39d0
commit 936485dde7
2 changed files with 34 additions and 14 deletions

View file

@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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

View file

@ -1,7 +1,5 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace ConfigLib;
use Exception;
@ -14,44 +12,42 @@
{
/**
* The name of the configuration
*
* @var string
* @var string|array
*/
private $name;
private string|array $name;
/**
* The path to the configuration file
*
* @var string
* @var string|null
*/
private $path;
private ?string $path;
/**
* The configuration data
*
* @var array
*/
private $configuration;
private array $configuration;
/**
* Indicates if the current instance is modified
*
* @var bool
*/
private $modified;
private bool $modified;
/**
* Public Constructor
*
* @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)
{
// Sanitize $name for a file path
$name = strtolower($name);
$name = str_replace(array('/', '\\', '.'), '_', $name);
$env = getenv(sprintf("CONFIGLIB_%s", strtoupper($name)));
$this->path = null;
if($env !== false)
{
if(file_exists($env))
@ -64,6 +60,21 @@
}
}
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)
{
$filePath = $name . '.conf';