From 648165a6ad392981a97ea7b9f3097cdbbbb91f81 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Thu, 20 Oct 2022 10:17:00 +0200 Subject: [PATCH] Add base for XmlConfig --- src/EnvReader/IniConfig.php | 28 +++++++++---------- src/EnvReader/XmlConfig.php | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/EnvReader/XmlConfig.php diff --git a/src/EnvReader/IniConfig.php b/src/EnvReader/IniConfig.php index 817ebda..295eb9b 100644 --- a/src/EnvReader/IniConfig.php +++ b/src/EnvReader/IniConfig.php @@ -8,6 +8,20 @@ use jrosset\ArrayClasses\InsensitiveCaseArrayClass; abstract class IniConfig extends GenericConfig implements IExternalConfigFile { use TMultiLevelProperties; + /** + * @inheritDoc + */ + protected function readConfig (): void { + $configFile = $this->getConfigFilePath(); + if (!file_exists($configFile)) { + throw new Exception('Unable to find configuration file "' . $configFile . '"'); + } + + if (($iniData = parse_ini_file($configFile, true, INI_SCANNER_RAW)) === false) { + throw new Exception('Unable to read configuration file "' . $configFile . '"'); + } + self::parseIniLevel($iniData, $this->properties); + } /** * Parse an INI level to properties * @@ -26,18 +40,4 @@ abstract class IniConfig extends GenericConfig implements IExternalConfigFile { $properties->set($iniKey, $iniValue); } } - /** - * @inheritDoc - */ - protected function readConfig (): void { - $configFile = $this->getConfigFilePath(); - if (!file_exists($configFile)) { - throw new Exception('Unable to find configuration file "' . $configFile . '"'); - } - - if (($iniData = parse_ini_file($configFile, true, INI_SCANNER_RAW)) === false) { - throw new Exception('Unable to read configuration file "' . $configFile . '"'); - } - self::parseIniLevel($iniData, $this->properties); - } } \ No newline at end of file diff --git a/src/EnvReader/XmlConfig.php b/src/EnvReader/XmlConfig.php new file mode 100644 index 0000000..4166d46 --- /dev/null +++ b/src/EnvReader/XmlConfig.php @@ -0,0 +1,55 @@ +getConfigFilePath(); + if (!file_exists($configFile)) { + throw new Exception('Unable to find configuration file "' . $configFile . '"'); + } + + if (($xml = simplexml_load_file($configFile)) === false) { + $errors = []; + foreach (libxml_get_errors() as $error) { + $errorStr = ''; + switch ($error->level) { + case LIBXML_ERR_WARNING: + $errorStr .= 'Warning'; + break; + + case LIBXML_ERR_ERROR: + $errorStr .= 'Error'; + break; + + case LIBXML_ERR_FATAL: + $errorStr .= 'Fatal error'; + break; + } + $errorStr .= ' #' . $error->code . ': ' . $error->message . ' (line: ' . $error->line . ', column: ' . $error->column . ')'; + $errors[] = $errorStr; + } + + throw new Exception('Unable to read configuration file "' . $configFile . '": ' . PHP_EOL . implode(PHP_EOL, $errors)); + } + $this->properties = $this->parseXml($xml); + } + /** + * Parse an XML node to properties + * + * @param SimpleXMLElement $xmlNode The INI level + * @param InsensitiveCaseArrayClass $properties [OUT] The out properties list + * + * @return void + */ + protected abstract function parseXml (SimpleXMLElement $xmlNode): InsensitiveCaseArrayClass; +} \ No newline at end of file