是否可以使用phalcon重写配置参数?我使用INI文件类型。如果不是 - 告诉我如何实施。
如果要使用php创建INI文件,则可能,甚至w/o phalcon。
来自PHP文档评论:
读取文件:$ ini = ini :: read('myfile.ini');写文件:ini ::写('myfile.ini',$ ini);
自定义INI类功能:
- 支持[]数组的语法
- 支持。在bar.foo.foo.something = value 之类的钥匙中
- true and false字符串将在布尔值中自动转换
- 整数字符串自动在整数中自动转换
- 编写时按键进行排序
常数被替换,但应写入括号之间的INI文件中:{myconstant}
class INI { /** * WRITE */ static function write($filename, $ini) { $string = ''; foreach(array_keys($ini) as $key) { $string .= '['.$key."]n"; $string .= INI::write_get_string($ini[$key], '')."n"; } file_put_contents($filename, $string); } /** * write get string */ static function write_get_string(& $ini, $prefix) { $string = ''; ksort($ini); foreach($ini as $key => $val) { if (is_array($val)) { $string .= INI::write_get_string($ini[$key], $prefix.$key.'.'); } else { $string .= $prefix.$key.' = '.str_replace("n", "\n", INI::set_value($val))."n"; } } return $string; } /** * manage keys */ static function set_value($val) { if ($val === true) { return 'true'; } else if ($val === false) { return 'false'; } return $val; } /** * READ */ static function read($filename) { $ini = array(); $lines = file($filename); $section = 'default'; $multi = ''; foreach($lines as $line) { if (substr($line, 0, 1) !== ';') { $line = str_replace("r", "", str_replace("n", "", $line)); if (preg_match('/^[(.*)]/', $line, $m)) { $section = $m[1]; } else if ($multi === '' && preg_match('/^([a-z0-9_.[]-]+)s*=s*(.*)$/i', $line, $m)) { $key = $m[1]; $val = $m[2]; if (substr($val, -1) !== "\") { $val = trim($val); INI::manage_keys($ini[$section], $key, $val); $multi = ''; } else { $multi = substr($val, 0, -1)."n"; } } else if ($multi !== '') { if (substr($line, -1) === "\") { $multi .= substr($line, 0, -1)."n"; } else { INI::manage_keys($ini[$section], $key, $multi.$line); $multi = ''; } } } } $buf = get_defined_constants(true); $consts = array(); foreach($buf['user'] as $key => $val) { $consts['{'.$key.'}'] = $val; } array_walk_recursive($ini, array('INI', 'replace_consts'), $consts); return $ini; } /** * manage keys */ static function get_value($val) { if (preg_match('/^-?[0-9]$/i', $val)) { return intval($val); } else if (strtolower($val) === 'true') { return true; } else if (strtolower($val) === 'false') { return false; } else if (preg_match('/^"(.*)"$/i', $val, $m)) { return $m[1]; } else if (preg_match('/^'(.*)'$/i', $val, $m)) { return $m[1]; } return $val; } /** * manage keys */ static function get_key($val) { if (preg_match('/^[0-9]$/i', $val)) { return intval($val); } return $val; } /** * manage keys */ static function manage_keys(& $ini, $key, $val) { if (preg_match('/^([a-z0-9_-]+).(.*)$/i', $key, $m)) { INI::manage_keys($ini[$m[1]], $m[2], $val); } else if (preg_match('/^([a-z0-9_-]+)[(.*)]$/i', $key, $m)) { if ($m[2] !== '') { $ini[$m[1]][INI::get_key($m[2])] = INI::get_value($val); } else { $ini[$m[1]][] = INI::get_value($val); } } else { $ini[INI::get_key($key)] = INI::get_value($val); } } /** * replace utility */ static function replace_consts(& $item, $key, $consts) { if (is_string($item)) { $item = strtr($item, $consts); } } }
在这里了解更多信息:http://lt1.php.net/parse_ini_file