This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Config.php

107 lines
2.9 KiB
PHP
Raw Normal View History

2015-04-01 15:38:04 +00:00
<?php
/*
* Configuration Management
*/
namespace Sakura;
/**
* Class Config
* @package Sakura
*/
class Config
{
2015-04-01 15:38:04 +00:00
// Configuration data
private static $local = [];
private static $database = [];
2015-04-01 15:38:04 +00:00
2015-06-29 00:36:37 +00:00
// Initialise configuration, does not contain database initialisation because explained below
public static function init($local)
{
2015-04-01 15:38:04 +00:00
2015-05-29 19:27:45 +00:00
// Check if the configuration file exists
if (!file_exists($local)) {
2015-05-29 19:27:45 +00:00
trigger_error('Local configuration file does not exist', E_USER_ERROR);
2015-06-27 19:29:37 +00:00
}
2015-05-29 19:27:45 +00:00
// Attempt to load the configuration file
$local = parse_ini_file($local, true);
// Check if $local is an array and then store it in $local
if (is_array($local)) {
self::$local = $local;
2015-06-27 19:29:37 +00:00
} else {
// Otherwise trigger an error
2015-09-14 21:41:43 +00:00
trigger_error(
'Failed to load local configuration file,' .
' check the structure of the file to see if you made mistake somewhere',
E_USER_ERROR
);
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
}
/*
* Initialise Database configuration values.
* Different from init as that is called before the database connection is initially
* established.
*/
public static function initDB()
{
2015-04-01 15:38:04 +00:00
// Get config table from the database
$_DATA = Database::fetch('config', true);
// Create variable to temporarily store values in
$_DBCN = [];
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
// Properly sort the values
foreach ($_DATA as $_CONF) {
2015-08-19 02:37:45 +00:00
$_DBCN[$_CONF['config_name']] = $_CONF['config_value'];
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
// Assign the temporary array to the static one
self::$database = $_DBCN;
2015-04-01 15:38:04 +00:00
}
2015-06-29 00:36:37 +00:00
// Get values from the configuration on the file system
2015-12-04 14:19:10 +00:00
public static function local($key, $subkey = null)
{
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
if (array_key_exists($key, self::$local)) {
if ($subkey) {
2015-06-27 19:29:37 +00:00
// If we also have a subkey return the proper data
return self::$local[$key][$subkey];
2015-06-27 19:29:37 +00:00
}
// else we just return the default value
return self::$local[$key];
2015-06-27 19:29:37 +00:00
}
// If it doesn't exist trigger an error to avoid explosions
2015-09-14 21:41:43 +00:00
trigger_error(
'Unable to get local configuration value "' . $key . '"',
E_USER_ERROR
);
2015-12-04 14:19:10 +00:00
return null;
2015-06-29 00:36:37 +00:00
}
2015-04-01 15:38:04 +00:00
2015-06-29 00:36:37 +00:00
// Get values from the configuration in the database
2015-12-04 14:19:10 +00:00
public static function get($key, $returnNull = false)
{
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
if (array_key_exists($key, self::$database)) {
2015-06-27 19:29:37 +00:00
// Then return the value
return self::$database[$key];
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
// Then return the value
2015-09-14 21:41:43 +00:00
trigger_error(
'Unable to get configuration value "' . $key . '"',
E_USER_ERROR
);
2015-12-04 14:19:10 +00:00
return null;
}
2015-04-01 15:38:04 +00:00
}