From a84354b74d8b4384393d580ed765ecd6b87f3db1 Mon Sep 17 00:00:00 2001 From: Dale Davies Date: Sat, 12 Feb 2022 00:22:19 +0000 Subject: [PATCH] Use config in site class --- jumpapp/classes/Config.php | 4 +++- jumpapp/classes/Site.php | 10 ++++++---- jumpapp/classes/Sites.php | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/jumpapp/classes/Config.php b/jumpapp/classes/Config.php index 046579c..a4deb70 100644 --- a/jumpapp/classes/Config.php +++ b/jumpapp/classes/Config.php @@ -23,7 +23,9 @@ class Config { */ private const BASE_APPLICATION_PATHS = [ 'backgroundsdir' => '/assets/backgrounds', - 'sitesfile' => '/sites/sites.json', + 'defaulticonpath' => '/assets/images/default-icon.png', + 'sitesdir' => '/sites', + 'sitesfile' => 'sites.json', 'templatedir' => '/templates', ]; diff --git a/jumpapp/classes/Site.php b/jumpapp/classes/Site.php index 7b4eb87..945a6fc 100644 --- a/jumpapp/classes/Site.php +++ b/jumpapp/classes/Site.php @@ -11,12 +11,14 @@ namespace Jump; */ class Site { + private Config $config; public string $name; public bool $nofollow; public string $icon; public string $url; - public function __construct(array $sitearray) { + public function __construct(Config $config, array $sitearray) { + $this->config = $config; if (!isset($sitearray['name'], $sitearray['url'])) { throw new \Exception('The array passed to Site() must contain the keys "name" and "url"!'); } @@ -30,15 +32,15 @@ class Site { if ($icon === null) { $favicon = new \Favicon\Favicon(); $favicon->cache([ - 'dir' => '/var/www/cache/icons/', + 'dir' => $this->config->get('cachdir').'/icons/', 'timeout' => 86400 ]); $rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE); if (!$rawimage) { - $rawimage = file_get_contents(__DIR__ . '/../assets/images/default-icon.png'); + $rawimage = file_get_contents($this->config->get('defaulticonpath')); } } else { - $rawimage = file_get_contents(__DIR__ . '/../sites/icons/'.$icon); + $rawimage = file_get_contents($this->config->get('sitesdir').'/icons/'.$icon); } $mimetype = (new \finfo(FILEINFO_MIME_TYPE))->buffer($rawimage); return 'data:'.$mimetype.';base64,'.base64_encode($rawimage); diff --git a/jumpapp/classes/Sites.php b/jumpapp/classes/Sites.php index c3d1cb3..f7a9773 100644 --- a/jumpapp/classes/Sites.php +++ b/jumpapp/classes/Sites.php @@ -24,7 +24,7 @@ class Sites { public function __construct(Config $config, Cache $cache) { $this->config = $config; $this->loadedsites = []; - $this->sitesfilelocation = $this->config->get('sitesfile'); + $this->sitesfilelocation = $this->config->get('sitesdir').'/'.$this->config->get('sitesfile'); $this->cache = $cache; $this->load_sites_from_json(); } @@ -53,7 +53,7 @@ class Sites { throw new Exception('Provided sites json does not contain a top-level array'); } foreach ($decodedjson as $array) { - $sites[] = new Site($array); + $sites[] = new Site($this->config, $array); } return $sites; });