Last active
August 20, 2017 10:52
-
-
Save millsoft/4c3f182509e6cbc4dfdfb8ad297c856f to your computer and use it in GitHub Desktop.
A simple Configuration Class for loading and saving to local config.json file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Configuration | |
*/ | |
class Conf{ | |
public static $configFile = "config.json"; | |
private static $currentConfig = array(); | |
public static function get($key, $default = null){ | |
if(!isset(self::$currentConfig[$key])){ | |
return $default; | |
} | |
return self::$currentConfig[$key]; | |
} | |
public static function set($key, $value){ | |
self::$currentConfig[$key] = $value; | |
self::save(); | |
} | |
/** | |
* Save current config from memory to config file | |
*/ | |
private static function save(){ | |
$json = json_encode(static::$currentConfig); | |
file_put_contents(static::$configFile, $json); | |
} | |
/** | |
* Load config file: | |
*/ | |
private static function load(){ | |
if(!file_exists(static::$configFile)){ | |
//create a new config file: | |
$emptyConf = json_encode(array()); | |
file_put_contents(static::$configFile, $emptyConf); | |
} | |
//read config file to array: | |
$C = json_decode(file_get_contents(static::$configFile), true); | |
static::$currentConfig = $C; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment