refactor: state class usage with getServerStateJson

This commit is contained in:
Zack Spear
2023-11-09 16:31:48 -08:00
parent 205552eda5
commit 463ff4a38a
3 changed files with 21 additions and 8 deletions

View File

@@ -7,9 +7,8 @@ require_once "$docroot/webGui/include/Helpers.php";
extract(parse_plugin_cfg('dynamix',true));
require_once "$docroot/plugins/dynamix.my.servers/include/state.php";
$serverStateClass = new ServerState();
$serverStateData = $serverStateClass->getServerState();
$serverState = new ServerState();
header('Content-type: application/json');
echo json_encode($serverStateData);
echo $serverState->getServerStateJson();

View File

@@ -3,7 +3,7 @@ $docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once("$docroot/plugins/dynamix.my.servers/include/state.php");
require_once("$docroot/plugins/dynamix.my.servers/include/translations.php");
$serverStateClass = new ServerState();
$serverState = new ServerState();
$wCTranslations = new WebComponentTranslations();
?>
<script>
@@ -24,5 +24,5 @@ if (!document.getElementsByTagName(modalsWebComponent).length) {
<?
echo "
<unraid-i18n-host>
<unraid-user-profile server='" . json_encode($serverStateClass->getServerState()) . "'></unraid-user-profile>
<unraid-user-profile server='" . $serverState->getServerStateJson() . "'></unraid-user-profile>
</unraid-i18n-host>";

View File

@@ -1,4 +1,7 @@
<?php
/**
* @todo refactor globals currently if you try to use $GLOBALS the class will break.
*/
$webguiGlobals = $GLOBALS;
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
@@ -9,7 +12,10 @@ require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
* Usage:
* ```
* $serverStateClass = new ServerState();
* $serverStateData = $serverStateClass->getServerState();
*
* $serverStateClass->getServerState();
* or
* $serverStateClass->getServerStateJson();
* ```
*/
class ServerState
@@ -86,7 +92,7 @@ class ServerState
}
/**
* Retrieve the server information as an associative array.
* Retrieve the server information as an associative array
*
* @return array An array containing server information.
*/
@@ -156,4 +162,12 @@ class ServerState
return $serverState;
}
}
/**
* Retrieve the server information as a JSON string
*
* @return string A JSON string containing server information.
*/
public function getServerStateJson() {
return json_encode($this->getServerState());
}
}