<?php
namespace ticky;
class config {
public static $config = [];
public static function load($name) {
$includeFile = self::config_file_path($name);
$key = base64_encode($includeFile);
if (!isset(self::$config[$key])) {
self::$config[$key] = include $includeFile;
}
return self::$config[$key];
}
public static function get($name, $key = null) {
$config = self::load($name);
if (is_array($config) && $key) {
$value = isset($config[$key]) ? $config[$key] : "";
return $value;
}
return $config;
}
public static function all() {
return self::$config;
}
public static function set($name, $config) {
$oldconfig = self::config_file_path($name);
$pattern = $replacement = array();
foreach ($config as $k => $v) {
$pattern[$k] = "/'" . $k . "'\s*=>\s*([']?)[^']*([']?)(\s*),/is";
$replacement[$k] = "'" . $k . "' => \${1}" . $v . "\${2}\${3},";
}
$str = file_get_contents($oldconfig);
$str = preg_replace($pattern, $replacement, $str);
return file_put_contents($oldconfig, $str, LOCK_EX);
}
private static function config_file_path($name) {
$config_path = defined('CONFIG_PATH') ? CONFIG_PATH : "";
$config = defined('CONFIG') ? CONFIG : "";
$ticky_config = defined('TICKY_CONFIG') ? TICKY_CONFIG : "";
$model_name = defined('MODULE_NAME') ? MODULE_NAME : "";
$configFile = [
'0' => $name,
'1' => $config_path . DS . APP_ENV . DS . $name . '.php',
'2' => $config_path . DS . $name . '.php',
'3' => $config_path . DS . $name . '.php',
'4' => APP_PATH . DS . $model_name . DS . 'config' . DS . $name . '.php',
'5' => $config . DS . APP_ENV . DS . $name . '.php',
'6' => $config . DS . $name . '.php',
'7' => $config . $name . '.php',
'8' => $ticky_config . DS . $name . '.php',
];
if (!is_file($name)) {
foreach ($configFile as $x => $x_value) {
<br>';
if (is_file($x_value)) {
$includeFile = $x_value;
break;
}
}
} else {
$includeFile = $name;
}
if (!is_file($includeFile)) {
<br>', $configFile)
app::fatalerror('config does not exist : ' . $name, $includeFile . '<br/><br/>' . implode('<br/>', $configFile), 1);
}
return $includeFile;
}
}