Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / config.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /*
4 A static config class to manage all of
5 our config params
6 */
7 class Config {
8
9 private static $items = array();
10 private static $cache = array();
11
12 /*
13 Load the default config file
14 */
15 public static function load($file) {
16 if(file_exists($file) === false) {
17 return false;
18 }
19
20 static::$items = array_merge(static::$items, require $file);
21
22 return true;
23 }
24
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;
28
29 $php .= 'return array(' . PHP_EOL;
30
31 foreach($array as $key => $value) {
32 $php .= "\t'" . $key . "' => ";
33
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;
38 }
39 $php .= "\t)," . PHP_EOL;
40 } else {
41 $php .= static::format($value) . ',' .PHP_EOL;
42 }
43 }
44
45 $php .= ');';
46
47 return file_put_contents(PATH . 'config.php', $php);
48 }
49
50 public static function format($value) {
51 if(is_int($value)) {
52 return $value;
53 }
54 if(is_bool($value)) {
55 return $value ? 'true' : 'false';
56 }
57 if(is_array($value)) {
58 $var = array_map(function($itm) {
59 return Config::format($itm);
60 }, $value);
61
62 return "array(" . implode(",", $var) . ")";
63 }
64 return "'" . (string) $value . "'";
65 }
66
67 /*
68 Set a config item
69 */
70 public static function set($key, $value) {
71 // array pointer for search
72 $array =& static::$items;
73
74 $keys = explode('.', $key);
75
76 while(count($keys) > 1) {
77 $key = array_shift($keys);
78
79 if(!isset($array[$key]) or !is_array($array[$key])) {
80 $array[$key] = array();
81 }
82
83 $array =& $array[$key];
84 }
85
86 $array[array_shift($keys)] = $value;
87 }
88
89 /*
90 Retrive a config param
91 */
92 public static function get($key = null, $default = false) {
93 // return all items
94 if(is_null($key)) return static::$items;
95
96 // copy array for search
97 $array = static::$items;
98
99 // search array
100 foreach(explode('.', $key) as $segment) {
101 if (!is_array($array) or array_key_exists($segment, $array) === false) {
102 return $default;
103 }
104
105 $array = $array[$segment];
106 }
107
108 return $array;
109 }
110
111 /*
112 Remove a config param
113 */
114 public static function forget($key) {
115 // array pointer for search
116 $array =& static::$items;
117
118 $keys = explode('.', $key);
119
120 while(count($keys) > 1) {
121 $key = array_shift($keys);
122
123 if(!isset($array[$key]) or !is_array($array[$key])) {
124 return;
125 }
126
127 $array =& $array[$key];
128 }
129
130 unset($array[array_shift($keys)]);
131 }
132 }