diff --git a/system/src/Server/Groups.php b/system/src/Server/Groups.php index 7d489f6a..7f315b91 100644 --- a/system/src/Server/Groups.php +++ b/system/src/Server/Groups.php @@ -20,7 +20,7 @@ class Groups $groups->load(); - return $groups->getGroups(); + return $groups->get(); }); } diff --git a/system/src/Server/Outfits.php b/system/src/Server/Outfits.php index 5ca42299..1b7dc669 100644 --- a/system/src/Server/Outfits.php +++ b/system/src/Server/Outfits.php @@ -20,7 +20,7 @@ class Outfits $outfits->load(); - return $outfits->getOutfits(); + return $outfits->get(); }); } diff --git a/system/src/Server/TOML/Groups.php b/system/src/Server/TOML/Groups.php index ae6b642a..779d4e6d 100644 --- a/system/src/Server/TOML/Groups.php +++ b/system/src/Server/TOML/Groups.php @@ -16,12 +16,20 @@ class Groups if(!@file_exists($file)) { error('Error: Cannot load groups.toml. More info in system/logs/error.log file.'); - log_append('error.log', "[OTS_Groups_List.php] Fatal error: Cannot load groups.toml ($file). It doesn't exist."); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load groups.toml - $file. It doesn't exist."); return; } $toml = file_get_contents($file); - $groups = Toml::decode($toml, asArray: true); + + try { + $groups = Toml::decode($toml, asArray: true); + } + catch (\Exception $e) { + error('Error: Cannot load groups.toml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load groups.toml - $file. Error: " . $e->getMessage()); + return; + } foreach ($groups as $group) { @@ -33,7 +41,7 @@ class Groups } } - public function getGroups(): array { + public function get(): array { return $this->groups; } } diff --git a/system/src/Server/TOML/Items.php b/system/src/Server/TOML/Items.php index dc957349..ede7ebe7 100644 --- a/system/src/Server/TOML/Items.php +++ b/system/src/Server/TOML/Items.php @@ -38,13 +38,10 @@ class Items continue; } - $article = $item['article'] ?? ''; - $name = $item['name'] ?? ''; - $plural = $item['plural'] ?? ''; $items[$id] = [ - 'article' => $article, - 'name' => $name, - 'plural' => $plural, + 'article' => $item['article'] ?? '', + 'name' => $item['name'] ?? '', + 'plural' => $item['plural'] ?? '', 'attributes' => $attributes, ]; } diff --git a/system/src/Server/TOML/Mounts.php b/system/src/Server/TOML/Mounts.php index cad7e3a8..1fb7e5e9 100644 --- a/system/src/Server/TOML/Mounts.php +++ b/system/src/Server/TOML/Mounts.php @@ -19,7 +19,15 @@ class Mounts } $toml = file_get_contents($file); - $mounts = Toml::decode($toml, asArray: true); + + try { + $mounts = Toml::decode($toml, asArray: true); + } + catch (\Exception $e) { + error('Error: Cannot load mounts.toml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load mounts.toml - $file. Error: " . $e->getMessage()); + return; + } foreach ($mounts as $name => $mount) { diff --git a/system/src/Server/TOML/Outfits.php b/system/src/Server/TOML/Outfits.php index 122631da..01be1917 100644 --- a/system/src/Server/TOML/Outfits.php +++ b/system/src/Server/TOML/Outfits.php @@ -19,7 +19,15 @@ class Outfits } $toml = file_get_contents($file); - $outfits = Toml::decode($toml, asArray: true); + + try { + $outfits = Toml::decode($toml, asArray: true); + } + catch (\Exception $e) { + error('Error: Cannot load outfits.toml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load outfits.toml - $file. Error: " . $e->getMessage()); + return; + } foreach ($outfits as $outfit) { @@ -34,7 +42,7 @@ class Outfits } } - public function getOutfits(): array { + public function get(): array { return $this->outfits; } } diff --git a/system/src/Server/TOML/Vocations.php b/system/src/Server/TOML/Vocations.php index f7c53e93..c29b3d1b 100644 --- a/system/src/Server/TOML/Vocations.php +++ b/system/src/Server/TOML/Vocations.php @@ -18,7 +18,16 @@ class Vocations foreach ($tomlVocations as $file) { $toml = file_get_contents($file); - $vocations = Toml::decode($toml, asArray: true); + + try { + $vocations = Toml::decode($toml, asArray: true); + } + catch (\Exception $e) { + $basename = basename($file); + error("Error: Cannot load vocations/$basename. More info in system/logs/error.log file."); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load mounts.toml - $file. Error: " . $e->getMessage()); + break; + } foreach ($vocations as $vocationArray) { $id = $vocationArray['id']; diff --git a/system/src/Server/XML/Groups.php b/system/src/Server/XML/Groups.php index 0b487509..8ceadbdd 100644 --- a/system/src/Server/XML/Groups.php +++ b/system/src/Server/XML/Groups.php @@ -6,20 +6,22 @@ class Groups { private array $groups = []; + const FILE = 'XML/groups.xml'; + public function load(): void { - $file = config('data_path') . 'XML/groups.xml'; + $file = config('data_path') . self::FILE; if(!@file_exists($file)) { error('Error: Cannot load groups.xml. More info in system/logs/error.log file.'); - log_append('error.log', "[OTS_Groups_List.php] Fatal error: Cannot load groups.xml ($file). It doesn't exist."); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load groups.xml - $file. It doesn't exist."); return; } $groups = new \DOMDocument(); if(!@$groups->load($file)) { error('Error: Cannot load groups.xml. More info in system/logs/error.log file.'); - log_append('error.log', '[OTS_Groups_List.php] Fatal error: Cannot load groups.xml (' . $file . '). Error: ' . print_r(error_get_last(), true)); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load groups.xml - $file. Error: " . print_r(error_get_last(), true)); return; } @@ -34,7 +36,7 @@ class Groups } } - public function getGroups(): array { + public function get(): array { return $this->groups; } } diff --git a/system/src/Server/XML/Items.php b/system/src/Server/XML/Items.php index b6335a1b..fbb1994a 100644 --- a/system/src/Server/XML/Items.php +++ b/system/src/Server/XML/Items.php @@ -8,20 +8,26 @@ class Items { private string $error = ''; + const FILE = 'items/items.xml'; + public function getError(): string { return $this->error; } public function load(): bool { - $file_path = config('data_path') . 'items/items.xml'; - if (!file_exists($file_path)) { - $this->error = 'Cannot load file ' . $file_path; + $file = config('data_path') . self::FILE; + if (!file_exists($file)) { + $this->error = 'Cannot load file ' . $file; return false; } $xml = new \DOMDocument; - $xml->load($file_path); + if(!$xml->load($file)) { + $this->error = 'Error: Cannot load items.xml. More info in system/logs/error.log file.'; + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load items.xml - $file. Error: " . print_r(error_get_last(), true)); + return false; + } $items = []; foreach ($xml->getElementsByTagName('item') as $item) { @@ -52,6 +58,14 @@ class Items $attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value'); } - return ['id' => $id, 'content' => ['article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => $attributes]]; + return [ + 'id' => $id, + 'content' => [ + 'article' => $article, + 'name' => $name, + 'plural' => $plural, + 'attributes' => $attributes + ], + ]; } } diff --git a/system/src/Server/XML/Mounts.php b/system/src/Server/XML/Mounts.php index 5773757c..f4a0dd19 100644 --- a/system/src/Server/XML/Mounts.php +++ b/system/src/Server/XML/Mounts.php @@ -17,7 +17,11 @@ class Mounts } $xml = new \DOMDocument(); - $xml->load($file); + if(!$xml->load($file)) { + error('Error: Cannot load mounts.xml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load mounts.xml - $file. Error: " . print_r(error_get_last(), true)); + return; + } foreach ($xml->getElementsByTagName('mount') as $mount) { $this->mounts[] = $this->parseMountNode($mount); diff --git a/system/src/Server/XML/Outfits.php b/system/src/Server/XML/Outfits.php index 9616fdd3..1fb191d0 100644 --- a/system/src/Server/XML/Outfits.php +++ b/system/src/Server/XML/Outfits.php @@ -16,8 +16,12 @@ class Outfits return; } - $xml = new \DOMDocument; - $xml->load($file); + $xml = new \DOMDocument(); + if(!$xml->load($file)) { + error('Error: Cannot load outfits.xml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load outfits.xml - $file. Error: " . print_r(error_get_last(), true)); + return; + } foreach ($xml->getElementsByTagName('outfit') as $outfit) { $this->outfits[] = $this->parseOutfitNode($outfit); @@ -43,7 +47,7 @@ class Outfits ]; } - public function getOutfits(): array { + public function get(): array { return $this->outfits; } } diff --git a/system/src/Server/XML/Vocations.php b/system/src/Server/XML/Vocations.php index 90b878ae..684885a2 100644 --- a/system/src/Server/XML/Vocations.php +++ b/system/src/Server/XML/Vocations.php @@ -7,23 +7,23 @@ class Vocations private array $vocations = []; private array $vocationsFrom = []; + const FILE = 'vocations.xml'; + public function load(): void { - if(!class_exists('DOMDocument')) { - throw new \RuntimeException('Please install PHP xml extension. MyAAC will not work without it.'); - } - - $vocationsXML = new \DOMDocument(); - $file = config('data_path') . 'XML/vocations.xml'; + $file = config('data_path') . 'XML/' . self::FILE; if(!@file_exists($file)) { - $file = config('data_path') . 'vocations.xml'; + $file = config('data_path') . self::FILE; } - if(!$vocationsXML->load($file)) { - throw new \RuntimeException('ERROR: Cannot load vocations.xml - the file is malformed. Check the file with xml syntax validator.'); + $xml = new \DOMDocument(); + if(!$xml->load($file)) { + error('Error: Cannot load vocations.xml. More info in system/logs/error.log file.'); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load vocations.xml - $file. Error: " . print_r(error_get_last(), true)); + return; } - foreach($vocationsXML->getElementsByTagName('vocation') as $vocation) { + foreach($xml->getElementsByTagName('vocation') as $vocation) { $id = $vocation->getAttribute('id'); $this->vocations[$id] = $vocation->getAttribute('name');