private function autoload($class) { $eject_array = array('gadgetIndexAbstract', 'gadgetIndexController', 'gadgetApplicationController', 'gadgetApplicationAbstract'); if (!is_array(self::$_maps)) { self::$_maps = (include ROOT_DIR . DS . 'conf' . DS . 'maps.php'); } $pattern = '/[A-Z][a-z0-9]+/'; preg_match_all($pattern, $class, $matches); $prefix = null; $path = null; if (isset($matches[0]) && !empty($matches[0])) { $prefix = substr($class, 0, stripos($class, $matches[0][0])); if ($prefix == 'read' || $prefix == 'write') { return; } $path = ''; foreach ($matches[0] as $m) { $path .= strtolower($m) . DS; } $path = rtrim($path, DS) . '.php'; } else { $path = strtolower($class) . '.php'; } if (in_array($class, $eject_array)) { $path = str_replace('/', '', $path); } if ($prefix && isset(self::$_maps[$prefix])) { $classPath = self::$_maps[$prefix]; } else { $classPath = ROOT_DIR . DS . 'common' . DS; } if (is_array($classPath)) { $required = false; foreach ($classPath as $cp) { if (file_exists($cp . $path)) { require $cp . $path; $required = true; break; } } if (!$required) { log::errorLog($class . ': FileNotFound'); return false; } } else { if (file_exists($classPath . $path)) { require $classPath . $path; } else { log::errorLog($class . ': FileNotFound'); return false; } } }
/** * 获取全局变量 * @access public * @static * @link get * @param $key String 变量名 * @return mixed 变量值 * @example registry::get('foo'); */ public static function get($key) { if (!isset(self::$_var[$key])) { if (self::$_enableLog) { log::errorLog('there has no this(' . $key . ') global variable'); } return false; } else { if (self::$_enableLog) { log::accessLog('access object of registry'); log::returnLog('return variable(' . $key . ')'); } return self::$_var[$key]; } }
/** * get config * @access public * @static * @param string $name * @return mixed * @example * <p> * $name = 'database.params'; * $params = config::get($name); * </p> */ public static function get($name = null) { if (empty($name)) { return self::$_config; } $segments = explode('.', $name); if (1 < count($segments)) { $config = self::$_config; foreach ($segments as $segment) { if (isset($config[$segment])) { $config = $config[$segment]; } else { log::errorLog('config item(' . $name . ') not found'); return false; } } return $config; } else { if (isset(self::$_config[$name])) { return self::$_config[$name]; } else { log::errorLog('config item(' . $name . ') not found'); return false; } } }
/** * 刪除 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; }
/** * 刪除 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 * @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; }
/** * 渲染视图并返回渲染结果 * * @param string $viewname * @param array $vars * @param array $config * * @return string */ public function fetch($viewname = null, array $vars = null, array $config = null) { if (!$this->_enableView) { die; } if (empty($viewname)) { $viewname = $this->_viewname; } $this->_before_render(); $view_dir = isset($config['view_dir']) ? $config['view_dir'] : $this->view_dir; $view_dir = !empty(self::$_viewDir) ? self::$_viewDir : $view_dir; $extname = isset($config['file_extname']) ? $config['file_extname'] : $this->file_extname; $filename = "{$view_dir}/{$viewname}.{$extname}"; if (file_exists($filename)) { if (!is_array($vars)) { $vars = $this->_vars; } if (is_null($this->_parser)) { $parser_name = $this->_parser_name; $this->_parser = new $parser_name($view_dir); } if ($this->_enableLayout) { $output = $this->_parser->assign($vars)->parse($filename, null, null, array('id' => mt_rand(), 'contents' => '', 'extends' => '_layout/default_layout', 'blocks_stacks' => array('content'), 'blocks' => array('content'), 'blocks_config' => array('content' => null), 'nested_blocks' => array())); } else { $output = $this->_parser->assign($vars)->parse($filename, null, null, array('id' => mt_rand(), 'contents' => '', 'extends' => '_layout/default_layout', 'blocks_stacks' => array('content'), 'blocks' => array('content'), 'blocks_config' => array('content' => null), 'nested_blocks' => array()), false); } } else { log::errorLog('Error view file:' . $filename); return false; } $this->_after_render($output); $this->_viewData = $output; log::accessLog('fetch view success'); log::returnLog($output); return $output; }
/** * 获取Cache访问对象并连接Cache * * @access public * @return Object cacheObject extended cacheAbstract * @example * <p> * $cacheObject = cacheLayer::_getInstance()->connect(); * </p> */ private function _connect() { if (!$this->_cacheObject instanceof cacheAbstract) { if (empty(self::$_config['cacheType'])) { log::errorLog('Cache type is empty!'); return false; } $className = 'cache' . ucfirst(strtolower(self::$_config['cacheType'])); $cacheObject = new $className(self::$_config['params']); if (!$cacheObject instanceof cacheAbstract) { log::errorLog($className . ' is not cacheAbstract instance'); return false; } $cacheObject->connect(); $this->_cacheObject =& $cacheObject; log::accessLog('Create ' . $className . ' object success'); } log::returnLog($this->_cacheObject); return $this->_cacheObject; }
/** * 设置base Url * @access public * @param string $baseUrl * @return void */ public function setBaseUrl($baseUrl = null) { if (null !== $baseUrl && !is_string($baseUrl)) { log::errorLog('Error baseUrl value!'); return false; } if ($baseUrl === null) { $filename = isset($_SERVER['SCRIPT_FILENAME']) ? basename($_SERVER['SCRIPT_FILENAME']) : ''; if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) { $baseUrl = $_SERVER['SCRIPT_NAME']; } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) { $baseUrl = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) { $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility } else { // Backtrack up the script_filename to find the portion matching // php_self $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : ''; $segs = explode('/', trim($file, '/')); $segs = array_reverse($segs); $index = 0; $last = count($segs); $baseUrl = ''; do { $seg = $segs[$index]; $baseUrl = '/' . $seg . $baseUrl; ++$index; } while ($last > $index && false !== ($pos = strpos($path, $baseUrl)) && 0 != $pos); } // Does the baseUrl have anything in common with the request_uri? $requestUri = $this->getRequestUri(); if (0 === strpos($requestUri, $baseUrl)) { // full $baseUrl matches $this->_baseUrl = $baseUrl; } else { if (0 === strpos($requestUri, dirname($baseUrl))) { // directory portion of $baseUrl matches $this->_baseUrl = rtrim(dirname($baseUrl), '/'); } else { $truncatedRequestUri = $requestUri; if (($pos = strpos($requestUri, '?')) !== false) { $truncatedRequestUri = substr($requestUri, 0, $pos); } $basename = basename($baseUrl); if (empty($basename) || !strpos($truncatedRequestUri, $basename)) { // no match whatsoever; set it blank $this->_baseUrl = ''; } else { if (strlen($requestUri) >= strlen($baseUrl) && (false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0)) { // If using mod_rewrite or ISAPI_Rewrite strip the script filename // out of baseUrl. $pos !== 0 makes sure it is not matching a value // from PATH_INFO or QUERY_STRING $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); } } } } } $this->_baseUrl = rtrim($baseUrl, '/'); log::accessLog('Execute setBaseUrl successful!'); }
/** * 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; }
public function __destruct() { if (!$this->close()) { log::errorLog('__destruct fail'); } log::accessLog('access close() success!'); //log::returnLog(true); //return true ; }
/** * 获取数据库访问对象并连接数据库 * * @access public * @return Object accessObject extended accessAbstract * @example * <p> * $accessObject = accessLayer::_getInstance()->_connect(); * </p> */ private function _connect() { if (!$this->_accessObject instanceof accessAbstract) { if (!isset(self::$_config[$this->_ns])) { log::errorLog('Config namespace[' . $this->_ns . '] is empty'); return false; } $config = self::$_config[$this->_ns]; if (empty($config['accessType'])) { log::errorLog('Access type is empty!'); return false; } $className = 'access' . ucfirst(strtolower($config['accessType'])); $accessObject = new $className($config['params']); if (!$accessObject instanceof accessAbstract) { log::errorLog($className . ' is not accessAbstract instance'); return false; } $accessObject->connect(); log::accessLog('Create ' . $className . ' object success'); $this->_accessObject =& $accessObject; } log::returnLog($this->_accessObject); return $this->_accessObject; }