/** * 最终执行入口 * @param $arg_context * @return mixed * @throws */ function doExecute(\Context $arg_context) { $exc = $arg_context->Params("exc"); if ($exc == "echo_exc") { file_put_contents(dirname(dirname(dirname(__FILE__))) . "/temp/testForCmd.text", "echo_exc"); $arg_context->json_echo(new test()); } if ($exc == "exc") { file_put_contents(dirname(dirname(dirname(__FILE__))) . "/temp/testForCmd.text", "exc"); throw new \Exception("test Exception"); } file_put_contents(dirname(dirname(dirname(__FILE__))) . "/temp/testForCmd.text", "test.test"); }
/** * 解析命令新建对应命令控制器实例 * * @param Context $arg_context 上下文 (请求内容) * * @return Controller * @throws \Exceptions\ResolverException */ function getController(Context $arg_context) { $cmd = $arg_context->Params("cmd"); $step = DIRECTORY_SEPARATOR; if (!$cmd) { $cmd = self::$_default_cmd; } //校验命令格式 TODO:可以在Params上校验 $cmd_filter = AppHelper::Instance()->config("CMD_FILTER"); if ($cmd_filter && preg_match($cmd_filter, $cmd) != 1) { throw new \Exceptions\ResolverException("Command cannot pass filter"); } //如果存在‘.’ 则替换成文件分隔符, //实现控制器目录下多级组合 $cmd = trim(str_replace(array("."), $step, $cmd)); //应用根目录,控制器目录,文件后缀名 $app_root = rtrim(self::$_AppPath, ' \\/'); $app_ctrl = self::$_ctrl_namespace; $ctrl_suffix = AppHelper::Instance()->config("CTRL_FILE_SUFFIX"); //构建文件目录和类 $file_path = $app_root . $step . $app_ctrl . $step . $cmd . $ctrl_suffix; $class_name = "\\{$app_ctrl}\\" . (strripos($cmd, $step) ? substr($cmd, strripos($cmd, $step) + 1) : $cmd); // echo "\n", $file_path, "\n", $class_name, "\n"; if (!file_exists($file_path)) { throw new \Exceptions\ResolverException("Command file '{$cmd}' not found"); } @(require_once "{$file_path}"); if (!class_exists("{$class_name}")) { throw new \Exceptions\ResolverException("Command '{$cmd}' not found"); } $cmd_class = new ReflectionClass("{$class_name}"); if (!$cmd_class->isSubclassOf(self::$_base_cmd)) { throw new \Exceptions\ResolverException("Command '{$cmd}' is not a command"); } return $cmd_class->newInstance(); }