function update($pk, $value, $appid, $item = 'base') { $project_path = (\Norma\Config::get('build.project_path') ?: dirname(dirname(FRAME_PATH)) . '/project') . '/' . $appid; $config = (include $project_path . '/Config/Global.php'); switch ($item) { case 'error': $data = (include APP_PATH . '/Data/error-config-key.php'); break; case 'template': $data = (include APP_PATH . '/Data/template-config-key.php'); break; case 'url': $data = (include APP_PATH . '/Data/url-config-key.php'); break; case 'module': $data = (include APP_PATH . '/Data/module-config-key.php'); break; case 'base': default: $data = (include APP_PATH . '/Data/base-config-key.php'); break; } if (in_array($pk, $data)) { $config[$pk] = $value; } file_put_contents($project_path . '/Config/Global.php', "<?php\nreturn " . var_export($config, true) . ";"); return $pk; }
public function run(Request $request) { $reject_request = true; if (array_key_exists('HTTP_HOST', $_SERVER)) { $host_name = \Norma\Support\Evn::getHost(); // [ need to cater for `host:port` since some "buggy" SAPI(s) have been known to return the port too, see http://goo.gl/bFrbCO $strpos = strpos($host_name, ':'); if ($strpos !== false) { $host_name = substr($host_name, $strpos); } // ] $enable_domain_whitelist = \Norma\Config::get('enable_domain_whitelist'); //如果启用白名单机制 if ($enable_domain_whitelist) { $domain_whitelist = \Norma\Config::get('domain_whitelist'); //允许的域名 if (!empty($domain_whitelist)) { // [ for dynamic verification, replace this chunk with db/file/curl queries $reject_request = false === array_search($host_name, explode(',', $domain_whitelist), true); // ] } else { $reject_request = false; } } else { $reject_request = false; } } if ($reject_request) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); exit('非法的请求域'); } }
/** * 获取最终服务类实例 * * 云引擎环境自动判断类名 * * @param $name 服务名 * @param $options array 服务参数 * @param $default string 服务默认类名 * @param $prex string 服务默认类名前缀 * @access public */ public static function getInstanceN($name = '', $options = array(), $default = 'Service', $prex = 'Norma') { $called_class = get_called_class(); $class_parts = explode('\\', $called_class); $server_name = array_pop($class_parts); if (empty($name)) { $name = \Norma\Config::get($server_name . ':default', strtoupper(\Norma\Support\Evn::$runEngine) . $default); } $name = static::getServiceName($name); if (isset(self::$instances[$name])) { return self::$instances[$name]; } self::$fac = $called_class . '\\Factory'; return self::$instances[$name] = static::getDriveInstance($name, array_merge((array) \Norma\Config::get($server_name . ':' . $name), (array) $options), $prex); }
function index() { $path = (\Norma\Config::get('build.project_path') ?: dirname(dirname(FRAME_PATH)) . '/project') . '/'; $app = []; $handle = opendir($path); if ($handle) { while ($file = readdir($handle)) { if ($file == '.' || $file == '..') { continue; } $newpath = $path . $file; if (is_dir($newpath)) { $app[] = basename($newpath); } } } $this->assign('list', $app); return $this->fetch('index'); }
/** * 获取错误信息 * ErrorException则使用错误级别作为错误编码 * @param \Exception $exception * @return string 错误信息 */ protected function getMessage(Exception $exception) { $message = $exception->getMessage(); if (\Norma\Support\Evn::isCli()) { return $message; } // 导入语言包 if (Config::get('lang_switch')) { Lang::load(THINK_PATH . '/lang' . DS . Lang::detect() . EXT); } if (strpos($message, ':')) { $name = strstr($message, ':', true); return $message; } else { return $message; } }
/** * 当前的请求类型 * @access public * @param bool $method true 获取原始请求类型 * @return string */ public function method($method = false) { if (true === $method) { // 获取原始请求类型 return \Norma\Support\Evn::isCli() ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } elseif (!$this->method) { if (isset($_POST[Config::get('var_method')])) { $this->method = strtoupper($_POST[Config::get('var_method')]); } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } else { $this->method = \Norma\Support\Evn::isCli() ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } } return $this->method; }
/** * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作 * @param string $url 调用地址 * @param string|array $vars 调用参数 支持字符串和数组 * @param string $layer 要调用的控制层名称 * @param bool $appendSuffix 是否添加类名后缀 * @return mixed */ public static function action($url, $vars = [], $layer = 'Controller', $appendSuffix = false) { $info = pathinfo($url); $action = $info['basename']; $module = '.' != $info['dirname'] ? $info['dirname'] : \Norma\Request::instance()->controller(); $class = self::controller($module, $layer, $appendSuffix); if ($class) { if (is_scalar($vars)) { if (strpos($vars, '=')) { parse_str($vars, $vars); } else { $vars = [$vars]; } } return \Norma\App::invokeMethod([$class, $action . \Norma\Config::get('action_suffix')], $vars); } }
function build($appid) { $project_path = (\Norma\Config::get('build.project_path') ?: dirname(dirname(FRAME_PATH)) . '/project') . '/' . $appid; \Norma\Hook::trigger('build_app', array('project_path' => $project_path, 'buildoption' => ['__dir__' => ['Runtime/cache', 'Runtime/log', 'Runtime/temp', 'Runtime/template', 'Config'], 'demo' => ['__file__' => ['common.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index', 'Test', 'UserType'], 'model' => ['User', 'UserType'], 'view' => ['index/index']]])); return $this->fetch(); }
// +---------------------------------------------------------------------- // | Author: LunnLew <*****@*****.**> // +---------------------------------------------------------------------- // 加载函数库 require FRAME_PATH . '/functions.php'; // 注册框架类加载器 require FRAME_PATH . '/Support/Traits/LoaderHelper.php'; require __DIR__ . '/Loader.php'; // 注册类加载器 $loader = new \Norma\Loader(); $loader->register(); $loader->addNamespace('Norma', FRAME_PATH); \Norma\App::$loader = $loader; // 注册错误和异常处理机制 \Norma\Error::register(); // 加载Composer库 defined('COMPOSER_VENDOR_PATH') and (require_once COMPOSER_VENDOR_PATH . '/autoload.php'); // 加载默认全局配置文件 \Norma\Config::load(FRAME_PATH . '/Config/Global-default.php'); // 加载应用配置文件 \Norma\Config::load(APP_PATH . '/Config/Global.php'); // 平台兼容支持 \Norma\Constant::LoadDefineWith([\Norma\Support\Evn::DetectOS(), function () { \Norma\Support\Evn::DetectEngine(); if (!empty(\Norma\Support\Evn::$runEngineEx)) { \Norma\Constant::LoadDefineWith(\Norma\Support\Evn::$runEngineEx, FRAME_PATH . '/Compatibility'); } return \Norma\Support\Evn::$runEngine; }, \Norma\Support\Evn::DetectAccessMode()], FRAME_PATH . '/Compatibility'); // 加载插件 \Norma\Hook::loadPlugin(FRAME_PATH . '/Plugin');
/** * 解析URL地址中的参数Request对象 * @access private * @param string $rule 路由规则 * @param array $var 变量 * @return void */ private static function parseUrlParams($url, $var = []) { if ($url) { if (Config::get('url_param_type')) { $var += explode('/', $url); } else { preg_replace_callback('/(\\w+)\\/([^\\/]+)/', function ($match) use(&$var) { $var[$match[1]] = strip_tags($match[2]); }, $url); } } // 设置当前请求的参数 Request::instance()->route($var); }
/** * 执行应用 * @static * @access public */ public static function listen(Request $request = null, $type = 'web') { is_null($request) && ($request = Request::instance()); //请求筛选 \Norma\Hook::trigger('request_action', [$request]); //加载应用独立文件 if (\Norma\Config::has('extra_config_list')) { $extra_config_list = \Norma\Config::get('extra_config_list'); if (is_string($extra_config_list)) { $extra_config_list = explode(',', $extra_config_list); } foreach ($extra_config_list as $key => $file) { \Norma\Config::load(APP_PATH . '/Config/' . $file . '.php'); } } // 设置系统时区 date_default_timezone_set(\Norma\Config::get('default_timezone')); // 应用调试模式 self::$debug = Config::get('app_debug'); if (!self::$debug) { ini_set('display_errors', 'Off'); } else { //重新申请一块比较大的buffer if (ob_get_level() > 0) { $output = ob_get_clean(); } ob_start(); if (!empty($output)) { echo $output; } } // 注册应用实现类名称空间 \Norma\App::$loader->addNamespace(\Norma\Config::get('app_namespace') ?: "App", APP_PATH); // 加载插件 \Norma\Hook::loadPlugin(APP_PATH . '/Plugin', \Norma\Config::get('app_namespace')); // 开启多语言机制 if (\Norma\Config::get('lang_switch_on')) { // 获取当前语言 $request->langset(Lang::detect()); // 加载系统语言包 Lang::load(FRAME_PATH . '/Ability/Lang' . DIRECTORY_SEPARATOR . $request->langset() . '.php'); if (!\Norma\Config::get('app_multi_module')) { Lang::load(APP_PATH . '/Lang' . DIRECTORY_SEPARATOR . $request->langset() . '.php'); } } // 获取应用调度信息 $dispatch = self::$dispatch; if (empty($dispatch)) { // 进行URL路由检测 $dispatch = self::routeCheck($request, \Norma\Config::get()); } // 记录当前调度信息 $request->dispatch($dispatch); // 记录路由信息 //self::$debug && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); // 监听app_begin //Hook::listen('app_begin', $dispatch); switch ($dispatch['type']) { case 'redirect': // 执行重定向跳转 $data = Response::create($dispatch['url'], 'redirect')->code($dispatch['status']); break; case 'module': // 模块/控制器/操作 $data = self::module($dispatch['module'], \Norma\Config::get(), isset($dispatch['convert']) ? $dispatch['convert'] : null); break; case 'controller': // 执行控制器操作 $data = Loader::action($dispatch['controller'], $dispatch['params']); break; case 'method': // 执行回调方法 $data = self::invokeMethod($dispatch['method'], $dispatch['params']); break; case 'function': // 执行闭包 $data = self::invokeFunction($dispatch['function'], $dispatch['params']); break; case 'response': $data = $dispatch['response']; break; default: throw new \InvalidArgumentException('dispatch type not support'); } // 输出数据到客户端 if ($data instanceof Response) { $response = $data; } elseif (!is_null($data)) { // 默认自动识别响应输出类型 $isAjax = $request->isAjax(); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); \Norma\Hook::trigger('output', [$data, $type]); } else { \Norma\Hook::trigger('output'); } // //return $response; }
/** * Get an instance of the exception handler. * * @return Handle */ public static function getExceptionHandler() { static $handle; if (!$handle) { // 异常处理handle $class = Config::get('exception_handle'); if ($class && class_exists($class) && is_subclass_of($class, "\\Norma\\Exception\\Handle")) { $handle = new $class(); } else { $handle = new Handle(); } } return $handle; }