[WIP] Refactoring - Uniform error message + TOML exception catch

This commit is contained in:
slawkens
2026-03-01 21:45:48 +01:00
parent 4ca79a1e85
commit fbb5034458
12 changed files with 92 additions and 38 deletions

View File

@@ -20,7 +20,7 @@ class Groups
$groups->load(); $groups->load();
return $groups->getGroups(); return $groups->get();
}); });
} }

View File

@@ -20,7 +20,7 @@ class Outfits
$outfits->load(); $outfits->load();
return $outfits->getOutfits(); return $outfits->get();
}); });
} }

View File

@@ -16,12 +16,20 @@ class Groups
if(!@file_exists($file)) { if(!@file_exists($file)) {
error('Error: Cannot load groups.toml. More info in system/logs/error.log 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; return;
} }
$toml = file_get_contents($file); $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) foreach ($groups as $group)
{ {
@@ -33,7 +41,7 @@ class Groups
} }
} }
public function getGroups(): array { public function get(): array {
return $this->groups; return $this->groups;
} }
} }

View File

@@ -38,13 +38,10 @@ class Items
continue; continue;
} }
$article = $item['article'] ?? '';
$name = $item['name'] ?? '';
$plural = $item['plural'] ?? '';
$items[$id] = [ $items[$id] = [
'article' => $article, 'article' => $item['article'] ?? '',
'name' => $name, 'name' => $item['name'] ?? '',
'plural' => $plural, 'plural' => $item['plural'] ?? '',
'attributes' => $attributes, 'attributes' => $attributes,
]; ];
} }

View File

@@ -19,7 +19,15 @@ class Mounts
} }
$toml = file_get_contents($file); $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) foreach ($mounts as $name => $mount)
{ {

View File

@@ -19,7 +19,15 @@ class Outfits
} }
$toml = file_get_contents($file); $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) foreach ($outfits as $outfit)
{ {
@@ -34,7 +42,7 @@ class Outfits
} }
} }
public function getOutfits(): array { public function get(): array {
return $this->outfits; return $this->outfits;
} }
} }

View File

@@ -18,7 +18,16 @@ class Vocations
foreach ($tomlVocations as $file) { foreach ($tomlVocations as $file) {
$toml = file_get_contents($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) { foreach ($vocations as $vocationArray) {
$id = $vocationArray['id']; $id = $vocationArray['id'];

View File

@@ -6,20 +6,22 @@ class Groups
{ {
private array $groups = []; private array $groups = [];
const FILE = 'XML/groups.xml';
public function load(): void public function load(): void
{ {
$file = config('data_path') . 'XML/groups.xml'; $file = config('data_path') . self::FILE;
if(!@file_exists($file)) { if(!@file_exists($file)) {
error('Error: Cannot load groups.xml. More info in system/logs/error.log 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; return;
} }
$groups = new \DOMDocument(); $groups = new \DOMDocument();
if(!@$groups->load($file)) { if(!@$groups->load($file)) {
error('Error: Cannot load groups.xml. More info in system/logs/error.log 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; return;
} }
@@ -34,7 +36,7 @@ class Groups
} }
} }
public function getGroups(): array { public function get(): array {
return $this->groups; return $this->groups;
} }
} }

View File

@@ -8,20 +8,26 @@ class Items
{ {
private string $error = ''; private string $error = '';
const FILE = 'items/items.xml';
public function getError(): string { public function getError(): string {
return $this->error; return $this->error;
} }
public function load(): bool public function load(): bool
{ {
$file_path = config('data_path') . 'items/items.xml'; $file = config('data_path') . self::FILE;
if (!file_exists($file_path)) { if (!file_exists($file)) {
$this->error = 'Cannot load file ' . $file_path; $this->error = 'Cannot load file ' . $file;
return false; return false;
} }
$xml = new \DOMDocument; $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 = []; $items = [];
foreach ($xml->getElementsByTagName('item') as $item) { foreach ($xml->getElementsByTagName('item') as $item) {
@@ -52,6 +58,14 @@ class Items
$attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value'); $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
],
];
} }
} }

View File

@@ -17,7 +17,11 @@ class Mounts
} }
$xml = new \DOMDocument(); $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) { foreach ($xml->getElementsByTagName('mount') as $mount) {
$this->mounts[] = $this->parseMountNode($mount); $this->mounts[] = $this->parseMountNode($mount);

View File

@@ -16,8 +16,12 @@ class Outfits
return; return;
} }
$xml = new \DOMDocument; $xml = new \DOMDocument();
$xml->load($file); 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) { foreach ($xml->getElementsByTagName('outfit') as $outfit) {
$this->outfits[] = $this->parseOutfitNode($outfit); $this->outfits[] = $this->parseOutfitNode($outfit);
@@ -43,7 +47,7 @@ class Outfits
]; ];
} }
public function getOutfits(): array { public function get(): array {
return $this->outfits; return $this->outfits;
} }
} }

View File

@@ -7,23 +7,23 @@ class Vocations
private array $vocations = []; private array $vocations = [];
private array $vocationsFrom = []; private array $vocationsFrom = [];
const FILE = 'vocations.xml';
public function load(): void public function load(): void
{ {
if(!class_exists('DOMDocument')) { $file = config('data_path') . 'XML/' . self::FILE;
throw new \RuntimeException('Please install PHP xml extension. MyAAC will not work without it.');
}
$vocationsXML = new \DOMDocument();
$file = config('data_path') . 'XML/vocations.xml';
if(!@file_exists($file)) { if(!@file_exists($file)) {
$file = config('data_path') . 'vocations.xml'; $file = config('data_path') . self::FILE;
} }
if(!$vocationsXML->load($file)) { $xml = new \DOMDocument();
throw new \RuntimeException('ERROR: Cannot load <i>vocations.xml</i> - the file is malformed. Check the file with xml syntax validator.'); 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'); $id = $vocation->getAttribute('id');
$this->vocations[$id] = $vocation->getAttribute('name'); $this->vocations[$id] = $vocation->getAttribute('name');