mirror of
https://github.com/slawkens/myaac.git
synced 2026-03-16 16:03:31 +01:00
[WIP] Mounts TOML + XML parsers
This commit is contained in:
24
system/src/Server/Mounts.php
Normal file
24
system/src/Server/Mounts.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyAAC\Server;
|
||||||
|
|
||||||
|
use MyAAC\Cache\Cache;
|
||||||
|
|
||||||
|
class Mounts
|
||||||
|
{
|
||||||
|
public static function get()
|
||||||
|
{
|
||||||
|
return Cache::remember('mounts', 10 * 60, function () {
|
||||||
|
if (file_exists(config('server_path') . TOML\Mounts::FILE)) {
|
||||||
|
$mounts = new TOML\Mounts();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$mounts = new XML\Mounts();
|
||||||
|
}
|
||||||
|
|
||||||
|
$mounts->load();
|
||||||
|
|
||||||
|
return $mounts->get();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
39
system/src/Server/TOML/Mounts.php
Normal file
39
system/src/Server/TOML/Mounts.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyAAC\Server\TOML;
|
||||||
|
|
||||||
|
use Devium\Toml\Toml;
|
||||||
|
|
||||||
|
class Mounts
|
||||||
|
{
|
||||||
|
private array $mounts = [];
|
||||||
|
|
||||||
|
const FILE = 'config/mounts.toml';
|
||||||
|
|
||||||
|
public function load(): void
|
||||||
|
{
|
||||||
|
$file = config('server_path') . self::FILE;
|
||||||
|
|
||||||
|
if(!@file_exists($file)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$toml = file_get_contents($file);
|
||||||
|
$mounts = Toml::decode($toml, asArray: true);
|
||||||
|
|
||||||
|
foreach ($mounts as $name => $mount)
|
||||||
|
{
|
||||||
|
$this->mounts[] = [
|
||||||
|
'id' => $mount['id'],
|
||||||
|
'client_id' => $mount['clientid'] ?? false,
|
||||||
|
'name' => $name,
|
||||||
|
'speed' => $mount['speed'] ?? 0,
|
||||||
|
'premium' => $mount['premium'] ?? false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(): array {
|
||||||
|
return $this->mounts;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
system/src/Server/XML/Mounts.php
Normal file
49
system/src/Server/XML/Mounts.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyAAC\Server\XML;
|
||||||
|
|
||||||
|
class Mounts
|
||||||
|
{
|
||||||
|
private array $mounts = [];
|
||||||
|
|
||||||
|
const FILE = 'XML/mounts.xml';
|
||||||
|
|
||||||
|
public function load(): void
|
||||||
|
{
|
||||||
|
$file = config('data_path') . self::FILE;
|
||||||
|
|
||||||
|
if(!@file_exists($file)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$xml = new \DOMDocument();
|
||||||
|
$xml->load($file);
|
||||||
|
|
||||||
|
foreach ($xml->getElementsByTagName('mount') as $mount) {
|
||||||
|
$this->mounts[] = $this->parseMountNode($mount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseMountNode($node): array
|
||||||
|
{
|
||||||
|
$id = (int)$node->getAttribute('id');
|
||||||
|
$client_id = (int)$node->getAttribute('clientid');
|
||||||
|
$name = $node->getAttribute('name');
|
||||||
|
$speed = (int)$node->getAttribute('speed');
|
||||||
|
$premium = getBoolean($node->getAttribute('premium'));
|
||||||
|
$type = $node->getAttribute('type');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $id,
|
||||||
|
'client_id' => $client_id,
|
||||||
|
'name' => $name,
|
||||||
|
'speed' => $speed,
|
||||||
|
'premium' => $premium,
|
||||||
|
'type' => $type
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(): array {
|
||||||
|
return $this->mounts;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user