diff --git a/convention.php b/convention.php index 9be115ea..0f979e7c 100644 --- a/convention.php +++ b/convention.php @@ -33,6 +33,8 @@ return [ 'default_filter' => '', // 自动Response输出 'response_auto_output' => true, + // 是否启用控制器类后缀 + 'use_controller_suffix' => false, // +---------------------------------------------------------------------- // | 模块设置 diff --git a/library/think/App.php b/library/think/App.php index 06a013db..6b953f94 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -227,7 +227,7 @@ class App // 安全检测 throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000); } - $instance = Loader::controller(CONTROLLER_NAME, '', Config::get('empty_controller')); + $instance = Loader::controller(CONTROLLER_NAME, '', Config::get('use_controller_suffix'), Config::get('empty_controller')); // 获取当前操作名 $action = ACTION_NAME . Config::get('action_suffix'); @@ -237,7 +237,7 @@ class App APP_HOOK && Hook::listen('action_begin', $call); if (!preg_match('/^[A-Za-z](\w)*$/', $action)) { // 非法操作 - throw new Exception('illegal action name :' . ACTION_NAME, 10001); + throw new \ReflectionException('illegal action name :' . ACTION_NAME); } // 执行操作方法 $data = self::invokeMethod($call); diff --git a/library/think/Loader.php b/library/think/Loader.php index 6d8198ac..89548650 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -263,9 +263,10 @@ class Loader * 实例化(分层)模型 * @param string $name Model名称 * @param string $layer 业务层名称 + * @param bool $appendSuffix 是否添加类名后缀 * @return Object */ - public static function model($name = '', $layer = MODEL_LAYER) + public static function model($name = '', $layer = MODEL_LAYER, $appendSuffix = false) { static $_model = []; if (isset($_model[$name . $layer])) { @@ -276,7 +277,7 @@ class Loader } else { $module = APP_MULTI_MODULE ? MODULE_NAME : ''; } - $class = self::parseClass($module, $layer, $name); + $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { $model = new $class(); } else { @@ -295,10 +296,11 @@ class Loader * 实例化(分层)控制器 格式:[模块名/]控制器名 * @param string $name 资源地址 * @param string $layer 控制层名称 + * @param bool $appendSuffix 是否添加类名后缀 * @param string $empty 空控制器名称 * @return Object|false */ - public static function controller($name, $layer = '', $empty = '') + public static function controller($name, $layer = '', $appendSuffix = false, $empty = '') { static $_instance = []; $layer = $layer ?: CONTROLLER_LAYER; @@ -310,12 +312,12 @@ class Loader } else { $module = APP_MULTI_MODULE ? MODULE_NAME : ''; } - $class = self::parseClass($module, $layer, $name); + $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { $action = new $class; $_instance[$name . $layer] = $action; return $action; - } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty))) { + } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { return new $emptyClass; } else { throw new Exception('class [ ' . $class . ' ] not exists', 10001); @@ -326,9 +328,10 @@ class Loader * 实例化验证类 格式:[模块名/]验证器名 * @param string $name 资源地址 * @param string $layer 验证层名称 + * @param bool $appendSuffix 是否添加类名后缀 * @return Object|false */ - public static function validate($name = '', $layer = '') + public static function validate($name = '', $layer = '', $appendSuffix = false) { if (empty($name)) { return new Validate; @@ -343,7 +346,7 @@ class Loader } else { $module = APP_MULTI_MODULE ? MODULE_NAME : ''; } - $class = self::parseClass($module, $layer, $name); + $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { $validate = new $class; } else { @@ -373,14 +376,15 @@ class Loader * @param string $url 调用地址 * @param string|array $vars 调用参数 支持字符串和数组 * @param string $layer 要调用的控制层名称 + * @param bool $appendSuffix 是否添加类名后缀 * @return mixed */ - public static function action($url, $vars = [], $layer = CONTROLLER_LAYER) + public static function action($url, $vars = [], $layer = CONTROLLER_LAYER, $appendSuffix = false) { $info = pathinfo($url); $action = $info['basename']; $module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME; - $class = self::controller($module, $layer); + $class = self::controller($module, $layer, $appendSuffix); if ($class) { if (is_scalar($vars)) { if (strpos($vars, '=')) { @@ -443,11 +447,11 @@ class Loader * @param string $name 类名 * @return string */ - public static function parseClass($module, $layer, $name) + public static function parseClass($module, $layer, $name, $appendSuffix = false) { $name = str_replace(['/', '.'], '\\', $name); $array = explode('\\', $name); - $class = self::parseName(array_pop($array), 1) . (CLASS_APPEND_SUFFIX ? ucfirst($layer) : ''); + $class = self::parseName(array_pop($array), 1) . (CLASS_APPEND_SUFFIX || $appendSuffix ? ucfirst($layer) : ''); $path = $array ? implode('\\', $array) . '\\' : ''; return APP_NAMESPACE . '\\' . (APP_MULTI_MODULE ? $module . '\\' : '') . $layer . '\\' . $path . $class; } diff --git a/library/think/config/driver/ConfigInterface.php b/library/think/config/driver/ConfigInterface.php deleted file mode 100644 index c38727c9..00000000 --- a/library/think/config/driver/ConfigInterface.php +++ /dev/null @@ -1,25 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think\config\driver; - -interface ConfigInterface -{ - - /** - * 解析配置 - * @access public - * @param mixed $config 配置 - * @return mixed - */ - public function parse($config); - -} diff --git a/library/think/config/driver/Ini.php b/library/think/config/driver/Ini.php index 61a49df6..d8dc558d 100644 --- a/library/think/config/driver/Ini.php +++ b/library/think/config/driver/Ini.php @@ -11,7 +11,7 @@ namespace think\config\driver; -class Ini implements ConfigInterface +class Ini { public function parse($config) { diff --git a/library/think/config/driver/Xml.php b/library/think/config/driver/Xml.php index 6cbd82bc..5bc93015 100644 --- a/library/think/config/driver/Xml.php +++ b/library/think/config/driver/Xml.php @@ -11,7 +11,7 @@ namespace think\config\driver; -class Xml implements ConfigInterface +class Xml { public function parse($config) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index c7a5fc78..1483eb5e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -139,17 +139,21 @@ class Query $result = $pdo->fetchAll(PDO::FETCH_COLUMN); } else { $resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC); - $fields = array_keys($resultSet[0]); - $count = count($fields); - $key1 = array_shift($fields); - $key2 = $fields ? array_shift($fields) : ''; - $key = $key ?: $key1; - foreach ($resultSet as $val) { - if ($count > 2) { - $result[$val[$key]] = $val; - } elseif (2 == $count) { - $result[$val[$key]] = $val[$key2]; + if ($resultSet) { + $fields = array_keys($resultSet[0]); + $count = count($fields); + $key1 = array_shift($fields); + $key2 = $fields ? array_shift($fields) : ''; + $key = $key ?: $key1; + foreach ($resultSet as $val) { + if ($count > 2) { + $result[$val[$key]] = $val; + } elseif (2 == $count) { + $result[$val[$key]] = $val[$key2]; + } } + } else { + $result = []; } } if (isset($cache)) {