/** * 实例化本程序 * @param $args = func_get_args(); * @return object of this class */ public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * 字符串分页 * * 返回一个二维数组, * * @param string $string * @param int $size * @param boolean|array * @return array */ public static function paginString($string, $size, $withTag = false) { $count = mb_strlen($string); $paginator = Leb_Page::getInstance(); $paginator->setTotalSize($count); $paginator->setPageSize($size); // 偏移点 $offset = $paginator->getRecordOffset(); if (!$withTag) { $string = strip_tags($string); } $subString = mb_substr($string, $offset, $size); // 分页供显示 $array = $paginator->toArray(); $pager = array(); // 当前url $request = Leb_Request::getInstance(); $currentUrl = $request->getUri(); $currentUrl = preg_replace("/[&?]?{$array['tag']}=[^\$?]*/", '', $currentUrl); if (false === strstr($currentUrl, '?')) { $pagerLinker = '?'; } else { $pagerLinker = '&'; } $pageUrl = $currentUrl . $pagerLinker . $array['tag'] . "="; // 每一页 for ($i = 1; $i <= $array['totalPage']; $i++) { $pager['all'][$i] = $pageUrl . $i; } // 上一页 if ($array['currentPage'] <= 1) { $prePage = 1; } else { $prePage = $array['currentPage'] - 1; } $pager['pre'] = $pageUrl . $prePage; // 下一页 if ($array['currentPage'] >= $array['totalPage']) { $nextPage = $array['totalPage']; } else { $nextPage = $array['currentPage'] + 1; } $pager['next'] = $pageUrl . $nextPage; // 尾页 $pager['end'] = $pageUrl . $array['totalPage']; // 首页 $pager['first'] = $pageUrl . 1; // 当前页 $pager['current'] = $pageUrl . $array['currentPage']; // 分开的内容 $pager['content'] = $subString; return $pager; }
/** * 分发过程 * * 可供程序直接跳转 * * @param string $appName * @param string $controllerName * @param string $actionName * @param array $params 查询参数,相当于post 或 get过来的参数值 */ public function dispatch($appName, $controllerName, $actionName, $params = array(), $plugins = null) { if (empty($plugins)) { $plugins = self::getInstance(); } // 找到相关程序并执行 $classPath = (_CLI_ ? _CMD_ : _APP_) . $appName . "/" . $controllerName . ".php"; if (!file_exists($classPath)) { //如果不存在,则按默认执行 $page_path = "{$appName}/{$controllerName}/{$actionName}"; $controllerName = $plugins->getDefaultController(); $actionName = $plugins->getDefaultAction(); $classPath = _APP_ . $appName . "/" . $controllerName . ".php"; if (!file_exists($classPath)) { $page_path = "{$page_path}|{$appName}/{$controllerName}/{$actionName}"; throw new Leb_Exception('Error 404, error page not found!' . $page_path, 404); } // 转发 $this->setAppName($appName); $this->setControllerName($controllerName); $this->setActionName($actionName); } else { try { require_once $classPath; } catch (Leb_Exception $e) { throw $e; } } // 默认动作如果动作为空 $controllerClassName = $controllerName . (_CLI_ ? 'Command' : 'Controller'); $actionFunctionName = $actionName . 'Action'; // 新建控制器 $controller = new $controllerClassName($plugins); if (!empty($params)) { $controller->setParam($params); } if (!method_exists($controller, $actionFunctionName)) { $refc = new ReflectionClass($controllerClassName); $mtdc = $refc->getMethod('__call'); $decl = $mtdc->getDeclaringClass(); // if ((new ReflectionClass($controllerClassName))->getMethod('__call')->getDeclaringClass()->name == $controllerClassName) if ($decl->name == $controllerClassName) { // $controller->$actionFunctionName(); // goon 继续下面的调用,因为我们知道该controller中对此有处理。 } else { throw new Leb_Exception('Page <strong>[' . $actionFunctionName . ']</strong>' . 'not found! The reason cause this error may be Method not exist' . 'in the Controller <strong>[' . $controllerClassName . ']</strong>'); } } // 运行类的run方法,主要是用于加载一些共用操作,如layout数据 $controller->run(); defined('__FRM_ACTION_BEGIN__') or define('__FRM_ACTION_BEGIN__', microtime(true)); $beforeMethod = $actionName . 'Before'; if (method_exists($controller, $beforeMethod)) { $controller->{$beforeMethod}(); } $method = new ReflectionMethod($controllerClassName, $actionFunctionName); if ($method->getNumberOfParameters() > 0) { $this->_act_return = $response = $this->runWithParams($controller, $method, _CLI_ ? $_SERVER['argv'] : $_GET); } else { $this->_act_return = $response = $controller->{$actionFunctionName}(); } $afterMethod = $actionName . 'After'; if (method_exists($controller, $afterMethod)) { $controller->{$afterMethod}(); } defined('__FRM_ACTION_END__') or define('__FRM_ACTION_END__', microtime(true)); //显示调试信息 $request = Leb_Request::getInstance(); if (!$request->isXmlHttpRequest() && !$request->isFlashRequest) { Leb_Debuger::showVar(); } $obj = FirePHP::getInstance(true); $obj->info('Response:' . (__FRM_ACTION_END__ - __FRM_ACTION_BEGIN__)); $obj->info('Framework:' . (__FRM_ACTION_BEGIN__ - __FRM_BEGIN__)); if (!empty($response) && $response instanceof Leb_Response) { $response->run(); } else { //shiling 计划任务forward的返回值 return $response; } }
/** * 注册结束脚本回调函数 */ public static function shutdown() { if (XHPROF_ANALYSIS) { $xhprof_data = xhprof_disable(); $obj = FirePHP::getInstance(true)->info($xhprof_data); //format //[ct] => 2 # number of calls to bar() from foo() //[wt] => 37 # time in bar() when called from foo() //[cpu]=> 0 # cpu time in bar() when called from foo() //[mu] => 2208 # change in PHP memory usage in bar() when called from foo() //[pmu]=> 0 # change in PHP peak memory usage in bar() when called from foo() } if (!defined('__FRM_ACTION_END__') && defined('__FRM_ACTION_END__')) { define('__FRM_ACTION_END__', microtime(true)); $request = Leb_Request::getInstance(); $obj = FirePHP::getInstance(true); $obj->info('Response:' . (__FRM_ACTION_END__ - __FRM_ACTION_BEGIN__)); $obj->info('Framework:' . (__FRM_ACTION_BEGIN__ - __FRM_BEGIN__)); } }
/** * 获得基本域 * @return string */ public function getBaseDomain() { $basedomain = $this->getBase('basedomain'); if (!$basedomain) { $domain = Leb_Request::get('HTTP_HOST'); $domainSections = explode('.', $domain); $domainparts = count($domainSections); $basedomain = $domainSections[$domainparts - 2] . $domainSections[$domainparts - 1]; } return $basedomain; }