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

149 lines
4 KiB
PHP
Raw Normal View History

2015-04-01 15:38:04 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the configuration manager.
2016-02-20 11:20:58 +00:00
*
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
2015-04-01 15:38:04 +00:00
namespace Sakura;
/**
2016-02-02 21:04:15 +00:00
* Handles both the local and database stored configuration sides of Sakura.
2016-02-20 11:20:58 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Config
{
2016-02-02 21:04:15 +00:00
/**
* Container for the parsed local configuration.
2016-02-20 11:20:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var array
*/
private static $local = [];
2016-02-02 21:04:15 +00:00
/**
2016-02-15 21:20:46 +00:00
* Cache for the configuration stored in the database.
2016-02-20 11:20:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var array
*/
private static $database = [];
2015-04-01 15:38:04 +00:00
2016-02-02 21:04:15 +00:00
/**
* Initialiser, parses the local configuration.
2016-02-20 11:20:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param string $local Path to the configuration file.
*/
public static function init($local)
{
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
}
2016-02-02 21:04:15 +00:00
/**
* Get a value from the local configuration file.
2016-02-20 11:20:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param string $key Configuration section.
* @param string $subkey Configuration key.
2016-02-20 11:20:58 +00:00
*
2016-02-18 23:28:44 +00:00
* @return array|string Configuration value.
2016-02-02 21:04:15 +00:00
*/
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
2016-02-02 21:04:15 +00:00
/**
* Get a configuration value from the database.
2016-02-20 11:20:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param string $key Configuration key.
2016-02-20 11:20:58 +00:00
* @param string $default Value that gets used when the value doesn't exist.
*
2016-02-02 21:04:15 +00:00
* @return string Configuration value.
*/
2016-02-20 11:20:58 +00:00
public static function get($key, $default = 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::$database)) {
2015-06-27 19:29:37 +00:00
// Then return the value
return self::$database[$key];
2016-02-15 21:20:46 +00:00
} else {
2016-02-25 16:06:29 +00:00
// Get the record from the database
$value = DB::table('config')
->where('config_name', $key)
->get();
// Check if it exists
2016-02-15 21:20:46 +00:00
if ($value) {
2016-02-25 16:06:29 +00:00
self::$database[$key] = $value[0]->config_value;
2016-02-15 21:20:46 +00:00
return self::$database[$key];
}
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
2016-02-20 11:20:58 +00:00
// If we fell all the way down here set the bundled default value
Config::set($key, $default);
// And then return default that value
return $default;
}
2016-02-18 23:28:44 +00:00
public static function set($key, $value)
{
// Unset the cached copy
if (array_key_exists($key, self::$database)) {
unset(self::$database[$key]);
}
// Check if the value already exists
2016-02-25 16:06:29 +00:00
$exists = DB::table('config')
->where('config_name', $key)
->count();
2016-02-18 23:28:44 +00:00
// If it exists run an update
2016-02-25 16:06:29 +00:00
if ($exists) {
DB::table('config')
->where('config_name', $key)
->update(['config_value' => $value]);
2016-02-18 23:28:44 +00:00
} else {
2016-02-25 16:06:29 +00:00
DB::table('config')
->insert(['config_name' => $key, 'config_value' => $value]);
2016-02-18 23:28:44 +00:00
}
// Return the value
return $value;
}
2015-04-01 15:38:04 +00:00
}