mirror of
https://github.com/slawkens/myaac.git
synced 2026-03-16 16:03:31 +01:00
[WIP] Refactoring - Uniform error message + TOML exception catch
This commit is contained in:
@@ -20,7 +20,7 @@ class Groups
|
||||
|
||||
$groups->load();
|
||||
|
||||
return $groups->getGroups();
|
||||
return $groups->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class Outfits
|
||||
|
||||
$outfits->load();
|
||||
|
||||
return $outfits->getOutfits();
|
||||
return $outfits->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -19,7 +19,15 @@ class Mounts
|
||||
}
|
||||
|
||||
$toml = file_get_contents($file);
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -19,7 +19,15 @@ class Outfits
|
||||
}
|
||||
|
||||
$toml = file_get_contents($file);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,16 @@ class Vocations
|
||||
|
||||
foreach ($tomlVocations as $file) {
|
||||
$toml = file_get_contents($file);
|
||||
|
||||
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'];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 <i>vocations.xml</i> - 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');
|
||||
|
||||
Reference in New Issue
Block a user