1 <?php
defined('IN_CMS') or die('No direct access allowed.');
4 A static config class to manage all of
9 private static $items = array();
10 private static $cache = array();
13 Load the default config file
15 public static function load($file) {
16 if(file_exists($file) === false) {
20 static::$items = array_merge(static::$items, require $file);
25 public static function write($file, $array) {
26 $php = '<?php defined(\'IN_CMS\') or die(\'No direct access allowed.\');' . PHP_EOL
. PHP_EOL
;
27 $php .= '/**' . PHP_EOL
. "\t" . 'Auto generated config' . PHP_EOL
. '*/' . PHP_EOL
. PHP_EOL
;
29 $php .= 'return array(' . PHP_EOL
;
31 foreach($array as $key => $value) {
32 $php .= "\t'" . $key . "' => ";
34 if(is_array($value)) {
35 $php .= 'array(' . PHP_EOL
;
36 foreach($value as $k => $v) {
37 $php .= "\t\t'" . $k . "' => " . static::format($v) . "," .PHP_EOL
;
39 $php .= "\t)," . PHP_EOL
;
41 $php .= static::format($value) . ',' .PHP_EOL
;
47 return file_put_contents(PATH
. 'config.php', $php);
50 public static function format($value) {
55 return $value ?
'true' : 'false';
57 if(is_array($value)) {
58 $var = array_map(function($itm) {
59 return Config
::format($itm);
62 return "array(" . implode(",", $var) . ")";
64 return "'" . (string) $value . "'";
70 public static function set($key, $value) {
71 // array pointer for search
72 $array =& static::$items;
74 $keys = explode('.', $key);
76 while(count($keys) > 1) {
77 $key = array_shift($keys);
79 if(!isset($array[$key]) or !is_array($array[$key])) {
80 $array[$key] = array();
83 $array =& $array[$key];
86 $array[array_shift($keys)] = $value;
90 Retrive a config param
92 public static function get($key = null, $default = false) {
94 if(is_null($key)) return static::$items;
96 // copy array for search
97 $array = static::$items;
100 foreach(explode('.', $key) as $segment) {
101 if (!is_array($array) or array_key_exists($segment, $array) === false) {
105 $array = $array[$segment];
112 Remove a config param
114 public static function forget($key) {
115 // array pointer for search
116 $array =& static::$items;
118 $keys = explode('.', $key);
120 while(count($keys) > 1) {
121 $key = array_shift($keys);
123 if(!isset($array[$key]) or !is_array($array[$key])) {
127 $array =& $array[$key];
130 unset($array[array_shift($keys)]);