diff --git a/src/EnvReader/EnvConfig.php b/src/EnvReader/EnvConfig.php index e52dd2d..22282cb 100644 --- a/src/EnvReader/EnvConfig.php +++ b/src/EnvReader/EnvConfig.php @@ -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) { diff --git a/src/EnvReader/GenericConfig.php b/src/EnvReader/GenericConfig.php index f4b539c..4e7453d 100644 --- a/src/EnvReader/GenericConfig.php +++ b/src/EnvReader/GenericConfig.php @@ -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'); diff --git a/src/EnvReader/TMultiLevelProperties.php b/src/EnvReader/TMultiLevelProperties.php new file mode 100644 index 0000000..5a0ad2e --- /dev/null +++ b/src/EnvReader/TMultiLevelProperties.php @@ -0,0 +1,82 @@ +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)); + } +} \ No newline at end of file