1 | <?php |
||
25 | class Config { |
||
26 | /// Properties /// |
||
27 | |||
28 | /** |
||
29 | * @var array The config data. |
||
30 | */ |
||
31 | protected static $data = []; |
||
32 | |||
33 | /** |
||
34 | * @var string The default path to load/save to. |
||
35 | */ |
||
36 | protected static $defaultPath; |
||
37 | |||
38 | /// Methods /// |
||
39 | |||
40 | /** |
||
41 | * Get or set the default path. |
||
42 | * |
||
43 | * @param string $value Pass a value to set a new default path. |
||
44 | * @return string Returns the current default path. |
||
45 | */ |
||
46 | public static function defaultPath($value = '') { |
||
54 | |||
55 | /** |
||
56 | * Return all of the config data. |
||
57 | * |
||
58 | * @return array Returns an array of config data. |
||
59 | */ |
||
60 | public static function data() { |
||
63 | |||
64 | /** |
||
65 | * Get a setting from the config. |
||
66 | * |
||
67 | * @param string $key The config key. |
||
68 | * @param mixed $default The default value if the config file doesn't exist. |
||
69 | * @return mixed The value at {@link $key} or {@link $default} if the key isn't found. |
||
70 | * @see \config() |
||
71 | */ |
||
72 | public static function get($key, $default = null) { |
||
79 | |||
80 | /** |
||
81 | * Encode an array in ini format. |
||
82 | * The resulting array will work with parse_ini_file() and parse_ini_string(). |
||
83 | * |
||
84 | * @param array $data A flat, associative array of data. |
||
85 | * @return string The data in ini format. |
||
86 | */ |
||
87 | // public static function iniEncode($data) { |
||
88 | // ksort($data, SORT_NATURAL | SORT_FLAG_CASE); |
||
89 | // |
||
90 | // $result = ''; |
||
91 | // |
||
92 | // $lastSection = null; |
||
93 | // |
||
94 | // foreach ($data as $key => $value) { |
||
95 | // $section = trim(strstr($key, '.', true), '.'); |
||
96 | // |
||
97 | // if ($section !== $lastSection) { |
||
98 | // if ($section) { |
||
99 | // $result .= "\n[$section]\n"; |
||
100 | // } |
||
101 | // $lastSection = $section; |
||
102 | // } |
||
103 | // |
||
104 | // $result .= $key . ' = '; |
||
105 | // |
||
106 | // if (is_bool($value)) { |
||
107 | // $str = $value ? 'true' : 'false'; |
||
108 | // } elseif (is_numeric($value)) { |
||
109 | // $str = $value; |
||
110 | // } elseif (is_string($value)) { |
||
111 | // $str = '"' . addcslashes($value, "\"") . '"'; |
||
112 | // } |
||
113 | // $result .= $str . "\n"; |
||
114 | // } |
||
115 | // |
||
116 | // return $result; |
||
117 | // } |
||
118 | |||
119 | /** |
||
120 | * Load configuration data from a file. |
||
121 | * |
||
122 | * @param string $path An optional path to load the file from. |
||
123 | * @param string $path If true the config will be put under the current config, not over it. |
||
124 | * @param string $php_var The name of the php variable to load from if using the php file type. |
||
125 | */ |
||
126 | public static function load($path = '', $underlay = false, $php_var = 'config') { |
||
147 | |||
148 | /** |
||
149 | * Save data to the config file. |
||
150 | * |
||
151 | * @param array $data The config data to save. |
||
152 | * @param string $path An optional path to save the data to. |
||
153 | * @param string $php_var The name of the php variable to load from if using the php file type. |
||
154 | * @return bool Returns true if the save was successful or false otherwise. |
||
155 | * @throws \InvalidArgumentException Throws an exception when the saved data isn't an array. |
||
156 | */ |
||
157 | public static function save($data, $path = null, $php_var = 'config') { |
||
180 | } |
||
181 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: