/** * 刪除 cache 資料 * @access public * @return boolean * @since 2011-09-05 */ public function delete($key) { if ($key == '') { log::errorLog('$key is empty'); return false; } if (!$this->_memcache->delete($key, 0)) { log::errorLog('memcacheLayer delete() failed'); return false; } log::accessLog('memcacheLayer delete() successful'); log::returnLog(true); return true; }
/** * load config from db * @access public * @static * @return boolean * @example * <p> * config::loadDbConfig(); * </p> */ public static function loadDbConfig() { $cacheConfig = self::get('cache'); if ($cacheConfig) { //从cache中读取dbConfig,如中没有命中,则继续从DB中读取 cacheLayer::setConfig($cacheConfig); $dbConfigCacheKey = cacheLayer::getKey('co', 'platform', 'config', 'loadDbConfig'); $dbConfig = cacheLayer::read($dbConfigCacheKey); if ($dbConfig) { self::$_config = array_merge_recursive(self::$_config, $dbConfig); log::accessLog('Execute loadDbConfig() successful(from cache)'); log::returnLog(true); return true; } } $mongoConfig = self::get('database.default.params'); if (is_array($mongoConfig) && count($mongoConfig)) { //从mongo Db中读取config $accessMongo = new accessMongo($mongoConfig); $accessMongo->connect(); $dbo = $accessMongo->getDb(); $connection = $dbo->selectCollection('sg_configuration'); $configures = iterator_to_array($connection->find(array('published' => 1), array('name', 'value', 'default_value'))); $config = array(); foreach ($configures as $configure) { if (is_numeric($configure['default_value'])) { if (!is_numeric($configure['value'])) { $config[$configure['name']] = $configure['default_value']; } else { $config[$configure['name']] = $configure['value']; } } else { if (empty($configure['value'])) { $config[$configure['name']] = $configure['default_value']; } else { $config[$configure['name']] = $configure['value']; } } } if (isset($dbConfigCacheKey)) { cacheLayer::write($dbConfigCacheKey, $config); } self::$_config = array_merge_recursive(self::$_config, $config); log::accessLog('Execute loadDbConfig() successful(from mongo db)'); log::returnLog(true); return true; } else { log::errorLog('mongo config error'); return false; } }
/** * 取得資料表裡的其中一個欄位的所有內容. * @access public * @link find() * @return array * @example $obj->findCol() ; */ public function findCol($table, $field, $conditions = null, $limit = null, $limitStart = 0, $sorts = null) { if (!$field) { log::errorLog('$field is empty'); return false; } $colArr = array(); $arr = $this->find($table, $field, $conditions, $limit, $limitStart, $sorts); if ($arr === false) { log::errorLog('execute $this->find() fail'); return false; } else { foreach ($arr as $ak => $av) { $colArr[] = $av[$field]; } } log::accessLog('execute findCol success'); log::returnLog($colArr); return $colArr; }
/** * 刪除 cache 資料 * @access public * @return string * @since 2011-09-08 */ public function delete($key) { if (!$key) { log::errorLog('$key is empty'); return false; } //convert key to file. sha1 has 1/2^51 collision possibility. $fileSeed = sha1($key); $dir1 = substr($fileSeed, 0, 2); $dir2 = substr($fileSeed, 2, 2); $cacheFile = $this->_cacheFileTopDirectory . "{$dir1}/{$dir2}/{$fileSeed}"; $cacheExpirationFile = "{$cacheFile}_expirationTime"; if (!file_exists($cacheFile)) { return false; } if (unlink($cacheFile) && unlink($cacheExpirationFile)) { log::accessLog('fileCache delete() success'); log::returnLog($str); return true; } else { log::errorLog('fileCache delete() failed'); return false; } }
/** * 检查变量是否存在 * @access public * @static * @link _isset * @param $key String 变量名 * @return Boolean * @example registry::_isset('foo'); */ public static function _isset($key) { $flag = false; if (isset(self::$_var[$key])) { $flag = true; } if (self::$_enableLog) { if ($flag) { log::accessLog('access object of registry'); log::returnLog('isset(' . $key . ') return true'); } else { log::returnLog('isset(' . $key . ') return false'); } } return $flag; }
/** * 分析一个视图文件并返回结果 * * @param string $filename * @param string $view_id * @param array $inherited_stack * * @return string */ public function parse($filename, $view_id = null, array $inherited_stack = null, $default = null, $enableLayout = true) { if (!$view_id) { $view_id = mt_rand(); } $stack = array('id' => $view_id, 'contents' => '', 'extends' => '', 'blocks_stacks' => array(), 'blocks' => array(), 'blocks_config' => array(), 'nested_blocks' => array()); array_push($this->_stacks, $stack); $this->_current = count($this->_stacks) - 1; unset($stack); ob_start(); $this->_include($filename); $stack = $this->_stacks[$this->_current]; if ($enableLayout) { if (empty($stack['blocks'])) { $stack = $default; $stack['blocks']['content'] = ob_get_clean(); $inherited_stack = $stack; } else { $stack['extends'] = $default['extends']; $stack['contents'] = ob_get_clean(); } } else { $stack['contents'] = ob_get_clean(); } // 如果有继承视图,则用继承视图中定义的块内容替换当前视图的块内容 if (is_array($inherited_stack)) { foreach ($inherited_stack['blocks'] as $block_name => $contents) { if (isset($stack['blocks_config'][$block_name])) { switch (strtolower($stack['blocks_config'][$block_name])) { case 'append': $stack['blocks'][$block_name] .= $contents; break; case 'replace': default: $stack['blocks'][$block_name] .= $contents; } } else { $stack['blocks'][$block_name] = $contents; } } } // 如果有嵌套 block,则替换内容 while (list($child, $parent) = array_pop($stack['nested_blocks'])) { $stack['blocks'][$parent] = str_replace("%block_contents_placeholder_{$child}_{$view_id}%", $stack['blocks'][$child], $stack['blocks'][$parent]); unset($stack['blocks'][$child]); } // 保存对当前视图堆栈的修改 $this->_stacks[$this->_current] = $stack; if (!$enableLayout) { $stack['extends'] = ''; } if ($stack['extends']) { // 如果有当前视图是从某个视图继承的,则载入继承视图 $filename = "{$this->_view_dir}/{$stack['extends']}.{$this->_extname}"; $data = $this->parse($filename, $view_id, $this->_stacks[$this->_current]); log::returnLog($data); return $data; } else { // 最后一个视图一定是没有 extends 的 $last = array_pop($this->_stacks); foreach ($last['blocks'] as $block_name => $contents) { $last['contents'] = str_replace("%block_contents_placeholder_{$block_name}_{$last['id']}%", $contents, $last['contents']); } $this->_stacks = array(); $data = $last['contents']; log::returnLog($data); return $data; } }
/** * 从Cache中删除Key对的应数据 * @access public * @static * @param String $key * @return boolean */ public static function delete($key) { if (!self::$_enabled) { log::accessLog('Cache disabled'); log::returnLog(false); return false; } $deleteResult = self::_getInstance()->_connect()->delete($key); log::accessLog('Execute delete() success'); log::returnLog($deleteResult); return $deleteResult; }
/** * 路由转换处理 * @access public */ public function route() { $path = trim(urldecode($this->getPathInfo()), '/'); $routeMatched = false; $routes = $this->_getRoutes(); if (is_array($routes) && count($routes)) { foreach ($routes as $module => $rules) { if (is_array($rules) && count($rules)) { foreach ($rules as $rule) { if (isset($rule['rule']) && is_string($rule['rule'])) { $pattern = '#^' . $rule['rule'] . '$#i'; $routeMatched = preg_match($pattern, $path, $matches); if ($routeMatched) { array_shift($matches); if (isset($rule['matches']['controller'])) { $classFile = sprintf('%s/app/%s/%scontroller.php', PROJECT_ROOT, $module, $rule['matches']['controller']); if (!is_file($classFile)) { throw new Exception('Error controller file!(' . $rule['matches']['controller'] . ')'); return false; } require_once $classFile; $className = sprintf('%s%sController', strtolower($module), ucfirst($rule['matches']['controller'])); if (!class_exists($className, false)) { throw new Exception('Error controller name!(' . $className . ')'); return false; } $action = 'index'; if (isset($rule['matches']['action'])) { $action = $rule['matches']['action']; } $action = strtolower($action) . 'Action'; $controllerObject = new $className(); if (method_exists($controllerObject, 'init')) { $controllerObject->init(); } if (!method_exists($controllerObject, $action)) { throw new Exception('Error action name(' . $className . '->' . $action . ')'); return false; } call_user_func_array(array($controllerObject, $action), $matches); $routeMatched = true; break 2; } } } } } } } log::accessLog('Execute route() sucessful'); log::returnLog($routeMatched); return $routeMatched; }
/** * convert fields to Mongo parameter array * @access public * @param array $fields * @return array result array */ public function fieldMapped($fields) { if (!$fields) { log::errorLog('$fields is empty!'); return false; } if (!is_array($fields)) { if (trim($fields) == '*') { return array(); } //empty array means all log::errorLog('$fields format is incorrect'); return false; } //make array $fieldsArray = array(); foreach ($fields as $field) { if (is_array($field)) { $fieldsArray[$field['asname']] = 1; } else { //empty field skipped $field = trim($field); if (empty($field)) { continue; } else { $fieldsArray[$field] = 1; } } } log::accessLog('access fieldMapped() succeess!'); log::returnLog(var_export($fieldsArray, true)); return $fieldsArray; }
/** * sort mapped * @access public * @link sortMapped * @return boolean * @example * $sorts = * array( * 'application_id' => 'desc' , // KEY 為 欄位名稱 , VALUE 為排序方式 , 支援多個 SORT 條件. * 'application_name' => 'asc' , * ); * accessMysql::sortMapped($sorts); */ public function sortMapped($sorts) { if (!$sorts) { log::errorLog('$sorts is empty!'); return false; } if (!is_array($sorts)) { log::errorLog('$sorts format is incorrect'); return false; } else { $no = 0; $sortStr = ''; if (is_array($sorts)) { //print_r($sorts); exit ; foreach ($sorts as $sk => $sv) { if ($no == 0) { $sortStr .= " `" . $sk . "` " . $sv . " "; } else { $sortStr .= " , `" . $sk . "` " . $sv . " "; } $no++; } } else { log::errorLog('$sorts format incorrect'); return false; } } log::accessLog('access sortMapped() success!'); log::returnLog($sortStr); return $sortStr; }
/** * * 获取表所在的NameSpace * @access public * @static * @param String $table 表名 * @return String * @example * <p> * $ns = self::_getTableNameSpace($table); * </p> */ private static function _getTableNameSpace($table) { $table = trim($table); $ns = self::MONGO_NS; if (in_array($table, self::$_tableMapped[self::MYSQL_NS])) { $ns = self::MYSQL_NS; } log::returnLog($ns); return $ns; }