/** * 取得一个数据数据库连接对象 * * @param mix $params * @return object */ public static function get($db = 'default', $new = false) { if (empty($db)) { $db = 'default'; } $configs = Hi_Store::get('config')->db->{$db}; if (empty($configs)) { throw new Hi_Db_Exception('没有找到对应的数据库配置'); } foreach ($configs as $cs) { $params[] = get_object_vars($cs); } //为每个数据库链接分配key $key = 'db-' . md5(serialize($params)); //如果不强制新建连接,从连接缓存中选取 if (!$new) { $db = Hi_Store::get($key); if ($db instanceof Hi_Db_Adapter_Abstract) { return $db; } } try { if (!is_array($params)) { throw new Hi_Db_Exception('数据库配置错误'); } $adapter = ucwords($params[0]['type']); $adapterClass = "Hi_Db_Adapter_{$adapter}"; $adapter = new $adapterClass($params); Hi_Store::set($key, $adapter); return $adapter; } catch (Hi_Db_Exception $e) { throw $e; } }
/** * 初始化应用 * * @param string $path */ private function _setApp($path) { $this->_checkPath($path); $path .= DS; $cf = $path . 'config.xml'; $config = new Hi_Config($cf); Hi_Store::set('config', $config); $classPath = $path . DS . 'class'; Hi_Env::$PATH_APP = $path; Hi_Env::$PATH_CLASS = $classPath; $incPath = $classPath . PS . get_include_path(); set_include_path($incPath); if (isset($config->timezone)) { date_default_timezone_set($config->timezone); } Hi_Env::$ENTRY_FILE = $_SERVER['SCRIPT_FILENAME']; Hi_Env::$APPID = $config->appid; Hi_Env::$DEBUG = (bool) $config->debug; Hi_Env::$DEBUG = $config->debug == 'on' || $config->debug == 'true'; Hi_Env::$CACHEABLE = $config->cacheable != 'false' || !empty($config->cacheable); //设置全局的临时文件路径、缓存路径、日志路径 $tmpPath = $config->tmp; if (empty($tmpPath)) { $tmpPath = sys_get_temp_dir(); } $tmpPath = Hi_Tool_Dir::standard($tmpPath); if (substr($tmpPath, -1) != DS) { $tmpPath .= DS; } Hi_Env::$PATH_TMP = $tmpPath; $cachePath = $config->cache; if (empty($cachePath)) { $cachePath = $tmpPath . 'cache' . DS . Hi_Env::$APPID; } Hi_Tool_Dir::create($cachePath); Hi_Env::$PATH_CACHE = $cachePath; $logPath = $config->log; if (empty($logPath)) { $logPath = $tmpPath . 'logs'; } Hi_Env::$PATH_LOG = $logPath; }
/** * 构造一个缓存对象 * * @param string $type 缓存的类型,当前memcached和file可选 * @param array $options * @throws Hi_Cache_Exception */ public function __construct($type = 'file', $options = array()) { $ts = array('file', 'memcached'); $type = strtolower($type); if (!in_array($type, $ts, true)) { throw new Hi_Cache_Exception('The cache type is "file" or "memcached"'); } if ($type == 'memcached') { $options['appid'] = Hi_Env::$APPID; } $cacheClass = 'Hi_Cache_' . ucwords($type); $options['type'] = $type; $key = md5(serialize($options)); $this->_backend = Hi_Store::get($key); if ($this->_backend instanceof $cacheClass) { return; } $this->_backend = new $cacheClass($options); Hi_Store::set($key, $this->_backend); }
/** * 注册一个modifier * * @param string $modifier * @param array $callback */ public function createModifier($modifier, $callback) { if (!preg_match('/^[_a-z]([_a-z0-9])*$/i', $modifier)) { throw new Hi_View_Exception('modifier的名称由字母、数字和下划线组成,并且以下划线或者字母开头'); } if (!is_array($callback) || sizeof($callback) != 2) { throw new Hi_View_Exception('无效的callback'); } $key = 'Hi_View_Modifier_' . $modifier; Hi_Store::set($key, $callback); $func = 'smarty_modifier_' . $modifier; $meta = ' function ' . $func . '(){ $modifier = Hi_Store::get("' . $key . '"); $args = func_get_args(); return call_user_func_array($modifier, $args); } '; eval($meta); $this->register_modifier($modifier, $func); }
public function __construct($action) { $this->_config = Hi_Store::get('config'); $this->_action = $action; }