Use config in site class

This commit is contained in:
Dale Davies
2022-02-12 00:22:19 +00:00
parent c36934e4f6
commit a84354b74d
3 changed files with 11 additions and 7 deletions

View File

@@ -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',
];

View File

@@ -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);

View File

@@ -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;
});