rhymix/common/framework/config.php
2018-12-13 18:59:27 +09:00

184 lines
4.1 KiB
PHP

<?php
namespace Rhymix\Framework;
/**
* The config class.
*/
class Config
{
/**
* System configuration is stored here.
*/
protected static $_config = array();
/**
* Location of configuration files.
*/
public static $config_filename = 'files/config/config.php';
public static $old_db_config_filename = 'files/config/db.config.php';
public static $old_ftp_config_filename = 'files/config/ftp.config.php';
public static $old_lang_config_filename = 'files/config/lang_selected.info';
public static $default_config_filename = 'common/defaults/config.php';
/**
* Load system configuration.
*
* @return void
*/
public static function init()
{
if (file_exists(\RX_BASEDIR . self::$config_filename))
{
ob_start();
self::$_config = (include \RX_BASEDIR . self::$config_filename);
ob_end_clean();
}
else
{
if (self::$_config = Parsers\ConfigParser::convert())
{
self::save();
}
}
return self::$_config;
}
/**
* Get all system configuration.
*
* @return array
*/
public static function getAll()
{
return self::$_config;
}
/**
* Get default system configuration.
*
* @return array
*/
public static function getDefaults()
{
return (include \RX_BASEDIR . self::$default_config_filename);
}
/**
* Get a system configuration value.
*
* @param string $key
* @return mixed
*/
public static function get($key)
{
if (!count(self::$_config))
{
self::init();
}
$data = self::$_config;
$key = explode('.', $key);
foreach ($key as $step)
{
if ($key === '' || !isset($data[$step]))
{
return null;
}
$data = $data[$step];
}
return $data;
}
/**
* Set a system configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function set($key, $value)
{
if (!count(self::$_config))
{
self::init();
}
$data = &self::$_config;
$key = explode('.', $key);
foreach ($key as $step)
{
$data = &$data[$step];
}
$data = $value;
}
/**
* Set all system configuration.
*
* @param array $config
* @return void
*/
public static function setAll($config)
{
self::$_config = $config;
}
/**
* Save the current system configuration.
*
* @param array $config (optional)
* @return bool
*/
public static function save($config = null)
{
if ($config)
{
self::setAll($config);
}
// Save the main config file.
$buff = '<?php' . "\n" . '// Rhymix System Configuration' . "\n" . 'return ' . self::serialize(self::$_config) . ';' . "\n";
$result = Storage::write(\RX_BASEDIR . self::$config_filename, $buff) ? true : false;
// Save XE-compatible config files.
$db_info = \Context::convertDBInfo(self::$_config);
$ftp_info = $db_info->ftp_info;
$db_info_without_ftp = clone $db_info;
unset($db_info_without_ftp->ftp_info);
$warning = '// DO NOT EDIT THIS FILE. CHANGES WILL NOT BE APPLIED.' . "\n" . '// TO CHANGE RHYMIX SYSTEM CONFIGURATION, EDIT config.php INSTEAD.';
$buff = '<?php' . "\n\n" . $warning . "\n\n" . '$db_info = ' . self::serialize($db_info_without_ftp) . ';' . "\n";
Storage::write(\RX_BASEDIR . self::$old_db_config_filename, $buff);
$buff = '<?php' . "\n\n" . $warning . "\n\n" . '$ftp_info = ' . self::serialize($ftp_info) . ';' . "\n";
Storage::write(\RX_BASEDIR . self::$old_ftp_config_filename, $buff);
return $result;
}
/**
* Serialize a value for insertion into a PHP-based configuration file.
*
* @param mixed $value
* @return string
*/
public static function serialize($value)
{
if (is_object($value))
{
return '(object)' . self::serialize((array)$value);
}
elseif (is_array($value))
{
$value = var_export($value, true);
$value = preg_replace('/array \(\n/', "array(\n", $value);
$value = preg_replace('/=>\s+array\(\n/', "=> array(\n", $value);
$value = preg_replace('/array\(\s*\n\s*\)/', 'array()', $value);
$value = preg_replace_callback('/\n(\x20+)/', function($m) {
return "\n" . str_repeat("\t", intval(strlen($m[1]) / 2));
}, $value);
$value = preg_replace('/\n(\t+)[0-9]+ => /', "\n\$1", $value);
return $value;
}
else
{
return var_export($value, true);
}
}
}