/** * 执行引导程序 * @param array $bt 引导程序数组 * @throws \Exception */ private function _boot($bt) { foreach ($bt as $b) { if (strpos($b, '.') === false) { //如果填写不存在方法名直接报错 throw new \Exception('Configs of boot params is error!'); } else { $ao = explode('.', $b); $o = \H2O::createObject($ao[0]); call_user_func([$o, 'act' . ucfirst($ao[1])]); } } }
/** * 执行方法 */ public function handleRequest() { $request = \H2O::getContainer('request'); //控制台请求 $route = $request->getRoute(); if (strncmp($route['controller'], '@', 1) === 0) { //当有前缀@时为,则为系统控制台模块 $sclass = '\\H2O\\console\\' . ucfirst(substr($route['controller'], 1)); $o = \H2O::createObject($sclass); $action = 'act' . ucfirst($route['action']); if (method_exists($o, $action)) { call_user_func([$o, $action]); } else { throw new \Exception($sclass . ' no method:' . $action); } } else { $module = \H2O::getContainer('module'); return $module->runAction($route); } }
/** * 执行动作 * @param array $route 路由 */ public function runAction($route) { $o = \H2O::createObject($this->_ctrnSpace . '\\' . $route['controller']); return $o->runAction(ucfirst($route['action'])); }
/** * @param $curoute 当前路由名称 */ private function getRealPath($curoute) { if (empty($this->_routeTable)) { return $curoute; } else { if (is_string($this->_routeTable)) { //通过API模块查寻真实URL if (strpos($this->_routeTable, '.') === false) { //如果填写不存在方法名直接报错 throw new \Exception('Configs of `request` params is error!'); } else { //自定义路由 $ao = explode('.', $this->_routeTable); $o = \H2O::createObject($ao[0]); $rroute = call_user_func([$o, 'act' . ucfirst($ao[1])]); return empty($rroute) ? $curoute : $rroute; } } else { $ecur = explode('/', $curoute); $lecur = count($ecur); if (isset($this->_routeTable[$ecur[0]])) { $rpath = $this->_routeTable[$ecur[0]]; if (is_array($rpath)) { for ($i = 1; $i < $lecur; $i++) { if (isset($rpath[$i])) { self::$getParams[$rpath[$i]] = $ecur[$i]; } } return $rpath[0]; } else { return $rpath; } } else { return $curoute; } } } }
/** * 返回模板渲染后的字符串 * @param string $tpl 模板文件 * @param array $vars 需要传入模板的数据参数 */ public function render($tpl, $vars = []) { $ov = \H2O::getContainer('view'); $ov->setFile($tpl); $ov->setController(new static()); //设置依附的控制器 $ov->setPath($this->getViewPath()); $ov->setContent($this->getContent()); $vars = array_merge(self::$viewglobalvars, $vars); //合并全局变量和局部变量 $content = $ov->render($vars); if (empty($this->_layout) || !empty($this->_content)) { //非布局 或者已有内容信息,则当前已为布局模块 return $content; } else { //有布局 $route = Module::parseRoute($this->_layout); $o = \H2O::createObject($this->_namespace . '\\' . $route['controller']); $o->setContent($content); //设置主模块缓存 return call_user_func([$o, 'act' . ucfirst($route['action'])]); } }