Files
hortusfox-web/app/modules/ThemeModule.php
Daniel Brendel 0a56ebab88 #120 Theme system
2024-03-04 22:05:03 +01:00

123 lines
4.0 KiB
PHP

<?php
/**
* This class represents your module
*/
class ThemeModule {
/** @var array $theme_data */
public static $theme_data = null;
/**
* @param $path
* @return void
* @throws \Exception
*/
public static function load($path)
{
try {
self::$theme_data = null;
self::$theme_data = json_decode(file_get_contents($path . '/layout.json'));
if (!is_object(self::$theme_data)) {
throw new \Exception('Invalid data @ ' . $path . '/layout.json: ' . print_r(self::$theme_data, true));
}
if (!file_exists($path . '/' . self::$theme_data->banner)) {
throw new \Exception('Banner asset not found');
}
self::$theme_data->banner_url = asset('themes/' . self::$theme_data->name . '/' . self::$theme_data->banner);
self::$theme_data->inline_rules = '';
foreach (self::$theme_data->rules as $key => $value) {
self::$theme_data->inline_rules .= $key . ': ' . $value . ' !important;';
}
if (isset(self::$theme_data->icon)) {
if (!file_exists($path . '/' . self::$theme_data->icon->asset)) {
throw new \Exception('Icon asset not found');
}
self::$theme_data->icon->url = asset('themes/' . self::$theme_data->name . '/' . self::$theme_data->icon->asset);
self::$theme_data->icon->inline_rules = new \stdClass();
self::$theme_data->icon->inline_rules->base = '';
foreach (self::$theme_data->icon->base as $key => $value) {
self::$theme_data->icon->inline_rules->base .= $key . ': ' . $value . ' !important;';
}
self::$theme_data->icon->inline_rules->img = '';
foreach (self::$theme_data->icon->img as $key => $value) {
self::$theme_data->icon->inline_rules->img .= $key . ': ' . $value . ' !important;';
}
}
if (isset(self::$theme_data->accessory)) {
if (!file_exists($path . '/' . self::$theme_data->accessory->asset)) {
throw new \Exception('Icon asset not found');
}
self::$theme_data->accessory->url = asset('themes/' . self::$theme_data->name . '/' . self::$theme_data->accessory->asset);
self::$theme_data->accessory->inline_rules = new \stdClass();
self::$theme_data->accessory->inline_rules->base = '';
foreach (self::$theme_data->accessory->base as $key => $value) {
self::$theme_data->accessory->inline_rules->base .= $key . ': ' . $value . ' !important;';
}
self::$theme_data->accessory->inline_rules->img = '';
foreach (self::$theme_data->accessory->img as $key => $value) {
self::$theme_data->accessory->inline_rules->img .= $key . ': ' . $value . ' !important;';
}
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* @return mixed
* @throws \Exception
*/
public static function data()
{
try {
return self::$theme_data;
} catch (\Exception $e) {
throw $e;
}
}
/**
* @return bool
*/
public static function ready()
{
return (is_object(self::$theme_data));
}
/**
* @return array
* @throws \Exception
*/
public static function list()
{
try {
$result = [];
$folders = scandir(public_path() . '/themes');
foreach ($folders as $folder) {
if (((substr($folder, 0, 1)) !== '.') && (is_dir(public_path() . '/themes/' . $folder))) {
$result[] = $folder;
}
}
return $result;
} catch (\Exception $e) {
throw $e;
}
}
}