m1k1oblog/app/config.class.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2016-12-27 20:25:32 +00:00
<?php
defined('PROJECT_PATH') OR exit('No direct script access allowed');
2016-12-27 20:25:32 +00:00
class Config
{
2019-12-22 21:14:09 +00:00
const CONFIG = 'config.ini';
2019-12-26 18:32:17 +00:00
const CUSTOM = 'data/config.ini';
const CUSTOM_FALLBACK = 'custom.ini';
2020-06-11 00:32:07 +00:00
const ENV_PREFIX = 'BLOG_';
2019-12-22 21:14:09 +00:00
2016-12-31 13:07:26 +00:00
private static $_settings = null;
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
private static function init(){
2019-12-22 21:14:09 +00:00
$config_file = PROJECT_PATH.self::CONFIG;
2016-12-28 13:30:21 +00:00
if(!is_readable($config_file)){
2019-12-23 18:09:04 +00:00
throw new ConfigException('Cannot read config file.');
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
$default_settings = parse_ini_file($config_file);
if($default_settings === false){
2019-12-26 18:32:17 +00:00
throw new ConfigException('Cannot parse config file.');
}
$custom_settings = [];
if(is_readable($config_file = PROJECT_PATH.self::CUSTOM)){
2019-12-26 18:32:17 +00:00
$custom = parse_ini_file($config_file);
if($custom !== false){
$custom_settings = $custom;
2019-12-26 18:32:17 +00:00
}
}
2019-12-20 17:38:48 +00:00
2019-12-26 18:32:17 +00:00
// Fallback for legacy versions
elseif(is_readable($config_file = PROJECT_PATH.self::CUSTOM_FALLBACK)){
$custom = parse_ini_file($config_file);
2016-12-28 13:30:21 +00:00
if($custom !== false){
2019-12-26 18:32:17 +00:00
// Fallback for old direcotry structure
if(!array_key_exists('images_path', $custom) && !array_key_exists('thumbnails_path', $custom)){
$custom['images_path'] = 'i/';
$custom['thumbnails_path'] = 't/';
}
$custom_settings = array_merge($custom_settings, $custom);
2016-12-27 20:25:32 +00:00
}
}
2020-06-11 00:22:07 +00:00
// Fallback for versions, where mysql was default
if(!array_key_exists('db_connection', $custom_settings) && array_key_exists('mysql_user', $custom_settings) &&
(array_key_exists('mysql_socket', $custom_settings) || array_key_exists('mysql_host', $custom_settings))) {
$custom_settings['db_connection'] = 'mysql';
}
// Merge default and custom settings
self::$_settings = array_merge($default_settings, $custom_settings);
2020-06-11 00:22:07 +00:00
// From envs
$envs = getenv();
2020-06-11 00:34:10 +00:00
$env_prefix_len = strlen(self::ENV_PREFIX);
2020-06-11 00:22:07 +00:00
foreach($envs as $key => $value){
2020-06-11 00:32:07 +00:00
if(substr($key, 0, $env_prefix_len) === self::ENV_PREFIX){
$key = strtolower(substr($key, $env_prefix_len));
2020-06-11 00:22:07 +00:00
if($value === 'true'){
$value = true;
}
elseif($value === 'false'){
$value = false;
}
self::$_settings[$key] = $value;
}
}
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
public static function get($key){
2016-12-31 13:07:26 +00:00
if(self::$_settings === null){
2016-12-27 20:25:32 +00:00
self::init();
}
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
if(!array_key_exists($key, self::$_settings)){
2016-12-31 13:07:26 +00:00
throw new ConfigException(sprintf('Key "%s" not found in settings.', $key));
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
return self::$_settings[$key];
}
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
public static function get_safe($key, $default = ''){
2016-12-27 20:25:32 +00:00
try {
$value = self::get($key);
} catch (ConfigException $e) {
$value = $default;
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
return $value;
}
}
2016-12-31 13:07:26 +00:00
class ConfigException extends Exception {}