diff --git a/core/base.php b/core/base.php index 28de4b51..bf1276f2 100644 --- a/core/base.php +++ b/core/base.php @@ -38,16 +38,18 @@ define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); require CORE_PATH . 'Loader.php'; // 加载环境变量配置文件 -if (is_file(ROOT_PATH . 'env' . EXT)) { - $env = include ROOT_PATH . 'env' . EXT; +if (is_file(ROOT_PATH . '.env')) { + $env = parse_ini_file(ROOT_PATH . '.env', true); foreach ($env as $key => $val) { $name = ENV_PREFIX . strtoupper($key); - if (is_bool($val)) { - $val = $val ? 1 : 0; - } elseif (!is_scalar($val)) { - continue; + if (is_array($val)) { + foreach ($val as $k => $v) { + $item = $name . '_' . strtoupper($k); + putenv("$item=$v"); + } + } else { + putenv("$name=$val"); } - putenv("$name=$val"); } } diff --git a/core/helper.php b/core/helper.php index 1b8def16..44c469f8 100644 --- a/core/helper.php +++ b/core/helper.php @@ -100,7 +100,7 @@ if (!function_exists('config')) { function config($name = '', $value = null, $range = '') { if (is_null($value) && is_string($name)) { - return Config::get($name, $range); + return 0 === strpos($name, '?') ? Config::has(substr($name, 1), $range) : Config::get($name, $range); } else { return Config::set($name, $value, $range); } diff --git a/core/lang/zh-cn.php b/core/lang/zh-cn.php index 778aef4d..b93092e2 100644 --- a/core/lang/zh-cn.php +++ b/core/lang/zh-cn.php @@ -13,7 +13,8 @@ return [ // 系统错误提示 'Undefined variable' => '未定义变量', - 'Undefined index' => '未定义索引', + 'Undefined index' => '未定义数组索引', + 'Undefined offset' => '未定义数组下标', 'Parse error' => '语法解析错误', 'Type error' => '类型错误', 'Fatal error' => '致命错误', diff --git a/core/library/think/App.php b/core/library/think/App.php index a9236f6f..bfaca8d2 100644 --- a/core/library/think/App.php +++ b/core/library/think/App.php @@ -12,6 +12,7 @@ namespace think; use think\Config; +use think\Env; use think\Exception; use think\exception\HttpException; use think\exception\HttpResponseException; @@ -371,7 +372,7 @@ class App self::$suffix = $config['class_suffix']; // 应用调试模式 - self::$debug = Config::get('app_debug'); + self::$debug = Env::get('app_debug', Config::get('app_debug')); if (!self::$debug) { ini_set('display_errors', 'Off'); } elseif (!IS_CLI) { @@ -448,11 +449,6 @@ class App $config = Config::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT); } - // 加载别名文件 - if (is_file(CONF_PATH . $module . 'alias' . EXT)) { - Loader::addClassMap(include CONF_PATH . $module . 'alias' . EXT); - } - // 加载行为扩展文件 if (is_file(CONF_PATH . $module . 'tags' . EXT)) { Hook::import(include CONF_PATH . $module . 'tags' . EXT); diff --git a/core/library/think/Cache.php b/core/library/think/Cache.php index 9f7a569d..ecfd2cf3 100644 --- a/core/library/think/Cache.php +++ b/core/library/think/Cache.php @@ -37,7 +37,7 @@ class Cache { $type = !empty($options['type']) ? $options['type'] : 'File'; if (false === $name) { - $name = $type; + $name = md5(serialize($options)); } if (true === $name || !isset(self::$instance[$name])) { diff --git a/core/library/think/Config.php b/core/library/think/Config.php index 7256e0ed..5ab032fd 100644 --- a/core/library/think/Config.php +++ b/core/library/think/Config.php @@ -81,20 +81,10 @@ class Config $range = $range ?: self::$range; if (!strpos($name, '.')) { - // 判断环境变量 - $result = getenv(ENV_PREFIX . strtoupper($name)); - if (false !== $result) { - return $result; - } return isset(self::$config[$range][strtolower($name)]); } else { // 二维数组设置和获取支持 - $name = explode('.', $name); - $result = getenv(ENV_PREFIX . strtoupper($name[0] . '_' . $name[1])); - // 判断环境变量 - if (false !== $result) { - return $result; - } + $name = explode('.', $name); return isset(self::$config[$range][strtolower($name[0])][$name[1]]); } } @@ -114,20 +104,11 @@ class Config } if (!strpos($name, '.')) { - $result = getenv(ENV_PREFIX . strtoupper($name)); - if (false !== $result) { - return $result; - } $name = strtolower($name); return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null; } else { // 二维数组设置和获取支持 - $name = explode('.', $name); - $result = getenv(ENV_PREFIX . strtoupper($name[0] . '_' . $name[1])); - // 判断环境变量 - if (false !== $result) { - return $result; - } + $name = explode('.', $name); $name[0] = strtolower($name[0]); return isset(self::$config[$range][$name[0]][$name[1]]) ? self::$config[$range][$name[0]][$name[1]] : null; } diff --git a/core/library/think/Env.php b/core/library/think/Env.php new file mode 100644 index 00000000..a23d9925 --- /dev/null +++ b/core/library/think/Env.php @@ -0,0 +1,31 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +class Env +{ + /** + * 获取环境变量值 + * @param string $name 环境变量名(支持二级 .号分割) + * @param string $default 默认值 + * @return mixed + */ + public static function get($name, $default = null) + { + $result = getenv(ENV_PREFIX . strtoupper(str_replace('.', '_', $name))); + if (false !== $result) { + return $result; + } else { + return $default; + } + } +} diff --git a/core/library/think/Model.php b/core/library/think/Model.php index 988012ea..e5a107f8 100644 --- a/core/library/think/Model.php +++ b/core/library/think/Model.php @@ -238,6 +238,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (is_string($data)) { $this->data[$data] = $value; } else { + // 清空数据 + $this->data = []; if (is_object($data)) { $data = get_object_vars($data); } diff --git a/core/library/think/cache/driver/File.php b/core/library/think/cache/driver/File.php index da3b3f6f..ea5688e5 100644 --- a/core/library/think/cache/driver/File.php +++ b/core/library/think/cache/driver/File.php @@ -90,8 +90,7 @@ class File extends Driver */ public function has($name) { - $filename = $this->getCacheKey($name); - return is_file($filename); + return $this->get($name) ? true : false; } /** diff --git a/core/library/think/cache/driver/Lite.php b/core/library/think/cache/driver/Lite.php index a527a012..9e702eed 100644 --- a/core/library/think/cache/driver/Lite.php +++ b/core/library/think/cache/driver/Lite.php @@ -61,8 +61,7 @@ class Lite extends Driver */ public function has($name) { - $filename = $this->getCacheKey($name); - return is_file($filename); + return $this->get($name) ? true : false; } /** diff --git a/core/library/think/cache/driver/Memcached.php b/core/library/think/cache/driver/Memcached.php index 9c0a2597..5857edce 100644 --- a/core/library/think/cache/driver/Memcached.php +++ b/core/library/think/cache/driver/Memcached.php @@ -24,6 +24,7 @@ class Memcached extends Driver 'prefix' => '', 'username' => '', //账号 'password' => '', //密码 + 'option' => [], ]; /** @@ -40,6 +41,9 @@ class Memcached extends Driver $this->options = array_merge($this->options, $options); } $this->handler = new \Memcached; + if (!empty($this->options['option'])) { + $this->handler->setOptions($this->options['option']); + } // 设置连接超时时间(单位:毫秒) if ($this->options['timeout'] > 0) { $this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']); diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 84f0edcb..1d51cc54 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -908,9 +908,11 @@ class Query $where = $field; } elseif ($field) { // 字符串查询 - $where[] = ['exp', $field]; - } else { - $where = ''; + if (is_numeric($field)) { + $where[] = ['exp', $field]; + } else { + $where[$field] = ['null', '']; + } } } elseif (is_array($op)) { $where[$field] = $param; @@ -1227,7 +1229,7 @@ class Query /** * 设置查询数据不存在是否抛出异常 * @access public - * @param bool $fail 是否严格检查字段 + * @param bool $fail 数据不存在是否抛出异常 * @return $this */ public function failException($fail = true) @@ -1855,7 +1857,8 @@ class Query $resultSet = false; if (empty($options['fetch_sql']) && !empty($options['cache'])) { // 判断查询缓存 - $cache = $options['cache']; + $cache = $options['cache']; + unset($options['cache']); $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); $resultSet = Cache::get($key); } @@ -1883,7 +1886,7 @@ class Query } // 返回结果处理 - if ($this->connection->getNumRows()) { + if (count($resultSet) > 0) { // 数据列表读取后的处理 if (!empty($this->model)) { // 生成模型对象 diff --git a/core/library/think/db/builder/Sqlsrv.php b/core/library/think/db/builder/Sqlsrv.php index f54bd139..bf630d90 100644 --- a/core/library/think/db/builder/Sqlsrv.php +++ b/core/library/think/db/builder/Sqlsrv.php @@ -31,6 +31,22 @@ class Sqlsrv extends Builder */ protected function parseOrder($order) { + if (is_array($order)) { + $array = []; + foreach ($order as $key => $val) { + if (is_numeric($key)) { + if (false === strpos($val, '(')) { + $array[] = $this->parseKey($val); + } elseif ('[rand]' == $val) { + $array[] = $this->parseRand(); + } + } else { + $sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; + $array[] = $this->parseKey($key) . ' ' . $sort; + } + } + $order = implode(',', $array); + } return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()'; } diff --git a/core/library/think/debug/Console.php b/core/library/think/debug/Console.php index 1763fc66..8a232c8c 100644 --- a/core/library/think/debug/Console.php +++ b/core/library/think/debug/Console.php @@ -54,7 +54,7 @@ class Console } // 获取基本信息 $runtime = number_format(microtime(true) - THINK_START_TIME, 10); - $reqs = number_format(1 / $runtime, 2); + $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; $mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2); if (isset($_SERVER['HTTP_HOST'])) { diff --git a/core/library/think/debug/Html.php b/core/library/think/debug/Html.php index 1061b372..f8651aa9 100644 --- a/core/library/think/debug/Html.php +++ b/core/library/think/debug/Html.php @@ -54,7 +54,7 @@ class Html } // 获取基本信息 $runtime = number_format(microtime(true) - THINK_START_TIME, 10); - $reqs = number_format(1 / $runtime, 2); + $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; $mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2); // 页面Trace信息