/** * 连接缓存 * @access public * @param string $type 缓存类型 * @param array $options 配置数组 * @return object */ public function connect($type = '', $options = array()) { if (empty($type)) { $type = Config::get('cache.data_cache_type'); } $class = strpos($type, '\\') ? $type : 'Core\\cache\\' . ucwords(strtolower($type)); if (class_exists($class)) { $cache = new $class($options); } else { exit('class :' . $class . ' not exists '); } // throw new \Exception('_CACHE_TYPE_INVALID_', 0,0); //TODO E(L('_CACHE_TYPE_INVALID_').':'.$type); return $cache; }
/** * 数据库连接参数解析 * @static * @access private * @param mixed $config * @return array */ private static function parseConfig($config) { if (!empty($config)) { if (is_string($config)) { return self::parseDsn($config); } $config = array_change_key_case($config); $config = array('type' => $config['db_type'], 'username' => $config['db_user'], 'password' => $config['db_pwd'], 'hostname' => $config['db_host'], 'hostport' => $config['db_port'], 'database' => $config['db_name'], 'dsn' => isset($config['db.db_dsn']) ? $config['db.db_dsn'] : null, 'params' => isset($config['db.db_params']) ? $config['db.db_params'] : null, 'charset' => isset($config['db.db_charset']) ? $config['db.db_charset'] : 'utf8', 'deploy' => isset($config['db.db_deploy_type']) ? $config['db.db_deploy_type'] : 0, 'rw_separate' => isset($config['db.db_rw_separate']) ? $config['db.db_rw_separate'] : false, 'master_num' => isset($config['db.db_master_num']) ? $config['db.db_master_num'] : 1, 'slave_no' => isset($config['db.db_slave_no']) ? $config['db.db_slave_no'] : '', 'debug' => isset($config['db.db_debug']) ? $config['db.db_debug'] : APP_DEBUG, 'lite' => isset($config['db.db_lite']) ? $config['db.db_lite'] : false); } else { $config = array('type' => Config::get('db.db_type'), 'username' => Config::get('db.db_username'), 'password' => Config::get('db.db_password'), 'hostname' => Config::get('db.db_host'), 'hostport' => Config::get('db.db_port'), 'database' => Config::get('db.db_name'), 'dsn' => Config::get('db.db_dsn'), 'params' => Config::get('db.db_params'), 'charset' => Config::get('db.db_charset'), 'deploy' => Config::get('db.db_deploy_type'), 'rw_separate' => Config::get('db.db_rw_separate'), 'master_num' => Config::get('db.db_master_num'), 'slave_no' => Config::get('db.db_slave_no'), 'debug' => DEBUG, 'lite' => Config::get('db_lite')); } //dump($config); return $config; }
public function __construct() { $suffix = Config::get('url.url_suffix'); // 资源类型检测 if ('' == $suffix) { $this->_type = 'json'; } elseif (!in_array($suffix, $this->allowType)) { $this->_type = 'json'; } // 请求方式检测 $method = strtolower(REQUEST_METHOD); if (!in_array($method, $this->allowMethod)) { $method = $this->defaultMethod; } $this->_method = $method; }
protected function _initEnv() { //时区设置,默认为中国(北京时区) date_default_timezone_set(Config::get('setting.timezone')); //设置异常处理 set_exception_handler(array($this, '_exception')); //关闭魔术变量,提高PHP运行效率 if (get_magic_quotes_runtime()) { @set_magic_quotes_runtime(0); } if (DEBUG) { ini_set('display_errors', 'on'); //正式环境关闭错误输出 if (substr(PHP_VERSION, 0, 3) == "5.3") { error_reporting(~E_WARNING & ~E_NOTICE & E_ALL & ~E_DEPRECATED); } else { error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); } } else { error_reporting(0); } }
/** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param int $expire 有效时间 0为永久 * @return boolean */ public function set($name, $value, $expire = null) { //TODO N('cache_write',1); if (is_null($expire)) { $expire = $this->options['expire']; } $filename = $this->filename($name); $data = serialize($value); if (Config::get('data_cache_compress') && function_exists('gzcompress')) { //数据压缩 $data = gzcompress($data, 3); } if (Config::get('data_cache_check')) { //开启数据校验 $check = md5($data); } else { $check = ''; } $data = "<?php\n//" . sprintf('%012d', $expire) . $check . $data . "\n?>"; $result = file_put_contents($filename, $data); if ($result) { if ($this->options['length'] > 0) { // 记录缓存队列 //TODO $this->queue($name); } clearstatcache(); return true; } else { return false; } }
/** * 切换当前的数据库连接 * @access public * @param integer $linkNum 连接序号 * @param mixed $config 数据库连接信息 * @param boolean $force 强制重新连接 * @return Model */ public function db($linkNum = '', $config = '', $force = false) { if ('' === $linkNum && $this->db) { return $this->db; } if (!isset($this->_db[$linkNum]) || $force) { // 创建一个新的实例 if (!empty($config) && is_string($config) && false === strpos($config, '/')) { // 支持读取配置参数 $config = Config::get($config); } $this->_db[$linkNum] = \Core\Db::getInstance($config); } elseif (NULL === $config) { $this->_db[$linkNum]->close(); // 关闭数据库连接 unset($this->_db[$linkNum]); return; } // 切换数据库连接 $this->db = $this->_db[$linkNum]; $this->_after_db(); // 字段检测 if (!empty($this->name) && $this->autoCheckFields) { $this->_checkTableInfo(); } return $this; }