Files
Jump/jumpapp/classes/Config.php

151 lines
4.6 KiB
PHP
Raw Normal View History

2022-02-04 09:53:55 +00:00
<?php
2022-07-19 16:38:55 +01:00
/**
* ██ ██ ██ ███ ███ ██████
* ██ ██ ██ ████ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██████
* ██ ██ ██ ██ ██ ██ ██ ██
* █████ ██████ ██ ██ ██
*
* @author Dale Davies <dale@daledavies.co.uk>
* @copyright Copyright (c) 2022, Dale Davies
* @license MIT
*/
2022-02-04 09:53:55 +00:00
namespace Jump;
use Jump\Exceptions\ConfigException;
2022-02-04 09:53:55 +00:00
2022-02-04 11:52:57 +00:00
/**
* Load, parse and enumerate all configuration paramaters requires throughout
* the application. Validates the config.php on load to ensure required params
* are all present.
*
* Provides a simple interface for retriving config paramaters once initialised.
*
* @author Dale Davies <dale@daledavies.co.uk>
* @license MIT
*/
2022-02-04 09:53:55 +00:00
class Config {
private \PHLAK\Config\Config $config;
/**
* Required files and directories need that should not be configurable.
*/
private const BASE_APPLICATION_PATHS = [
'backgroundsdir' => '/assets/backgrounds',
2022-02-12 00:22:19 +00:00
'defaulticonpath' => '/assets/images/default-icon.png',
2022-05-28 20:42:56 +01:00
'searchenginesfile' => '/search/searchengines.json',
2022-02-12 00:22:19 +00:00
'sitesdir' => '/sites',
'sitesfile' => '/sites/sites.json',
2022-02-04 09:53:55 +00:00
'templatedir' => '/templates',
'translationsdir' => '/translations'
2022-02-04 09:53:55 +00:00
];
/**
* Configurable params we do expect to find in config.php
*/
private const CONFIG_PARAMS = [
'sitename',
'showclock',
'metrictemp',
2022-02-04 09:53:55 +00:00
'wwwroot',
'cachebypass',
'cachedir',
'noindex'
2022-02-04 09:53:55 +00:00
];
2022-04-13 16:28:12 +01:00
/**
* Session config params.
*/
private const CONFIG_SESSION = [
'sessionname' => 'JUMP',
'sessiontimeout' => '10 minutes'
];
2022-02-04 09:53:55 +00:00
public function __construct() {
$this->config = new \PHLAK\Config\Config(__DIR__.'/../config.php');
$this->add_wwwroot_to_base_paths();
2022-04-13 16:28:12 +01:00
$this->add_session_config();
2022-02-04 09:53:55 +00:00
if ($this->config_params_missing()) {
throw new ConfigException('Config.php must always contain... '.implode(', ', self::CONFIG_PARAMS));
2022-02-04 09:53:55 +00:00
}
}
/**
* Prefixes the wwwroot string from config.php to the base application paths
* so they can be located in the file system correctly.
*
* @return void
*/
private function add_wwwroot_to_base_paths(): void {
$wwwroot = $this->config->get('wwwroot');
foreach(self::BASE_APPLICATION_PATHS as $key => $value) {
$this->config->set($key, $wwwroot.$value);
}
}
2022-04-13 16:28:12 +01:00
private function add_session_config(): void {
foreach(self::CONFIG_SESSION as $key => $value) {
$this->config->set($key, $value);
}
}
2022-02-04 09:53:55 +00:00
/**
* Determine if any configuration params are missing in the list loaded
* from the config.php.
*
* @return boolean
*/
private function config_params_missing(): bool {
return !!array_diff(
array_merge(
array_keys(self::BASE_APPLICATION_PATHS),
self::CONFIG_PARAMS
),
array_keys($this->config->toArray()),
);
2022-02-04 09:53:55 +00:00
}
/**
* Retrieves the config parameter provided in $key, first checks for its
* existence.
*
2023-04-14 11:44:05 +01:00
* @param string $key The requested config parameter key, not case sensitive.
* @param bool $strict Throw exception if requested param is not found, or return null.
2022-02-04 09:53:55 +00:00
* @return mixed The selected value from the configuration array.
*/
public function get(string $key, $strict = true): mixed {
2023-04-14 11:44:05 +01:00
$key = strtolower($key);
if (!$this->config->has($key) && $strict === true) {
throw new ConfigException('Config key does not exist... ('.$key.')');
2022-02-04 09:53:55 +00:00
}
2023-03-10 08:35:21 +00:00
return trim($this->config->get($key));
2022-02-04 09:53:55 +00:00
}
/**
* Get all config paramaters and values as an array.
*
* @return array Multidimensional array of config params.
*/
public function get_all(): array {
return $this->config->toArray();
}
2022-02-04 09:53:55 +00:00
/**
* Attempt to converts a string to a boolean correctly, will return the parsed boolean
* or null on failure.
*
* @param mixed $input A string representing a boolean value... "true", "yes", "no", "false" etc.
* @return mixed Returns a proper boolean or null on failure.
*/
public function parse_bool(mixed $input): mixed {
return filter_var($input,FILTER_VALIDATE_BOOLEAN,FILTER_NULL_ON_FAILURE);
}
public function get_wwwurl() {
return rtrim($this->config->get('wwwurl', false), '/');
}
2022-02-04 09:53:55 +00:00
}