Add TMultiLevelProperties for multi-level support

+ No default properties in GenericConfig
+ EnvConfig throws Exception if config file not found
2.x
Julien Rosset 3 years ago
parent 7dea458c68
commit 016c97c875

@ -11,12 +11,12 @@ abstract class EnvConfig extends GenericConfig implements IExternalConfigFile {
protected function readConfig (): void {
$configFile = $this->getConfigFilePath();
if (!file_exists($configFile)) {
return;
throw new Exception('Unable to find configuration file "' . $configFile . '"');
}
$lines = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
throw new Exception('Unable to read environment file "' . $configFile . '"');
throw new Exception('Unable to read configuration file "' . $configFile . '"');
}
foreach ($lines as $line) {

@ -43,9 +43,7 @@ abstract class GenericConfig {
* @noinspection PhpReturnDocTypeMismatchInspection
*/
protected function initialProperties () {
return [
'APP_DEV' => 0,
];
return [];
}
/**
* Read the configuration file
@ -77,15 +75,15 @@ abstract class GenericConfig {
/**
* Get a property value
*
* @param string $name The property name
* @param string|null $default The default value if property is not set
* Raise an exception if property is not set AND $default is Null
* @param string $name The property name
* @param mixed $default The default value if property is not set
* Raise an exception if property is not set AND $default is Null
*
* @return string The property value
* @return mixed The property value
*
* @throws UnexpectedValueException If property is not set AND $default is Null
*/
public function getProperty (string $name, ?string $default = null): string {
public function getProperty (string $name, $default = null) {
if (!$this->hasProperty($name)) {
if ($default === null) {
throw new UnexpectedValueException('The "' . $name . '" property is not set');

@ -0,0 +1,82 @@
<?php
namespace jrosset\EnvReader;
use jrosset\ArrayClasses\IArrayClass;
use jrosset\ArrayClasses\ImmutableArrayClass;
use UnexpectedValueException;
/**
* Trait for multi-level properties
*/
trait TMultiLevelProperties {
/**
* @inheritDoc
*/
public function hasProperty (string $name): bool {
$levels = $this->getPropertyLevels($name);
$value = $this->getProperties();
foreach ($levels as $level) {
if ($value instanceof IArrayClass) {
if (!$value->has($level)) {
return false;
}
$value = $value->get($level);
}
else {
if (!isset($value[$level])) {
return false;
}
$value = $value[$level];
}
}
return true;
}
/**
* @inheritDoc
*/
public function getProperty (string $name, $default = null) {
if (!$this->hasProperty($name)) {
if ($default === null) {
throw new UnexpectedValueException('The "' . $name . '" property is not set');
}
return $default;
}
$levels = $this->getPropertyLevels($name);
$value = $this->getProperties();
foreach ($levels as $level) {
if ($value instanceof IArrayClass) {
$value = $value->get($level);
}
else {
$value = $value[$level];
}
}
return $value;
}
/**
* Provide the multi-level separator
*
* Reimplements to override
*
* @return string The separator
*/
protected function getPropertySeparator (): string {
return '/';
}
/**
* Get array of levels of a property name
*
* @param string $propertyName The property name
*
* @return ImmutableArrayClass The property levels
*/
private function getPropertyLevels (string $propertyName): ImmutableArrayClass {
return new ImmutableArrayClass(explode($this->getPropertySeparator(), $propertyName));
}
}
Loading…
Cancel
Save