/** * 写入日志 * @param $msg * @param $name * @param int $level * @param AbstractHandler $handler */ public static function write($msg, $name, $level = Logger::INFO, AbstractHandler $handler = null) { $name .= '_' . $level; $name = $name . '_' . date('Y-m-d-H', Date::now()); if (isset(self::$loggers[$name])) { $logger = self::$loggers[$name]; if ($handler != null) { $logger->pushHandler($handler); } } else { $logger = new Logger($name); if ($handler == null) { $path = ConfigManager::get('log'); $handler = new StreamHandler($path . DIRECTORY_SEPARATOR . $name . '.log', $level); $formatter = new LineFormatter(ConfigManager::get('log_formatter'), 'Y-m-d H:i:s'); $handler->setFormatter($formatter); } $logger->pushHandler($handler); self::$loggers[$name] = $logger; } if (is_scalar($msg) === false) { $msg = json_encode($msg); } $logger->log($level, $msg); }
/** * 获取连接 * @param $name * @return NoSQL */ public static function memcache($name) { if (isset(self::$_map[$name])) { return self::$_map[$name]; } $cnf = ConfigManager::getNoSQLConfig($name); $nosql = new NoSQL($cnf['host'], $cnf['port']); self::$_map[$name] = $nosql; return $nosql; }
/** * 获取数据库连接 * @param $name * @return MySQLPDO */ public static function get($name) { if (isset(self::$_map[$name])) { return self::$_map[$name]; } //数据库配置信息 $cnf = ConfigManager::getDbConfig($name); $pdo = new MySQLPDO($cnf['host'], $cnf['port'], $cnf['username'], $cnf['passwd'], $cnf['database'], $cnf['charset']); self::$_map[$name] = $pdo; return $pdo; }
/** * 新增模板所需要的一些常量 * @param \Twig_Environment $twig */ protected function addGlobal(\Twig_Environment $twig) { //添加常用的全局变量 $twig->addGlobal('__ROOT__', _ROOT_); $twig->addGlobal('__APP__', _APP_); $twig->addGlobal('__MODULE__', _MODULE_); $twig->addGlobal('__ACTION__', _ACTION_); $twig->addGlobal('__TIME__', _TIME_); $twig->addGlobal('__CSS__', _ROOT_ . '/' . ConfigManager::get('css')); $twig->addGlobal('__JS__', _ROOT_ . '/' . ConfigManager::get('js')); $twig->addGlobal('__IMAGE__', _ROOT_ . '/' . ConfigManager::get('image')); }
/** * * 输出调试信息,应尽量避免使用echo输出 * @param $msg * @param string $name * @param AbstractHandler $handler * @param int $level */ public static function trace($msg, $name = 'debug', AbstractHandler $handler = null, $level = Logger::INFO) { //只有在debug的情况下才会输出调试信息 $debug = ConfigManager::get('debug'); if ($debug === false) { return; } $logger = self::get($name, $handler, $level); if (is_scalar($msg) === false) { $msg = json_encode($msg); } $logger->addRecord($level, $msg); }
/** * 把数据返回给客户端 * @return string */ function toClient() { $data = array(); $data['stat'] = $this->getStatus(); $data['data'] = $this->getBody(); $header = $this->getHeader(); $data['head'][ConfigManager::get('module_var')] = $header[0]; $data['head'][ConfigManager::get('action_var')] = $header[1]; if (isset($header[2])) { $data['head'][ConfigManager::get('group_var')] = $header[2]; } return $data; }
/** * 生成连接 * @param null $m * @param null $a * @param array $p * @param null $g * @return string */ public static function U($m = null, $a = null, $p = array(), $g = null) { $module = $m ? $m : _MODULE_; $action = $a ? $a : _ACTION_; $mVar = ConfigManager::get('module_var'); $aVar = ConfigManager::get('action_var'); $gVar = ConfigManager::get('group_var'); $route = array(); $route[$mVar] = $module; $route[$aVar] = $action; if ($g) { $route[$gVar] = $g; } return '?' . http_build_query(array_merge($route, $p)); }
/** * 加载模板文件 * @param WebResponse $response * @param string $tpl 要加载的模板,默认为null,根据header加载 * @throws TemplateException */ public static function loadTemplate(WebResponse $response, $tpl = null) { $tplPath = ConfigManager::get('tpl'); //.implode(DIRECTORY_SEPARATOR,$header); if ($tpl) { $tplPath = $tplPath . $tpl . '.php'; } else { $header = $response->getHeader(); $tplPath = $tplPath . implode(DIRECTORY_SEPARATOR, $header) . '.php'; } if (file_exists($tplPath) === false) { throw new TemplateException('不存在的模板文件:' . $tplPath); } include $tplPath; }
/** * 处理路由信息 * */ protected function _process() { $moduleVar = ConfigManager::get('module_var'); $actionVar = ConfigManager::get('action_var'); $groupVar = ConfigManager::get('group_var'); //模块 if (isset($_GET[$moduleVar])) { $this->module = String::processMethodName($_GET[$moduleVar]); unset($_GET[$moduleVar]); } else { if (isset($_POST[$moduleVar])) { $this->module = String::processMethodName($_POST[$moduleVar]); unset($_POST[$moduleVar]); } } if (!$this->check($this->module)) { $this->module = String::processMethodName(ConfigManager::get('default_module')); } //操作 if (isset($_GET[$actionVar])) { $this->action = String::processMethodName($_GET[$actionVar]); unset($_GET[$actionVar]); } else { if (isset($_POST[$actionVar])) { $this->action = String::processMethodName($_POST[$actionVar]); unset($_POST[$actionVar]); } } if (!$this->check($this->action)) { $this->action = String::processMethodName(ConfigManager::get('default_action')); } //组 if (isset($_GET[$groupVar])) { $this->group = String::processMethodName($_GET[$groupVar]); unset($_GET[$groupVar]); } else { if (isset($_POST[$groupVar])) { $this->group = String::processMethodName($_POST[$groupVar]); unset($_POST[$groupVar]); } } if (!$this->check($this->group)) { $this->group = null; } }
/** * 自动加载 * @param $className * @throws Exception\BootstrapException */ public static function autoload($className) { $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strrpos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } if (empty($namespace)) { throw new BootstrapException($className . 'namespace is global'); } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; $namespaceInfo = explode('\\', $namespace); $topNameSpace = $namespaceInfo[0]; if (strcmp($topNameSpace, "Simple") == 0) { //系统类库 $fileName = SIMPLE_LIB_PATH . $fileName; } elseif (strcmp(APP_TOP_NAMESPACE, $topNameSpace) === 0) { //项目类库 $fileName = str_replace($topNameSpace, '', APP_PATH) . $fileName; } else { //第三方库 $simpleVendor = ConfigManager::get('simple_vendor'); if (in_array($topNameSpace, $simpleVendor)) { //系统的第三方库 $fileName = SIMPLE_LIB_PATH . 'Simple' . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . $fileName; } elseif (in_array($topNameSpace, ConfigManager::get('app_vendor'))) { //项目的第三方类库 $appVendorPath = ConfigManager::get('app_vendor_path'); $fileName = APP_PATH . $appVendorPath . DIRECTORY_SEPARATOR . $fileName; } } if (file_exists($fileName) == false) { throw new BootstrapException('加载文件' . $fileName . '失败'); } require $fileName; }