Heimdall/app/Application.php

187 lines
4.3 KiB
PHP
Raw Normal View History

2018-10-18 14:59:38 +00:00
<?php
namespace App;
use GuzzleHttp\Exception\GuzzleException;
2018-10-18 14:59:38 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
2018-10-18 14:59:38 +00:00
class Application extends Model
{
/**
* @var bool
*/
2018-10-25 13:42:14 +00:00
public $incrementing = false;
/**
* @var string
*/
2018-10-25 13:42:14 +00:00
protected $primaryKey = 'appid';
/**
* @return mixed
*/
2018-10-19 14:10:05 +00:00
public function icon()
{
if (! file_exists(storage_path('app/public/'.$this->icon))) {
$img_src = app_path('SupportedApps/'.$this->name.'/'.str_replace('icons/', '', $this->icon));
$img_dest = storage_path('app/public/'.$this->icon);
//die("i: ".$img_src);
@copy($img_src, $img_dest);
}
2018-10-21 11:39:12 +00:00
return $this->icon;
}
/**
* @return string
*/
public function iconView(): string
2018-10-21 11:39:12 +00:00
{
return asset('storage/'.$this->icon);
2018-10-19 14:10:05 +00:00
}
/**
* @return string
*/
public function defaultColour(): string
2018-10-19 14:10:05 +00:00
{
// check if light or dark
if ($this->tile_background == 'light') {
return '#fafbfc';
}
2018-10-19 14:10:05 +00:00
return '#161b1f';
}
/**
* @return string
*/
public function class(): string
2018-10-19 14:10:05 +00:00
{
$name = $this->name;
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
return '\App\SupportedApps\\'.$name.'\\'.$name;
2018-10-19 14:10:05 +00:00
}
2022-03-16 15:49:44 +00:00
/**
* @param $name
* @return string
*/
public static function classFromName($name): string
2022-03-16 15:49:44 +00:00
{
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
2022-03-16 15:49:44 +00:00
2022-03-19 14:05:01 +00:00
$class = '\App\SupportedApps\\'.$name.'\\'.$name;
2022-03-16 15:49:44 +00:00
return $class;
}
2018-10-21 11:39:12 +00:00
/**
* @return \Illuminate\Support\Collection
*/
public static function apps(): \Illuminate\Support\Collection
{
$json = json_decode(file_get_contents(storage_path('app/supportedapps.json'))) ?? [];
$apps = collect($json->apps);
return $apps->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE);
}
/**
* @return array
*/
public static function autocomplete(): array
{
$apps = self::apps();
$list = [];
foreach ($apps as $app) {
$list[] = (object) [
'label' => $app->name,
'value' => $app->appid,
];
}
return $list;
}
/**
* @param $appid
* @return mixed|null
* @throws GuzzleException
*/
2022-03-15 18:19:01 +00:00
public static function getApp($appid)
{
Log::debug("Get app triggered for: $appid");
$localapp = self::where('appid', $appid)->first();
2022-03-15 18:19:01 +00:00
$app = self::single($appid);
$application = ($localapp) ? $localapp : new self;
2022-03-15 18:19:01 +00:00
// Files missing? || app not in db || old sha version
if (! file_exists(app_path('SupportedApps/'.className($app->name))) ||
! $localapp ||
$localapp->sha !== $app->sha
) {
$gotFiles = SupportedApps::getFiles($app);
if ($gotFiles) {
2022-03-15 18:19:01 +00:00
$app = SupportedApps::saveApp($app, $application);
}
}
return $app;
2022-03-15 18:19:01 +00:00
}
/**
* @param $appid
* @return mixed|null
*/
public static function single($appid)
{
$apps = self::apps();
$app = $apps->where('appid', $appid)->first();
if ($app === null) {
// Try in db for Private App
$appModel = self::where('appid', $appid)->first();
if ($appModel) {
$app = json_decode($appModel->toJson());
}
}
if ($app === null) {
return null;
}
$classname = preg_replace('/[^\p{L}\p{N}]/u', '', $app->name);
2022-03-19 14:05:01 +00:00
$app->class = '\App\SupportedApps\\'.$classname.'\\'.$classname;
return $app;
}
/**
* @return array
*/
public static function applist(): array
2018-10-23 14:53:56 +00:00
{
$list = [];
$list['null'] = 'None';
$apps = self::apps();
foreach ($apps as $app) {
$list[$app->appid] = $app->name;
2018-10-23 14:53:56 +00:00
}
// Check for private apps in the db
$appsListFromDB = self::all(['appid', 'name']);
foreach ($appsListFromDB as $app) {
// Already existing keys are overwritten,
// only private apps should be added at the end
$list[$app->appid] = $app->name;
}
2018-10-23 14:53:56 +00:00
return $list;
}
2018-10-18 14:59:38 +00:00
}