diff --git a/application/admin/Controller.php b/application/admin/Controller.php index a7bd627..3cac7d2 100644 --- a/application/admin/Controller.php +++ b/application/admin/Controller.php @@ -288,6 +288,7 @@ protected function filterId($filterData, $error = '该记录不能执行此操 * $map['_order_by'] 可强制设置排序字段(field asc|desc[,filed2 asc|desc...]或者false) * $map['_paginate'] 是否开启分页,传入false可以关闭分页 * $map['_model'] 可强制指定模型 + * $map['_func'] 匿名函数,可以给模型设置属性,比如关联,alias,function ($model) {$model->alias('table')->join(...)} * * @param Model|Db $model 数据对象 * @param array $map 过滤条件 @@ -300,13 +301,17 @@ protected function filterId($filterData, $error = '该记录不能执行此操 protected function datalist($model, $map, $field = null, $sortBy = '', $asc = false, $return = false, $paginate = true) { // 私有字段,指定特殊条件,查询时要删除 - $protectField = ['_table', '_relation', '_field', '_order_by', '_paginate', '_model']; + $protectField = ['_table', '_relation', '_field', '_order_by', '_paginate', '_model', '_func']; // 通过过滤器指定模型 if (isset($map['_model'])) { $model = $map['_model']; } + if (isset($map['_func']) && ($map['_func'] instanceof \Closure)) { + call_user_func_array($map['_func'], [$model]); + } + // 排序字段 默认为主键名 $order = $this->request->param('_order') ?: (empty($sortBy) ? $model->getPk() : $sortBy); diff --git a/application/admin/common.php b/application/admin/common.php index 9f06eac..6be2423 100644 --- a/application/admin/common.php +++ b/application/admin/common.php @@ -240,11 +240,13 @@ function reset_by_key($arr, $key) /** * 节点遍历 - * @param $list + * + * @param $list * @param string $pk * @param string $pid * @param string $child - * @param int $root + * @param int $root + * * @return array */ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) @@ -255,10 +257,16 @@ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root // 创建基于主键的数组引用 $refer = []; foreach ($list as $key => $data) { + if ($data instanceof \think\Model) { + $list[$key] = $data->toArray(); + } $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent + if (!isset($list[$key][$child])) { + $list[$key][$child] = []; + } $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; @@ -418,3 +426,79 @@ function format_bytes($size, $delimiter = '') return round($size, 2) . $delimiter . $units[$i]; } + +/** + * 生成一定长度的UUID + * + * @param int $length + * + * @return string + */ +function get_uuid($length = 16) +{ + mt_srand((double)microtime()*10000); + $uuid = sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); + $str = base64_encode($uuid); + return substr($str, mt_rand(0, strlen($str) - $length), $length); +} + +/** + * 根据模型名称获取模型 + * + * @param $modelName + * + * @return \think\Model|\think\db\Query + */ +function get_model($modelName) +{ + if (false !== strpos($modelName, '\\')) { + // 指定模型类 + $db = new $modelName; + } else { + try { + $db = \think\Loader::model($modelName); + } catch (\think\exception\ClassNotFoundException $e) { + $db = \think\Db::name($modelName); + } + } + + return $db; +} + +/** + * 验证规则扩展 + */ +\think\Validate::extend([ + // 验证字段是否在模型中存在 + 'checkExist' => function($value, $rule, $data, $field) { + if (is_string($rule)) { + $rule = explode(',', $rule); + } + $db = get_model($rule[0]); + $key = isset($rule[1]) ? $rule[1] : $field; + + if (strpos($key, '^')) { + // 支持多个字段验证 + $fields = explode('^', $key); + foreach ($fields as $key) { + $map[$key] = $data[$key]; + } + } elseif (strpos($key, '=')) { + parse_str($key, $map); + } else { + $map[$key] = $data[$field]; + } + + $pk = strval(isset($rule[3]) ? $rule[3] : $db->getPk()); + if (isset($rule[2])) { + $map[$pk] = ['neq', $rule[2]]; + } elseif (isset($data[$pk])) { + $map[$pk] = ['neq', $data[$pk]]; + } + + if ($db->where($map)->field($pk)->find()) { + return true; + } + return false; + } +]); diff --git a/application/admin/controller/AdminUser.php b/application/admin/controller/AdminUser.php index eb91064..865163e 100644 --- a/application/admin/controller/AdminUser.php +++ b/application/admin/controller/AdminUser.php @@ -35,13 +35,13 @@ protected function filter(&$map) $map['realname'] = ["like", "%" . $this->request->param('realname') . "%"]; } if ($this->request->param('account')) { - $map['realname'] = ["like", "%" . $this->request->param('account') . "%"]; + $map['account'] = ["like", "%" . $this->request->param('account') . "%"]; } if ($this->request->param('email')) { - $map['realname'] = ["like", "%" . $this->request->param('email') . "%"]; + $map['email'] = ["like", "%" . $this->request->param('email') . "%"]; } if ($this->request->param('mobile')) { - $map['realname'] = ["like", "%" . $this->request->param('mobile') . "%"]; + $map['mobile'] = ["like", "%" . $this->request->param('mobile') . "%"]; } } diff --git a/application/admin/controller/LoginLog.php b/application/admin/controller/LoginLog.php index 26655dc..b4a9755 100644 --- a/application/admin/controller/LoginLog.php +++ b/application/admin/controller/LoginLog.php @@ -17,6 +17,8 @@ \think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT); use app\admin\Controller; +use app\common\model\LoginLog as ModelLoginLog; +use app\common\model\AdminUser as ModelAdminUser; class LoginLog extends Controller { @@ -42,7 +44,9 @@ protected function filter(&$map) // 设置属性 $map['_table'] = "login_log"; - $map['_relation'] = "user"; $map['_order_by'] = "login_log.id desc"; + $map['_func'] = function (ModelLoginLog $model) use ($map) { + $model->alias($map['_table'])->join(ModelAdminUser::getTable() . ' user', 'login_log.uid = user.id'); + }; } -} \ No newline at end of file +} diff --git a/application/admin/controller/NodeMap.php b/application/admin/controller/NodeMap.php index 4125ee1..ccaa915 100644 --- a/application/admin/controller/NodeMap.php +++ b/application/admin/controller/NodeMap.php @@ -31,7 +31,7 @@ class NodeMap extends Controller protected function filter(&$map) { if ($this->request->param("map")) { - $map['map'] = ["like", "%" . $this->request->param("map") . "%"]; + $map['action|module|controller'] = ["like", "%" . $this->request->param("map") . "%"]; } if ($this->request->param("comment")) { $map['comment'] = ["like", "%" . $this->request->param("comment") . "%"]; diff --git a/application/admin/view/admin_group/index.html b/application/admin/view/admin_group/index.html index b41508c..1075bb7 100755 --- a/application/admin/view/admin_group/index.html +++ b/application/admin/view/admin_group/index.html @@ -33,7 +33,7 @@
' . $label . $output . ''; } if ($echo) { - echo ($output); - return null; + echo($output); + return; } else { return $output; } diff --git a/thinkphp/library/think/Env.php b/thinkphp/library/think/Env.php index a23d992..6f31841 100644 --- a/thinkphp/library/think/Env.php +++ b/thinkphp/library/think/Env.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/Error.php b/thinkphp/library/think/Error.php index 28f7588..c17292b 100644 --- a/thinkphp/library/think/Error.php +++ b/thinkphp/library/think/Error.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/Exception.php b/thinkphp/library/think/Exception.php index ac64876..034c85b 100644 --- a/thinkphp/library/think/Exception.php +++ b/thinkphp/library/think/Exception.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/File.php b/thinkphp/library/think/File.php index 60a1641..ba59273 100644 --- a/thinkphp/library/think/File.php +++ b/thinkphp/library/think/File.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/Hook.php b/thinkphp/library/think/Hook.php index 4d69ca5..f06196e 100644 --- a/thinkphp/library/think/Hook.php +++ b/thinkphp/library/think/Hook.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,11 +11,6 @@ namespace think; -use think\App; -use think\Debug; -use think\Loader; -use think\Log; - class Hook { diff --git a/thinkphp/library/think/Lang.php b/thinkphp/library/think/Lang.php index e064ae4..d52f194 100644 --- a/thinkphp/library/think/Lang.php +++ b/thinkphp/library/think/Lang.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,10 +11,6 @@ namespace think; -use think\App; -use think\Cookie; -use think\Log; - class Lang { // 语言数据 diff --git a/thinkphp/library/think/Loader.php b/thinkphp/library/think/Loader.php index 0b536d6..e7a6df2 100644 --- a/thinkphp/library/think/Loader.php +++ b/thinkphp/library/think/Loader.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -369,12 +369,17 @@ public static function model($name = '', $layer = 'model', $appendSuffix = false if (isset(self::$instance[$guid])) { return self::$instance[$guid]; } - if (strpos($name, '/')) { - list($module, $name) = explode('/', $name, 2); - } else { + if (false !== strpos($name, '\\')) { + $class = $name; $module = Request::instance()->module(); + } else { + if (strpos($name, '/')) { + list($module, $name) = explode('/', $name, 2); + } else { + $module = Request::instance()->module(); + } + $class = self::parseClass($module, $layer, $name, $appendSuffix); } - $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { $model = new $class(); } else { @@ -400,12 +405,17 @@ public static function model($name = '', $layer = 'model', $appendSuffix = false */ public static function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '') { - if (strpos($name, '/')) { - list($module, $name) = explode('/', $name); - } else { + if (false !== strpos($name, '\\')) { + $class = $name; $module = Request::instance()->module(); + } else { + if (strpos($name, '/')) { + list($module, $name) = explode('/', $name); + } else { + $module = Request::instance()->module(); + } + $class = self::parseClass($module, $layer, $name, $appendSuffix); } - $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { return App::invokeClass($class); } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { @@ -432,12 +442,17 @@ public static function validate($name = '', $layer = 'validate', $appendSuffix = if (isset(self::$instance[$guid])) { return self::$instance[$guid]; } - if (strpos($name, '/')) { - list($module, $name) = explode('/', $name); - } else { + if (false !== strpos($name, '\\')) { + $class = $name; $module = Request::instance()->module(); + } else { + if (strpos($name, '/')) { + list($module, $name) = explode('/', $name); + } else { + $module = Request::instance()->module(); + } + $class = self::parseClass($module, $layer, $name, $appendSuffix); } - $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { $validate = new $class; } else { @@ -509,14 +524,16 @@ public static function parseName($name, $type = 0, $ucfirst = true) } /****************** 修改结束 ********************/ - /* if ($type) { + /* + if ($type) { $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) { return strtoupper($match[1]); }, $name); return $ucfirst ? ucfirst($name) : lcfirst($name); } else { return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); - }*/ + } + */ } /** diff --git a/thinkphp/library/think/Log.php b/thinkphp/library/think/Log.php index 413e276..4a8b0b4 100644 --- a/thinkphp/library/think/Log.php +++ b/thinkphp/library/think/Log.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/Model.php b/thinkphp/library/think/Model.php index 80c224a..34bef23 100644 --- a/thinkphp/library/think/Model.php +++ b/thinkphp/library/think/Model.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,14 +12,9 @@ namespace think; use InvalidArgumentException; -use think\Cache; -use think\Collection; -use think\Config; -use think\Db; use think\db\Query; -use think\Exception; use think\Exception\ValidateException; -use think\Loader; +use think\model\Collection as ModelCollection; use think\model\Relation; use think\model\relation\BelongsTo; use think\model\relation\BelongsToMany; @@ -28,23 +23,11 @@ use think\model\relation\HasOne; use think\model\relation\MorphMany; use think\model\relation\MorphTo; -use think\paginator\Collection as PaginatorCollection; /** * Class Model * @package think - * @method static PaginatorCollection paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询 - * @method static mixed value($field, $default = null) 得到某个字段的值 - * @method static array column($field, $key = '') 得到某个列的数组 - * @method static integer count($field = '*') COUNT查询 - * @method static integer sum($field = '*') SUM查询 - * @method static integer min($field = '*') MIN查询 - * @method static integer max($field = '*') MAX查询 - * @method static integer avg($field = '*') AVG查询 - * @method static setField($field, $value = '') - * @method static Query where($field, $op = null, $condition = null) 指定AND查询条件 - * @method static static findOrFail($data = null) 查找单条记录 如果不存在则抛出异常 - * + * @mixin Query */ abstract class Model implements \JsonSerializable, \ArrayAccess { @@ -96,7 +79,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 更新时间字段 protected $updateTime = 'update_time'; // 时间字段取出后的默认时间格式 - protected $dateFormat = 'Y-m-d H:i:s'; + protected $dateFormat; // 字段类型或者格式转换 protected $type = []; // 是否为更新数据 @@ -113,8 +96,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $batchValidate = false; // 查询数据集对象 protected $resultSetType; + // 关联自动写入 + protected $relationWrite; // protected static $db; + // 是否开启自动时间戳格式转换 + protected $autoDatetimeFormat; /** * 初始化过的模型. @@ -124,7 +111,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static $initialized = []; /** - * 架构函数 + * 构造方法 * @access public * @param array|object $data 数据 */ @@ -151,9 +138,22 @@ public function __construct($data = []) if (is_null($this->autoWriteTimestamp)) { // 自动写入时间戳 - $this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp'); + $this->autoWriteTimestamp = $this->db(false)->getConfig('auto_timestamp'); } + if (is_null($this->dateFormat)) { + // 设置时间戳格式 + $this->dateFormat = $this->db(false)->getConfig('datetime_format'); + } + + if (is_null($this->autoDatetimeFormat)) { + // 开启时间戳自动转换 + $this->autoDatetimeFormat = $this->db(false)->getConfig('auto_datetime_format'); + } + + if (is_null($this->resultSetType)) { + $this->resultSetType = $this->db(false)->getConfig('resultset_type'); + } // 执行初始化操作 $this->initialize(); } @@ -179,7 +179,7 @@ public function db($baseQuery = true) $connection = []; } // 设置当前模型 确保查询返回模型对象 - $query = Db::connect($connection)->model($model, $this->query); + $query = Db::connect($connection)->getQuery($model, $this->query); // 设置当前数据表和模型名 if (!empty($this->table)) { @@ -222,12 +222,13 @@ protected function initialize() * @return void */ protected static function init() - {} + { + } /** * 设置数据对象值 * @access public - * @param mixed $data 数据或者属性名 + * @param mixed $data 数据或者属性名 * @param mixed $value 值 * @return $this */ @@ -274,9 +275,9 @@ public function getData($name = null) /** * 修改器 设置数据对象值 * @access public - * @param string $name 属性名 - * @param mixed $value 属性值 - * @param array $data 数据 + * @param string $name 属性名 + * @param mixed $value 属性值 + * @param array $data 数据 * @return $this */ public function setAttr($name, $value, $data = []) @@ -296,9 +297,11 @@ public function setAttr($name, $value, $data = []) } // 标记字段更改 - if (isset($this->data[$name]) && is_scalar($this->data[$name]) && is_scalar($value) && 0 !== strcmp($this->data[$name], $value)) { + if (!isset($this->data[$name])) { $this->change[] = $name; - } elseif (!isset($this->data[$name]) || $value != $this->data[$name]) { + } elseif (is_scalar($value) && is_scalar($this->data[$name]) && 0 !== strcmp($this->data[$name], $value)) { + $this->change[] = $name; + } elseif (!is_object($value) && $value != $this->data[$name]) { $this->change[] = $name; } // 设置数据对象属性 @@ -309,7 +312,7 @@ public function setAttr($name, $value, $data = []) /** * 自动写入时间戳 * @access public - * @param string $name 时间戳字段 + * @param string $name 时间戳字段 * @return mixed */ protected function autoWriteTimestamp($name) @@ -323,7 +326,7 @@ protected function autoWriteTimestamp($name) case 'datetime': case 'date': $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, $_SERVER['REQUEST_TIME']); + $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $format); break; case 'timestamp': case 'integer': @@ -331,19 +334,42 @@ protected function autoWriteTimestamp($name) $value = $_SERVER['REQUEST_TIME']; break; } - } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { - $value = date($this->dateFormat, $_SERVER['REQUEST_TIME']); + } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ + 'datetime', + 'date', + 'timestamp', + ]) + ) { + $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); } else { - $value = $_SERVER['REQUEST_TIME']; + $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat, true); } return $value; } + /** + * 时间日期字段格式化处理 + * @access public + * @param mixed $time 时间日期表达式 + * @param mixed $format 日期格式 + * @param bool $timestamp 是否进行时间戳转换 + * @return mixed + */ + protected function formatDateTime($time, $format, $timestamp = false) + { + if (false !== strpos($format, '\\')) { + $time = new $format($time); + } elseif (!$timestamp && false !== $format) { + $time = date($format, $time); + } + return $time; + } + /** * 数据写入 类型转换 * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 + * @param mixed $value 值 + * @param string|array $type 要转换的类型 * @return mixed */ protected function writeTransform($value, $type) @@ -374,7 +400,8 @@ protected function writeTransform($value, $type) break; case 'datetime': $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, is_numeric($value) ? $value : strtotime($value)); + $value = is_numeric($value) ? $value : strtotime($value); + $value = $this->formatDateTime($value, $format); break; case 'object': if (is_object($value)) { @@ -390,6 +417,7 @@ protected function writeTransform($value, $type) case 'serialize': $value = serialize($value); break; + } return $value; } @@ -418,9 +446,25 @@ public function getAttr($name) } elseif (isset($this->type[$name])) { // 类型转换 $value = $this->readTransform($value, $this->type[$name]); + } elseif (in_array($name, [$this->createTime, $this->updateTime])) { + // 可以关闭自动时间戳转换 + if ($this->autoDatetimeFormat) { + if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ + 'datetime', + 'date', + 'timestamp', + ]) + ) { + $value = $this->formatDateTime(strtotime($value), $this->dateFormat); + } else { + $value = $this->formatDateTime($value, $this->dateFormat); + } + } } elseif ($notFound) { $method = Loader::parseName($name, 1, false); if (method_exists($this, $method) && $this->$method() instanceof Relation) { + // 清空之前的查询参数 + $this->$method()->removeOption(); // 不存在该字段 获取关联数据 $value = $this->$method()->getRelation(); // 保存关联对象值 @@ -435,8 +479,8 @@ public function getAttr($name) /** * 数据读取 类型转换 * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 + * @param mixed $value 值 + * @param string|array $type 要转换的类型 * @return mixed */ protected function readTransform($value, $type) @@ -463,20 +507,20 @@ protected function readTransform($value, $type) case 'timestamp': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, $value); + $value = $this->formatDateTime($value, $format); } break; case 'datetime': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, strtotime($value)); + $value = $this->formatDateTime(strtotime($value), $format); } break; case 'json': $value = json_decode($value, true); break; case 'array': - $value = is_null($value) ? [] : json_decode($value, true); + $value = empty($value) ? [] : json_decode($value, true); break; case 'object': $value = empty($value) ? new \stdClass() : json_decode($value); @@ -484,6 +528,11 @@ protected function readTransform($value, $type) case 'serialize': $value = unserialize($value); break; + default: + if (false !== strpos($type, '\\')) { + // 对象类型 + $value = new $type($value); + } } return $value; } @@ -491,8 +540,8 @@ protected function readTransform($value, $type) /** * 设置需要追加的输出属性 * @access public - * @param array $append 属性列表 - * @param bool $override 是否覆盖 + * @param array $append 属性列表 + * @param bool $override 是否覆盖 * @return $this */ public function append($append = [], $override = false) @@ -504,9 +553,10 @@ public function append($append = [], $override = false) /** * 设置附加关联对象的属性 * @access public - * @param string $relation 关联方法 - * @param string|array $append 追加属性名 + * @param string $relation 关联方法 + * @param string|array $append 追加属性名 * @return $this + * @throws Exception */ public function appendRelationAttr($relation, $append) { @@ -530,8 +580,8 @@ public function appendRelationAttr($relation, $append) /** * 设置需要隐藏的输出属性 * @access public - * @param array $hidden 属性列表 - * @param bool $override 是否覆盖 + * @param array $hidden 属性列表 + * @param bool $override 是否覆盖 * @return $this */ public function hidden($hidden = [], $override = false) @@ -542,8 +592,9 @@ public function hidden($hidden = [], $override = false) /** * 设置需要输出的属性 + * @access public * @param array $visible - * @param bool $override 是否覆盖 + * @param bool $override 是否覆盖 * @return $this */ public function visible($visible = [], $override = false) @@ -552,6 +603,56 @@ public function visible($visible = [], $override = false) return $this; } + /** + * 解析隐藏及显示属性 + * @access protected + * @param array $attrs 属性 + * @param array $result 结果集 + * @param bool $visible + * @return array + */ + protected function parseAttr($attrs, &$result, $visible = true) + { + $array = []; + foreach ($attrs as $key => $val) { + if (is_array($val)) { + if ($visible) { + $array[] = $key; + } + $result[$key] = $val; + } elseif (strpos($val, '.')) { + list($key, $name) = explode('.', $val); + if ($visible) { + $array[] = $key; + } + $result[$key][] = $name; + } else { + $array[] = $val; + } + } + return $array; + } + + /** + * 转换子模型对象 + * @access protected + * @param Model|ModelCollection $model + * @param $visible + * @param $hidden + * @param $key + * @return array + */ + protected function subToArray($model, $visible, $hidden, $key) + { + // 关联模型对象 + if (isset($visible[$key])) { + $model->visible($visible[$key]); + } elseif (isset($hidden[$key])) { + $model->hidden($hidden[$key]); + } + return $model->toArray(); + } + /** * 转换当前模型对象为数组 * @access public @@ -559,26 +660,29 @@ public function visible($visible = [], $override = false) */ public function toArray() { - $item = []; - - //过滤属性 + $item = []; + $visible = []; + $hidden = []; + // 过滤属性 if (!empty($this->visible)) { - $data = array_intersect_key($this->data, array_flip($this->visible)); + $array = $this->parseAttr($this->visible, $visible); + $data = array_intersect_key($this->data, array_flip($array)); } elseif (!empty($this->hidden)) { - $data = array_diff_key($this->data, array_flip($this->hidden)); + $array = $this->parseAttr($this->hidden, $hidden, false); + $data = array_diff_key($this->data, array_flip($array)); } else { $data = $this->data; } foreach ($data as $key => $val) { - if ($val instanceof Model || $val instanceof Collection) { + if ($val instanceof Model || $val instanceof ModelCollection) { // 关联模型对象 - $item[$key] = $val->toArray(); + $item[$key] = $this->subToArray($val, $visible, $hidden, $key); } elseif (is_array($val) && reset($val) instanceof Model) { // 关联模型数据集 $arr = []; foreach ($val as $k => $value) { - $arr[$k] = $value->toArray(); + $arr[$k] = $this->subToArray($value, $visible, $hidden, $k); } $item[$key] = $arr; } else { @@ -588,8 +692,19 @@ public function toArray() } // 追加属性(必须定义获取器) if (!empty($this->append)) { - foreach ($this->append as $name) { - $item[$name] = $this->getAttr($name); + foreach ($this->append as $key => $name) { + if (is_array($name)) { + // 追加关联对象属性 + $relation = $this->getAttr($key); + $item[$key] = $relation->append($name)->toArray(); + } elseif (strpos($name, '.')) { + list($key, $attr) = explode('.', $name); + // 追加关联对象属性 + $relation = $this->getAttr($key); + $item[$key] = $relation->append([$attr])->toArray(); + } else { + $item[$name] = $this->getAttr($name); + } } } return !empty($item) ? $item : []; @@ -598,7 +713,7 @@ public function toArray() /** * 转换当前模型对象为JSON字符串 * @access public - * @param integer $options json参数 + * @param integer $options json参数 * @return string */ public function toJson($options = JSON_UNESCAPED_UNICODE) @@ -609,15 +724,15 @@ public function toJson($options = JSON_UNESCAPED_UNICODE) /** * 转换当前模型数据集为数据集对象 * @access public - * @param array|Collection $collection 数据集 - * @return Collection + * @param array|\think\Collection $collection 数据集 + * @return \think\Collection */ public function toCollection($collection) { if ($this->resultSetType) { if ('collection' == $this->resultSetType) { - $collection = new Collection($collection); - } else { + $collection = new ModelCollection($collection); + } elseif (false !== strpos($this->resultSetType, '\\')) { $class = $this->resultSetType; $collection = new $class($collection); } @@ -625,6 +740,21 @@ public function toCollection($collection) return $collection; } + /** + * 关联数据一起更新 + * @access public + * @param mixed $relation 关联 + * @return $this + */ + public function together($relation) + { + if (is_string($relation)) { + $relation = explode(',', $relation); + } + $this->relationWrite = $relation; + return $this; + } + /** * 获取模型对象的主键 * @access public @@ -634,10 +764,10 @@ public function toCollection($collection) public function getPk($name = '') { if (!empty($name)) { - $table = $this->db()->getTable($name); - return $this->db()->getPk($table); + $table = $this->db(false)->getTable($name); + return $this->db(false)->getPk($table); } elseif (empty($this->pk)) { - $this->pk = $this->db()->getPk(); + $this->pk = $this->db(false)->getPk(); } return $this->pk; } @@ -662,9 +792,9 @@ protected function isPk($key) /** * 保存当前数据对象 * @access public - * @param array $data 数据 - * @param array $where 更新条件 - * @param string $sequence 自增序列名 + * @param array $data 数据 + * @param array $where 更新条件 + * @param string $sequence 自增序列名 * @return integer|false */ public function save($data = [], $where = [], $sequence = null) @@ -683,8 +813,32 @@ public function save($data = [], $where = [], $sequence = null) } } + // 自动关联写入 + if (!empty($this->relationWrite)) { + $relation = []; + foreach ($this->relationWrite as $key => $name) { + if (!is_numeric($key)) { + $relation[$key] = []; + foreach ($name as $val) { + if (isset($this->data[$val])) { + $relation[$key][$val] = $this->data[$val]; + unset($this->data[$val]); + } + } + } elseif (isset($this->data[$name])) { + $relation[$name] = $this->data[$name]; + if (!$this->isUpdate) { + unset($this->data[$name]); + } + } + } + } + // 检测字段 if (!empty($this->field)) { + if (true === $this->field) { + $this->field = $this->db(false)->getTableInfo('', 'fields'); + } foreach ($this->data as $key => $val) { if (!in_array($key, $this->field)) { unset($this->data[$key]); @@ -696,7 +850,7 @@ public function save($data = [], $where = [], $sequence = null) $this->autoCompleteData($this->auto); // 自动写入更新时间 - if ($this->autoWriteTimestamp && $this->updateTime) { + if ($this->autoWriteTimestamp && $this->updateTime && (empty($this->change) || !in_array($this->updateTime, $this->change))) { $this->setAttr($this->updateTime, null); } @@ -743,7 +897,33 @@ public function save($data = [], $where = [], $sequence = null) unset($data[$pk]); } + // 关联更新 + if (isset($relation)) { + foreach ($relation as $name => $val) { + if (isset($data[$name])) { + unset($data[$name]); + } + } + } + + // 模型更新 $result = $this->db()->where($where)->update($data); + + // 关联更新 + if (isset($relation)) { + foreach ($relation as $name => $val) { + if ($val instanceof Model) { + $val->save(); + } else { + unset($this->data[$name]); + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->save($val); + } + } + } + } + // 清空change $this->change = []; // 更新回调 @@ -753,7 +933,7 @@ public function save($data = [], $where = [], $sequence = null) $this->autoCompleteData($this->insert); // 自动写入创建时间 - if ($this->autoWriteTimestamp && $this->createTime) { + if ($this->autoWriteTimestamp && $this->createTime && (empty($this->change) || !in_array($this->createTime, $this->change))) { $this->setAttr($this->createTime, null); } @@ -770,6 +950,15 @@ public function save($data = [], $where = [], $sequence = null) $this->data[$pk] = $insertId; } } + + // 关联写入 + if (isset($relation)) { + foreach ($relation as $name => $val) { + $method = Loader::parseName($name, 1, false); + $this->$method()->save($val); + } + } + // 标记为更新 $this->isUpdate = true; // 清空change @@ -786,9 +975,10 @@ public function save($data = [], $where = [], $sequence = null) /** * 保存多个数据到当前数据对象 * @access public - * @param array $dataSet 数据 - * @param boolean $replace 是否自动识别更新和写入 + * @param array $dataSet 数据 + * @param boolean $replace 是否自动识别更新和写入 * @return array|false + * @throws \Exception */ public function saveAll($dataSet, $replace = true) { @@ -828,18 +1018,33 @@ public function saveAll($dataSet, $replace = true) /** * 设置允许写入的字段 * @access public - * @param bool|array $field 允许写入的字段 如果为true只允许写入数据表字段 + * @param mixed $field 允许写入的字段 如果为true只允许写入数据表字段 * @return $this */ public function allowField($field) { - if (true === $field) { - $field = $this->db()->getTableInfo('', 'fields'); + if (is_string($field)) { + $field = explode(',', $field); } $this->field = $field; return $this; } + /** + * 设置只读字段 + * @access public + * @param mixed $field 只读字段 + * @return $this + */ + public function readonly($field) + { + if (is_string($field)) { + $field = explode(',', $field); + } + $this->readonly = $field; + return $this; + } + /** * 是否为更新数据 * @access public @@ -886,7 +1091,29 @@ public function delete() return false; } - $result = $this->db()->delete($this->data); + // 删除条件 + $pk = $this->getPk(); + if (is_string($pk) && isset($this->data[$pk])) { + $where = [$pk => $this->data[$pk]]; + } elseif (!empty($this->updateWhere)) { + $where = $this->updateWhere; + } else { + $where = null; + } + + // 删除当前模型数据 + $result = $this->db()->where($where)->delete(); + + // 关联删除 + if (!empty($this->relationWrite)) { + foreach ($this->relationWrite as $key => $name) { + $name = is_numeric($key) ? $name : $key; + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->delete(); + } + } + } $this->trigger('after_delete', $this); return $result; @@ -907,8 +1134,8 @@ public function auto($fields) /** * 设置字段验证 * @access public - * @param array|string|bool $rule 验证规则 true表示自动读取验证器类 - * @param array $msg 提示信息 + * @param array|string|bool $rule 验证规则 true表示自动读取验证器类 + * @param array $msg 提示信息 * @param bool $batch 批量验证 * @return $this */ @@ -941,8 +1168,8 @@ public function validateFailException($fail = true) /** * 自动验证数据 * @access protected - * @param array $data 验证数据 - * @param mixed $rule 验证规则 + * @param array $data 验证数据 + * @param mixed $rule 验证规则 * @param bool $batch 批量验证 * @return bool */ @@ -993,9 +1220,9 @@ public function getError() /** * 注册回调方法 * @access public - * @param string $event 事件名 - * @param callable $callback 回调方法 - * @param bool $override 是否覆盖 + * @param string $event 事件名 + * @param callable $callback 回调方法 + * @param bool $override 是否覆盖 * @return void */ public static function event($event, $callback, $override = false) @@ -1010,8 +1237,8 @@ public static function event($event, $callback, $override = false) /** * 触发事件 * @access protected - * @param string $event 事件名 - * @param mixed $params 传入参数(引用) + * @param string $event 事件名 + * @param mixed $params 传入参数(引用) * @return bool */ protected function trigger($event, &$params) @@ -1032,8 +1259,8 @@ protected function trigger($event, &$params) /** * 写入数据 * @access public - * @param array $data 数据数组 - * @param array|true $field 允许字段 + * @param array $data 数据数组 + * @param array|true $field 允许字段 * @return $this */ public static function create($data = [], $field = null) @@ -1049,9 +1276,9 @@ public static function create($data = [], $field = null) /** * 更新数据 * @access public - * @param array $data 数据数组 - * @param array $where 更新条件 - * @param array|true $field 允许字段 + * @param array $data 数据数组 + * @param array $where 更新条件 + * @param array|true $field 允许字段 * @return $this */ public static function update($data = [], $where = [], $field = null) @@ -1075,6 +1302,10 @@ public static function update($data = [], $where = [], $field = null) */ public static function get($data = null, $with = [], $cache = false) { + if (true === $with || is_int($with)) { + $cache = $with; + $with = []; + } $query = static::parseQuery($data, $with, $cache); return $query->find($data); } @@ -1090,6 +1321,10 @@ public static function get($data = null, $with = [], $cache = false) */ public static function all($data = null, $with = [], $cache = false) { + if (true === $with || is_int($with)) { + $cache = $with; + $with = []; + } $query = static::parseQuery($data, $with, $cache); return $query->select($data); } @@ -1097,9 +1332,9 @@ public static function all($data = null, $with = [], $cache = false) /** * 分析查询表达式 * @access public - * @param mixed $data 主键列表或者查询条件(闭包) - * @param string $with 关联预查询 - * @param bool $cache 是否缓存 + * @param mixed $data 主键列表或者查询条件(闭包) + * @param string $with 关联预查询 + * @param bool $cache 是否缓存 * @return Query */ protected static function parseQuery(&$data, $with, $cache) @@ -1151,9 +1386,9 @@ public static function destroy($data) /** * 命名范围 * @access public - * @param string|array|Closure $name 命名范围名称 逗号分隔 - * @param mixed ...$params 参数调用 - * @return Model + * @param string|array|\Closure $name 命名范围名称 逗号分隔 + * @internal mixed ...$params 参数调用 + * @return Model|Query */ public static function scope($name) { @@ -1181,43 +1416,45 @@ public static function scope($name) /** * 设置是否使用全局查询范围 - * @param bool $use 是否启用全局查询范围 + * @param bool $use 是否启用全局查询范围 * @access public * @return Model */ public static function useGlobalScope($use) { - $model = new static(); - self::$db = $model->db($use); + $model = new static(); + static::$db = $model->db($use); return $model; } /** * 根据关联条件查询当前模型 * @access public - * @param string $relation 关联方法名 - * @param string $operator 比较操作符 - * @param integer $count 个数 - * @param string $id 关联表的统计字段 - * @return Model + * @param string $relation 关联方法名 + * @param mixed $operator 比较操作符 + * @param integer $count 个数 + * @param string $id 关联表的统计字段 + * @return Relation|Query */ public static function has($relation, $operator = '>=', $count = 1, $id = '*') { - $model = new static(); - return $model->$relation()->has($model, $operator, $count, $id); + $relation = (new static())->$relation(); + if (is_array($operator) || $operator instanceof \Closure) { + return $relation->hasWhere($operator); + } + return $relation->has($operator, $count, $id); } /** * 根据关联条件查询当前模型 * @access public - * @param string $relation 关联方法名 - * @param mixed $where 查询条件(数组或者闭包) - * @return Model + * @param string $relation 关联方法名 + * @param mixed $where 查询条件(数组或者闭包) + * @return Relation|Query */ public static function hasWhere($relation, $where = []) { - $model = new static(); - return $model->$relation()->hasWhere($model, $where); + return (new static())->$relation()->hasWhere($where); } /** @@ -1249,8 +1486,22 @@ public function relationQuery($relations) $relations = explode(',', $relations); } - foreach ($relations as $relation) { - $this->data[$relation] = $this->$relation()->getRelation(); + foreach ($relations as $key => $relation) { + $subRelation = ''; + $closure = null; + if ($relation instanceof \Closure) { + // 支持闭包查询过滤关联条件 + $closure = $relation; + $relation = $key; + } + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { + list($relation, $subRelation) = explode('.', $relation, 2); + } + $method = Loader::parseName($relation, 1, false); + $this->data[$relation] = $this->$method()->getRelation($subRelation, $closure); } return $this; } @@ -1258,12 +1509,11 @@ public function relationQuery($relations) /** * 预载入关联查询 返回数据集 * @access public - * @param array $resultSet 数据集 - * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 + * @param array $resultSet 数据集 + * @param string $relation 关联名 * @return array */ - public function eagerlyResultSet(&$resultSet, $relation, $class = '') + public function eagerlyResultSet(&$resultSet, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $key => $relation) { @@ -1273,23 +1523,25 @@ public function eagerlyResultSet(&$resultSet, $relation, $class = '') $closure = $relation; $relation = $key; } - if (strpos($relation, '.')) { - list($relation, $subRelation) = explode('.', $relation); + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { + list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure); } } /** * 预载入关联查询 返回模型对象 * @access public - * @param Model $result 数据对象 - * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 + * @param Model $result 数据对象 + * @param string $relation 关联名 * @return Model */ - public function eagerlyResult(&$result, $relation, $class = '') + public function eagerlyResult(&$result, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -1300,22 +1552,68 @@ public function eagerlyResult(&$result, $relation, $class = '') $closure = $relation; $relation = $key; } - if (strpos($relation, '.')) { - list($relation, $subRelation) = explode('.', $relation); + if (is_array($relation)) { + $subRelation = $relation; + $relation = $key; + } elseif (strpos($relation, '.')) { + list($relation, $subRelation) = explode('.', $relation, 2); + } + $relation = Loader::parseName($relation, 1, false); + $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure); + } + } + + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param string|array $relation 关联名 + * @return void + */ + public function relationCount(&$result, $relation) + { + $relations = is_string($relation) ? explode(',', $relation) : $relation; + + foreach ($relations as $key => $relation) { + $closure = false; + if ($relation instanceof \Closure) { + $closure = $relation; + $relation = $key; + } elseif (is_string($key)) { + $name = $relation; + $relation = $key; } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class); + $count = $this->$relation()->relationCount($result, $closure); + if (!isset($name)) { + $name = Loader::parseName($relation) . '_count'; + } + $result->setAttr($name, $count); } } + /** + * 获取模型的默认外键名 + * @access public + * @param string $name 模型名 + * @return string + */ + protected function getForeignKey($name) + { + if (strpos($name, '\\')) { + $name = basename(str_replace('\\', '/', $name)); + } + return Loader::parseName($name) . '_id'; + } + /** * HAS ONE 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 - * @param array $alias 别名定义 - * @param string $joinType JOIN类型 + * @param string $localKey 关联主键 + * @param array $alias 别名定义(已经废弃) + * @param string $joinType JOIN类型 * @return HasOne */ public function hasOne($model, $foreignKey = '', $localKey = '', $alias = [], $joinType = 'INNER') @@ -1323,97 +1621,93 @@ public function hasOne($model, $foreignKey = '', $localKey = '', $alias = [], $j // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return new HasOne($this, $model, $foreignKey, $localKey, $alias, $joinType); + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); + return new HasOne($this, $model, $foreignKey, $localKey, $joinType); } /** * BELONGS TO 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $otherKey 关联主键 - * @param array $alias 别名定义 - * @param string $joinType JOIN类型 + * @param string $localKey 关联主键 + * @param array $alias 别名定义(已经废弃) + * @param string $joinType JOIN类型 * @return BelongsTo */ - public function belongsTo($model, $foreignKey = '', $otherKey = '', $alias = [], $joinType = 'INNER') + public function belongsTo($model, $foreignKey = '', $localKey = '', $alias = [], $joinType = 'INNER') { // 记录当前关联信息 $model = $this->parseModel($model); - $foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; - $otherKey = $otherKey ?: (new $model)->getPk(); - return new BelongsTo($this, $model, $foreignKey, $otherKey, $alias, $joinType); + $foreignKey = $foreignKey ?: $this->getForeignKey($model); + $localKey = $localKey ?: (new $model)->getPk(); + return new BelongsTo($this, $model, $foreignKey, $localKey, $joinType); } /** * HAS MANY 关联定义 * @access public - * @param string $model 模型名 + * @param string $model 模型名 * @param string $foreignKey 关联外键 - * @param string $localKey 关联主键 - * @param array $alias 别名定义 + * @param string $localKey 关联主键 * @return HasMany */ - public function hasMany($model, $foreignKey = '', $localKey = '', $alias = []) + public function hasMany($model, $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return new HasMany($this, $model, $foreignKey, $localKey, $alias); + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); + return new HasMany($this, $model, $foreignKey, $localKey); } /** * HAS MANY 远程关联定义 * @access public - * @param string $model 模型名 - * @param string $through 中间模型名 + * @param string $model 模型名 + * @param string $through 中间模型名 * @param string $foreignKey 关联外键 * @param string $throughKey 关联外键 - * @param string $localKey 关联主键 - * @param array $alias 别名定义 + * @param string $localKey 关联主键 * @return HasManyThrough */ - public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = []) + public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $through = $this->parseModel($through); $localKey = $localKey ?: $this->getPk(); - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - $name = Loader::parseName(basename(str_replace('\\', '/', $through))); - $throughKey = $throughKey ?: $name . '_id'; - return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $alias); + $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); + $throughKey = $throughKey ?: $this->getForeignKey($through); + return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey); } /** * BELONGS TO MANY 关联定义 * @access public - * @param string $model 模型名 - * @param string $table 中间表名 + * @param string $model 模型名 + * @param string $table 中间表名 * @param string $foreignKey 关联外键 - * @param string $localKey 当前模型关联键 - * @param array $alias 别名定义 + * @param string $localKey 当前模型关联键 * @return BelongsToMany */ - public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = []) + public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); - $table = $table ?: $this->db()->getTable(Loader::parseName($this->name) . '_' . $name); + $table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name); $foreignKey = $foreignKey ?: $name . '_id'; - $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; - return new BelongsToMany($this, $model, $table, $foreignKey, $localKey, $alias); + $localKey = $localKey ?: $this->getForeignKey($this->name); + return new BelongsToMany($this, $model, $table, $foreignKey, $localKey); } /** * MORPH MANY 关联定义 * @access public - * @param string $model 模型名 - * @param string|array $morph 多态字段信息 - * @param string $type 多态类型 + * @param string $model 模型名 + * @param string|array $morph 多态字段信息 + * @param string $type 多态类型 * @return MorphMany */ public function morphMany($model, $morph = null, $type = '') @@ -1437,8 +1731,8 @@ public function morphMany($model, $morph = null, $type = '') /** * MORPH TO 关联定义 * @access public - * @param string|array $morph 多态字段信息 - * @param array $alias 多态别名定义 + * @param string|array $morph 多态字段信息 + * @param array $alias 多态别名定义 * @return MorphTo */ public function morphTo($morph = null, $alias = []) @@ -1459,7 +1753,13 @@ public function morphTo($morph = null, $alias = []) public function __call($method, $args) { - $query = $this->db(); + if (isset(static::$db)) { + $query = static::$db; + static::$db = null; + } else { + $query = $this->db(); + } + if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; @@ -1474,7 +1774,8 @@ public function __call($method, $args) public static function __callStatic($method, $params) { if (isset(static::$db)) { - $query = static::$db; + $query = static::$db; + static::$db = null; } else { $query = (new static())->db(); } @@ -1485,8 +1786,8 @@ public static function __callStatic($method, $params) /** * 修改器 设置数据对象的值 * @access public - * @param string $name 名称 - * @param mixed $value 值 + * @param string $name 名称 + * @param mixed $value 值 * @return void */ public function __set($name, $value) @@ -1579,6 +1880,8 @@ public function __wakeup() /** * 模型事件快捷方法 + * @param $callback + * @param bool $override */ protected static function beforeInsert($callback, $override = false) { diff --git a/thinkphp/library/think/Paginator.php b/thinkphp/library/think/Paginator.php index 7385ebb..d3547f8 100644 --- a/thinkphp/library/think/Paginator.php +++ b/thinkphp/library/think/Paginator.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,15 +11,19 @@ namespace think; -use think\paginator\Collection as PaginatorCollection; -use think\Request; +use ArrayAccess; +use ArrayIterator; +use Countable; +use IteratorAggregate; +use JsonSerializable; +use Traversable; -abstract class Paginator +abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { /** @var bool 是否为简洁模式 */ protected $simple = false; - /** @var PaginatorCollection 数据集 */ + /** @var Collection 数据集 */ protected $items; /** @var integer 当前页 */ @@ -42,33 +46,33 @@ abstract class Paginator 'var_page' => 'page', 'path' => '/', 'query' => [], - 'fragment' => '' + 'fragment' => '', ]; - protected function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) + public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) { $this->options = array_merge($this->options, $options); - $this->options['path'] = $this->options['path'] != '/' ? rtrim($this->options['path'], '/') : $this->options['path']; + $this->options['path'] = '/' != $this->options['path'] ? rtrim($this->options['path'], '/') : $this->options['path']; $this->simple = $simple; $this->listRows = $listRows; + if (!$items instanceof Collection) { + $items = Collection::make($items); + } + if ($simple) { - if (!$items instanceof Collection) { - $items = Collection::make($items); - } $this->currentPage = $this->setCurrentPage($currentPage); $this->hasMore = count($items) > ($this->listRows); $items = $items->slice(0, $this->listRows); } else { $this->total = $total; - $this->lastPage = (int)ceil($total / $listRows); + $this->lastPage = (int) ceil($total / $listRows); $this->currentPage = $this->setCurrentPage($currentPage); $this->hasMore = $this->currentPage < $this->lastPage; } - - $this->items = PaginatorCollection::make($items, $this); + $this->items = $items; } /** @@ -78,12 +82,11 @@ protected function __construct($items, $listRows, $currentPage = null, $total = * @param bool $simple * @param null $total * @param array $options - * @return PaginatorCollection + * @return Paginator */ public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) { - $paginator = new static($items, $listRows, $currentPage, $total, $simple, $options); - return $paginator->items; + return new static($items, $listRows, $currentPage, $total, $simple, $options); } protected function setCurrentPage($currentPage) @@ -134,7 +137,7 @@ public static function getCurrentPage($varPage = 'page', $default = 1) { $page = Request::instance()->request($varPage); - if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) { + if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { return $page; } @@ -182,7 +185,7 @@ public function lastPage() */ public function hasPages() { - return !($this->currentPage == 1 && !$this->hasMore); + return !(1 == $this->currentPage && !$this->hasMore); } /** @@ -239,7 +242,6 @@ public function appends($key, $value = null) return $this; } - /** * 构造锚点字符串 * @@ -255,4 +257,113 @@ protected function buildFragment() * @return mixed */ abstract public function render(); -} \ No newline at end of file + + public function items() + { + return $this->items->all(); + } + + public function getCollection() + { + return $this->items; + } + + public function isEmpty() + { + return $this->items->isEmpty(); + } + + /** + * Retrieve an external iterator + * @return Traversable An instance of an object implementing Iterator or + * Traversable + */ + public function getIterator() + { + return new ArrayIterator($this->items->all()); + } + + /** + * Whether a offset exists + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) + { + return $this->items->offsetExists($offset); + } + + /** + * Offset to retrieve + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->items->offsetGet($offset); + } + + /** + * Offset to set + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + $this->items->offsetSet($offset, $value); + } + + /** + * Offset to unset + * @param mixed $offset + * @return void + * @since 5.0.0 + */ + public function offsetUnset($offset) + { + $this->items->offsetUnset($offset); + } + + /** + * Count elements of an object + */ + public function count() + { + return $this->items->count(); + } + + public function __toString() + { + return (string) $this->render(); + } + + public function toArray() + { + try { + $total = $this->total(); + } catch (\DomainException $e) { + $total = null; + } + + return [ + 'total' => $total, + 'per_page' => $this->listRows(), + 'current_page' => $this->currentPage(), + 'data' => $this->items->toArray(), + ]; + } + + /** + * Specify data which should be serialized to JSON + */ + public function jsonSerialize() + { + return $this->toArray(); + } + + public function __call($name, $arguments) + { + return call_user_func_array([$this->getCollection(), $name], $arguments); + } + +} diff --git a/thinkphp/library/think/Process.php b/thinkphp/library/think/Process.php index 1982de2..6f3faa3 100644 --- a/thinkphp/library/think/Process.php +++ b/thinkphp/library/think/Process.php @@ -14,9 +14,9 @@ use think\process\exception\Failed as ProcessFailedException; use think\process\exception\Timeout as ProcessTimeoutException; use think\process\pipes\Pipes; -use think\process\Utils; use think\process\pipes\Unix as UnixPipes; use think\process\pipes\Windows as WindowsPipes; +use think\process\Utils; class Process { @@ -47,10 +47,10 @@ class Process private $exitcode; private $fallbackExitcode; private $processInformation; - private $outputDisabled = false; + private $outputDisabled = false; private $stdout; private $stderr; - private $enhanceWindowsCompatibility = true; + private $enhanceWindowsCompatibility = true; private $enhanceSigchildCompatibility; private $process; private $status = self::STATUS_READY; @@ -147,7 +147,7 @@ public function __construct($commandline, $cwd = null, array $env = null, $input $this->enhanceSigchildCompatibility = '\\' !== DS && $this->isSigchildEnabled(); $this->options = array_replace([ 'suppress_errors' => true, - 'binary_pipes' => true + 'binary_pipes' => true, ], $options); } @@ -490,7 +490,7 @@ public function getExitCode() public function getExitCodeText() { if (null === $exitcode = $this->getExitCode()) { - return null; + return; } return isset(self::$exitCodes[$exitcode]) ? self::$exitCodes[$exitcode] : 'Unknown error'; @@ -586,7 +586,7 @@ public function isRunning() */ public function isStarted() { - return $this->status != self::STATUS_READY; + return self::STATUS_READY != $this->status; } /** @@ -597,7 +597,7 @@ public function isTerminated() { $this->updateStatus(false); - return $this->status == self::STATUS_TERMINATED; + return self::STATUS_TERMINATED == $this->status; } /** @@ -645,7 +645,7 @@ public function stop() * @param string $line */ public function addOutput($line) - { +{ $this->lastOutputTime = microtime(true); $this->stdout .= $line; } @@ -655,7 +655,7 @@ public function addOutput($line) * @param string $line */ public function addErrorOutput($line) - { +{ $this->lastOutputTime = microtime(true); $this->stderr .= $line; } @@ -665,7 +665,7 @@ public function addErrorOutput($line) * @return string */ public function getCommandLine() - { +{ return $this->commandline; } @@ -675,7 +675,7 @@ public function getCommandLine() * @return self */ public function setCommandLine($commandline) - { +{ $this->commandline = $commandline; return $this; @@ -686,7 +686,7 @@ public function setCommandLine($commandline) * @return float|null */ public function getTimeout() - { +{ return $this->timeout; } @@ -695,7 +695,7 @@ public function getTimeout() * @return float|null */ public function getIdleTimeout() - { +{ return $this->idleTimeout; } @@ -705,7 +705,7 @@ public function getIdleTimeout() * @return self */ public function setTimeout($timeout) - { +{ $this->timeout = $this->validateTimeout($timeout); return $this; @@ -717,7 +717,7 @@ public function setTimeout($timeout) * @return self */ public function setIdleTimeout($timeout) - { +{ if (null !== $timeout && $this->outputDisabled) { throw new \LogicException('Idle timeout can not be set while the output is disabled.'); } @@ -733,7 +733,7 @@ public function setIdleTimeout($timeout) * @return self */ public function setTty($tty) - { +{ if ('\\' === DS && $tty) { throw new \RuntimeException('TTY mode is not supported on Windows platform.'); } @@ -741,7 +741,7 @@ public function setTty($tty) throw new \RuntimeException('TTY mode requires /dev/tty to be readable.'); } - $this->tty = (bool)$tty; + $this->tty = (bool) $tty; return $this; } @@ -751,7 +751,7 @@ public function setTty($tty) * @return bool */ public function isTty() - { +{ return $this->tty; } @@ -761,8 +761,8 @@ public function isTty() * @return self */ public function setPty($bool) - { - $this->pty = (bool)$bool; +{ + $this->pty = (bool) $bool; return $this; } @@ -772,7 +772,7 @@ public function setPty($bool) * @return bool */ public function isPty() - { +{ return $this->pty; } @@ -781,7 +781,7 @@ public function isPty() * @return string|null */ public function getWorkingDirectory() - { +{ if (null === $this->cwd) { return getcwd() ?: null; } @@ -795,7 +795,7 @@ public function getWorkingDirectory() * @return self */ public function setWorkingDirectory($cwd) - { +{ $this->cwd = $cwd; return $this; @@ -806,7 +806,7 @@ public function setWorkingDirectory($cwd) * @return array */ public function getEnv() - { +{ return $this->env; } @@ -816,14 +816,14 @@ public function getEnv() * @return self */ public function setEnv(array $env) - { +{ $env = array_filter($env, function ($value) { return !is_array($value); }); $this->env = []; foreach ($env as $key => $value) { - $this->env[(binary)$key] = (binary)$value; + $this->env[(binary) $key] = (binary) $value; } return $this; @@ -834,7 +834,7 @@ public function setEnv(array $env) * @return null|string */ public function getInput() - { +{ return $this->input; } @@ -844,7 +844,7 @@ public function getInput() * @return self */ public function setInput($input) - { +{ if ($this->isRunning()) { throw new \LogicException('Input can not be set while the process is running.'); } @@ -859,7 +859,7 @@ public function setInput($input) * @return array */ public function getOptions() - { +{ return $this->options; } @@ -869,7 +869,7 @@ public function getOptions() * @return self */ public function setOptions(array $options) - { +{ $this->options = $options; return $this; @@ -880,7 +880,7 @@ public function setOptions(array $options) * @return bool */ public function getEnhanceWindowsCompatibility() - { +{ return $this->enhanceWindowsCompatibility; } @@ -890,8 +890,8 @@ public function getEnhanceWindowsCompatibility() * @return self */ public function setEnhanceWindowsCompatibility($enhance) - { - $this->enhanceWindowsCompatibility = (bool)$enhance; +{ + $this->enhanceWindowsCompatibility = (bool) $enhance; return $this; } @@ -901,7 +901,7 @@ public function setEnhanceWindowsCompatibility($enhance) * @return bool */ public function getEnhanceSigchildCompatibility() - { +{ return $this->enhanceSigchildCompatibility; } @@ -911,8 +911,8 @@ public function getEnhanceSigchildCompatibility() * @return self */ public function setEnhanceSigchildCompatibility($enhance) - { - $this->enhanceSigchildCompatibility = (bool)$enhance; +{ + $this->enhanceSigchildCompatibility = (bool) $enhance; return $this; } @@ -921,8 +921,8 @@ public function setEnhanceSigchildCompatibility($enhance) * 是否超时 */ public function checkTimeout() - { - if ($this->status !== self::STATUS_STARTED) { +{ + if (self::STATUS_STARTED !== $this->status) { return; } @@ -944,7 +944,7 @@ public function checkTimeout() * @return bool */ public static function isPtySupported() - { +{ static $result; if (null !== $result) { @@ -970,7 +970,7 @@ public static function isPtySupported() * @return array */ private function getDescriptors() - { +{ if ('\\' === DS) { $this->processPipes = WindowsPipes::create($this, $this->input); } else { @@ -994,7 +994,7 @@ private function getDescriptors() * @return callable */ protected function buildCallback($callback) - { +{ $out = self::OUT; $callback = function ($type, $data) use ($callback, $out) { if ($out == $type) { @@ -1016,7 +1016,7 @@ protected function buildCallback($callback) * @param bool $blocking */ protected function updateStatus($blocking) - { +{ if (self::STATUS_STARTED !== $this->status) { return; } @@ -1036,7 +1036,7 @@ protected function updateStatus($blocking) * @return bool */ protected function isSigchildEnabled() - { +{ if (null !== self::$sigchild) { return self::$sigchild; } @@ -1057,8 +1057,8 @@ protected function isSigchildEnabled() * @return float|null */ private function validateTimeout($timeout) - { - $timeout = (float)$timeout; +{ + $timeout = (float) $timeout; if (0.0 === $timeout) { $timeout = null; @@ -1075,15 +1075,15 @@ private function validateTimeout($timeout) * @param bool $close */ private function readPipes($blocking, $close) - { +{ $result = $this->processPipes->readAndWrite($blocking, $close); $callback = $this->callback; foreach ($result as $type => $data) { if (3 == $type) { - $this->fallbackExitcode = (int)$data; + $this->fallbackExitcode = (int) $data; } else { - $callback($type === self::STDOUT ? self::OUT : self::ERR, $data); + $callback(self::STDOUT === $type ? self::OUT : self::ERR, $data); } } } @@ -1092,7 +1092,7 @@ private function readPipes($blocking, $close) * 捕获退出码 */ private function captureExitCode() - { +{ if (isset($this->processInformation['exitcode']) && -1 != $this->processInformation['exitcode']) { $this->exitcode = $this->processInformation['exitcode']; } @@ -1103,7 +1103,7 @@ private function captureExitCode() * @return int 退出码 */ private function close() - { +{ $this->processPipes->close(); if (is_resource($this->process)) { $exitcode = proc_close($this->process); @@ -1117,7 +1117,7 @@ private function close() if (-1 === $this->exitcode && null !== $this->fallbackExitcode) { $this->exitcode = $this->fallbackExitcode; } elseif (-1 === $this->exitcode && $this->processInformation['signaled'] - && 0 < $this->processInformation['termsig'] + && 0 < $this->processInformation['termsig'] ) { $this->exitcode = 128 + $this->processInformation['termsig']; } @@ -1129,7 +1129,7 @@ private function close() * 重置数据 */ private function resetProcessData() - { +{ $this->starttime = null; $this->callback = null; $this->exitcode = null; @@ -1151,7 +1151,7 @@ private function resetProcessData() * @return bool */ private function doSignal($signal, $throwException) - { +{ if (!$this->isRunning()) { if ($throwException) { throw new \LogicException('Can not send signal on a non running process.'); @@ -1186,7 +1186,7 @@ private function doSignal($signal, $throwException) * @param string $functionName */ private function requireProcessIsStarted($functionName) - { +{ if (!$this->isStarted()) { throw new \LogicException(sprintf('Process must be started before calling %s.', $functionName)); } @@ -1197,9 +1197,9 @@ private function requireProcessIsStarted($functionName) * @param string $functionName */ private function requireProcessIsTerminated($functionName) - { +{ if (!$this->isTerminated()) { throw new \LogicException(sprintf('Process must be terminated before calling %s.', $functionName)); } } -} \ No newline at end of file +} diff --git a/thinkphp/library/think/Request.php b/thinkphp/library/think/Request.php index 086e366..d4f96a7 100644 --- a/thinkphp/library/think/Request.php +++ b/thinkphp/library/think/Request.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,11 +11,6 @@ namespace think; -use think\Config; -use think\Exception; -use think\File; -use think\Session; - class Request { /** @@ -25,7 +20,7 @@ class Request protected $method; /** - * @var string 域名 + * @var string 域名(含协议和端口) */ protected $domain; @@ -125,7 +120,7 @@ class Request protected $isCheckCache; /** - * 架构函数 + * 构造函数 * @access protected * @param array $options 参数 */ @@ -226,8 +221,9 @@ public static function create($uri, $method = 'GET', $params = [], $cookie = [], if (!isset($info['path'])) { $info['path'] = '/'; } - $options = []; - $queryString = ''; + $options = []; + $options[strtolower($method)] = $params; + $queryString = ''; if (isset($info['query'])) { parse_str(html_entity_decode($info['query']), $query); if (!empty($params)) { @@ -240,6 +236,11 @@ public static function create($uri, $method = 'GET', $params = [], $cookie = [], } elseif (!empty($params)) { $queryString = http_build_query($params, '', '&'); } + if ($queryString) { + parse_str($queryString, $get); + $options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get; + } + $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : ''); $server['QUERY_STRING'] = $queryString; $options['cookie'] = $cookie; @@ -632,7 +633,7 @@ public function param($name = '', $default = null, $filter = '') if (true === $name) { // 获取包含文件上传信息的数组 $file = $this->file(); - $data = array_merge($this->param, $file); + $data = is_array($file) ? array_merge($this->param, $file) : $this->param; return $this->input($data, '', $default, $filter); } return $this->input($this->param, $name, $default, $filter); @@ -687,8 +688,8 @@ public function post($name = '', $default = null, $filter = '') { if (empty($this->post)) { $content = $this->input; - if (empty($_POST) && strpos($content, '":')) { - $this->post = json_decode($content, true); + if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) { + $this->post = (array) json_decode($content, true); } else { $this->post = $_POST; } @@ -712,8 +713,8 @@ public function put($name = '', $default = null, $filter = '') { if (is_null($this->put)) { $content = $this->input; - if (strpos($content, '":')) { - $this->put = json_decode($content, true); + if (false !== strpos($this->contentType(), 'application/json')) { + $this->put = (array) json_decode($content, true); } else { parse_str($content, $this->put); } @@ -885,7 +886,7 @@ public function file($name = '') return $array[$name]; } } - return null; + return; } /** @@ -1347,6 +1348,25 @@ public function remotePort() return $this->server('REMOTE_PORT'); } + /** + * 当前请求 HTTP_CONTENT_TYPE + * @access public + * @return string + */ + public function contentType() + { + $contentType = $this->server('CONTENT_TYPE'); + if ($contentType) { + if (strpos($contentType, ';')) { + list($type) = explode(';', $contentType); + } else { + $type = $contentType; + } + return trim($type); + } + return ''; + } + /** * 获取当前请求的路由信息 * @access public @@ -1486,9 +1506,10 @@ public function token($name = '__token__', $type = 'md5') * @access public * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param mixed $expire 缓存有效期 + * @param array $except 缓存排除 * @return void */ - public function cache($key, $expire = null) + public function cache($key, $expire = null, $except = []) { if (false !== $key && $this->isGet() && !$this->isCheckCache) { // 标记请求缓存检查 @@ -1500,14 +1521,19 @@ public function cache($key, $expire = null) if ($key instanceof \Closure) { $key = call_user_func_array($key, [$this]); } elseif (true === $key) { + foreach ($except as $rule) { + if (0 === strpos($this->url(), $rule)) { + return; + } + } // 自动缓存功能 - $key = '__URL__'; + $key = md5($this->host()) . '__URL__'; } elseif (strpos($key, '|')) { list($key, $fun) = explode('|', $key); } // 特殊规则替换 if (false !== strpos($key, '__')) { - $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url())], $key); + $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__', ''], [$this->module, $this->controller, $this->action, md5($this->url())], $key); } if (false !== strpos($key, ':')) { diff --git a/thinkphp/library/think/Response.php b/thinkphp/library/think/Response.php index a7ebecf..d034578 100644 --- a/thinkphp/library/think/Response.php +++ b/thinkphp/library/think/Response.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,11 +11,6 @@ namespace think; -use think\Cache; -use think\Config; -use think\Debug; -use think\Env; -use think\Request; use think\response\Json as JsonResponse; use think\response\Jsonp as JsonpResponse; use think\response\Redirect as RedirectResponse; @@ -45,7 +40,7 @@ class Response protected $content = null; /** - * 架构函数 + * 构造函数 * @access public * @param mixed $data 输出数据 * @param int $code diff --git a/thinkphp/library/think/Route.php b/thinkphp/library/think/Route.php index d327207..506d74b 100644 --- a/thinkphp/library/think/Route.php +++ b/thinkphp/library/think/Route.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,14 +11,7 @@ namespace think; -use think\App; -use think\Config; use think\exception\HttpException; -use think\Hook; -use think\Loader; -use think\Log; -use think\Request; -use think\Response; class Route { @@ -239,6 +232,7 @@ protected static function registerRules($rules, $type = '*') public static function rule($rule, $route = '', $type = '*', $option = [], $pattern = []) { $group = self::getGroup('name'); + if (!is_null($group)) { // 路由分组 $option = array_merge(self::getGroup('option'), $option); @@ -296,12 +290,14 @@ protected static function setRule($rule, $route, $type = '*', $option = [], $pat } elseif ('$' == substr($rule, -1, 1)) { // 是否完整匹配 $option['complete_match'] = true; - $rule = substr($rule, 0, -1); } } elseif (empty($option['complete_match']) && '$' == substr($rule, -1, 1)) { // 是否完整匹配 $option['complete_match'] = true; - $rule = substr($rule, 0, -1); + } + + if ('$' == substr($rule, -1, 1)) { + $rule = substr($rule, 0, -1); } if ('/' != $rule || $group) { @@ -309,8 +305,12 @@ protected static function setRule($rule, $route, $type = '*', $option = [], $pat } $vars = self::parseVar($rule); if (isset($name)) { - $key = $group ? $group . ($rule ? '/' . $rule : '') : $rule; - self::name($name, [$key, $vars, self::$domain]); + $key = $group ? $group . ($rule ? '/' . $rule : '') : $rule; + $suffix = isset($option['ext']) ? $option['ext'] : null; + self::name($name, [$key, $vars, self::$domain, $suffix]); + } + if (isset($option['modular'])) { + $route = $option['modular'] . '/' . $route; } if ($group) { if ('*' != $type) { @@ -452,7 +452,8 @@ public static function group($name, $routes, $option = [], $pattern = []) $vars = self::parseVar($key); $item[] = ['rule' => $key, 'route' => $route, 'var' => $vars, 'option' => $options, 'pattern' => $patterns]; // 设置路由标识 - self::name($route, [$name . ($key ? '/' . $key : ''), $vars, self::$domain]); + $suffix = isset($options['ext']) ? $options['ext'] : null; + self::name($route, [$name . ($key ? '/' . $key : ''), $vars, self::$domain, $suffix]); } self::$rules['*'][$name] = ['rule' => $item, 'route' => '', 'var' => [], 'option' => $option, 'pattern' => $pattern]; } @@ -661,14 +662,14 @@ public static function setMethodPrefix($method, $prefix = '') /** * rest方法定义和修改 * @access public - * @param string $name 方法名称 - * @param array $resource 资源 + * @param string $name 方法名称 + * @param array|bool $resource 资源 * @return void */ public static function rest($name, $resource = []) { if (is_array($name)) { - self::$rest = array_merge(self::$rest, $name); + self::$rest = $resource ? $name : array_merge(self::$rest, $name); } else { self::$rest[$name] = $resource; } @@ -832,7 +833,7 @@ public static function check($request, $url, $depr = '/', $checkDomain = false) // 分隔符替换 确保路由定义使用统一的分隔符 $url = str_replace($depr, '|', $url); - if (strpos($url, '|') && isset(self::$rules['alias'][strstr($url, '|', true)])) { + if (isset(self::$rules['alias'][$url]) || isset(self::$rules['alias'][strstr($url, '|', true)])) { // 检测路由别名 $result = self::checkRouteAlias($request, $url, $depr); if (false !== $result) { @@ -1102,7 +1103,7 @@ public static function bindToController($url, $controller, $depr = '/') * 绑定到模块/控制器 * @access public * @param string $url URL地址 - * @param string $class 控制器类名(带命名空间) + * @param string $controller 控制器类名(带命名空间) * @param string $depr URL分隔符 * @return array */ @@ -1131,8 +1132,8 @@ private static function checkOption($option, $request) || (isset($option['ajax']) && !$option['ajax'] && $request->isAjax()) // 非Ajax检测 || (isset($option['pjax']) && $option['pjax'] && !$request->isPjax()) // Pjax检测 || (isset($option['pjax']) && !$option['pjax'] && $request->isPjax()) // 非Pjax检测 - || (isset($option['ext']) && false === stripos($option['ext'], $request->ext())) // 伪静态后缀检测 - || (isset($option['deny_ext']) && false !== stripos($option['deny_ext'], $request->ext())) + || (isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) // 伪静态后缀检测 + || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')) || (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测 || (isset($option['https']) && $option['https'] && !$request->isSsl()) // https检测 || (isset($option['https']) && !$option['https'] && $request->isSsl()) // https检测 @@ -1169,7 +1170,7 @@ private static function checkRule($rule, $route, $url, $pattern, $option, $depr) $len1 = substr_count($url, '|'); $len2 = substr_count($rule, '/'); // 多余参数是否合并 - $merge = !empty($option['merge_extra_vars']) ? true : false; + $merge = !empty($option['merge_extra_vars']); if ($merge && $len1 > $len2) { $url = str_replace('|', $depr, $url); $url = implode('|', explode($depr, $url, $len2 + 1)); @@ -1280,9 +1281,6 @@ private static function parseUrlPath($url) } elseif (strpos($url, '/')) { // [模块/控制器/操作] $path = explode('/', $url); - } elseif (false !== strpos($url, '=')) { - // 参数1=值1&参数2=值2... - parse_str($url, $var); } else { $path = [$url]; } @@ -1493,6 +1491,10 @@ private static function parseRule($rule, $route, $pathinfo, $option = [], $match $route = substr($route, 1); list($route, $var) = self::parseUrlPath($route); $result = ['type' => 'controller', 'controller' => implode('/', $route), 'var' => $var]; + $request->action(array_pop($route)); + $request->controller($route ? array_pop($route) : Config::get('default_controller')); + $request->module($route ? array_pop($route) : Config::get('default_module')); + App::$modulePath = APP_PATH . (Config::get('app_multi_module') ? $request->module() . DS : ''); } else { // 路由到模块/控制器/操作 $result = self::parseModule($route); diff --git a/thinkphp/library/think/Session.php b/thinkphp/library/think/Session.php index b9527a5..3572622 100644 --- a/thinkphp/library/think/Session.php +++ b/thinkphp/library/think/Session.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,7 +11,6 @@ namespace think; -use think\App; use think\exception\ClassNotFoundException; class Session @@ -78,7 +77,12 @@ public static function init(array $config = []) ini_set('session.gc_maxlifetime', $config['expire']); ini_set('session.cookie_lifetime', $config['expire']); } - + if (isset($config['secure'])) { + ini_set('session.cookie_secure', $config['secure']); + } + if (isset($config['httponly'])) { + ini_set('session.cookie_httponly', $config['httponly']); + } if (isset($config['use_cookies'])) { ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0); } @@ -193,7 +197,7 @@ public static function pull($name, $prefix = null) self::delete($name, $prefix); return $result; } else { - return null; + return; } } diff --git a/thinkphp/library/think/Template.php b/thinkphp/library/think/Template.php index 9f7793b..4e7153b 100644 --- a/thinkphp/library/think/Template.php +++ b/thinkphp/library/think/Template.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,7 +12,6 @@ namespace think; use think\exception\TemplateNotFoundException; -use think\Request; /** * ThinkPHP分离出来的模板引擎 @@ -58,7 +57,7 @@ class Template protected $storage; /** - * 架构函数 + * 构造函数 * @access public */ public function __construct(array $config = []) @@ -130,7 +129,7 @@ public function config($config) } elseif (isset($this->config[$config])) { return $this->config[$config]; } else { - return null; + return; } } @@ -669,7 +668,7 @@ private function getIncludeTagLib(&$content) $content = str_replace($matches[0], '', $content); return explode(',', $matches['name']); } - return null; + return; } /** diff --git a/thinkphp/library/think/Url.php b/thinkphp/library/think/Url.php index d873074..de3cd91 100644 --- a/thinkphp/library/think/Url.php +++ b/thinkphp/library/think/Url.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,11 +11,6 @@ namespace think; -use think\Config; -use think\Loader; -use think\Request; -use think\Route; - class Url { // 生成URL地址的root @@ -85,11 +80,15 @@ public static function build($url = '', $vars = '', $suffix = true, $domain = fa if (!empty($match[1])) { $domain = $match[1]; } + if (!is_null($match[2])) { + $suffix = $match[2]; + } } elseif (!empty($rule) && isset($name)) { throw new \InvalidArgumentException('route name not exists:' . $name); } else { // 检查别名路由 - $alias = Route::rules('alias'); + $alias = Route::rules('alias'); + $matchAlias = false; if ($alias) { // 别名路由解析 foreach ($alias as $key => $val) { @@ -97,11 +96,13 @@ public static function build($url = '', $vars = '', $suffix = true, $domain = fa $val = $val[0]; } if (0 === strpos($url, $val)) { - $url = $key . substr($url, strlen($val)); + $url = $key . substr($url, strlen($val)); + $matchAlias = true; break; } } - } else { + } + if (!$matchAlias) { // 路由标识不存在 直接解析 $url = self::parseUrl($url, $domain); } @@ -155,7 +156,8 @@ public static function build($url = '', $vars = '', $suffix = true, $domain = fa // 检测域名 $domain = self::parseDomain($url, $domain); // URL组装 - $url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/'); + $url = $domain . rtrim(self::$root ?: Request::instance()->root(), '/') . '/' . ltrim($url, '/'); + self::$bindCheck = false; return $url; } @@ -250,7 +252,7 @@ protected static function parseDomain(&$url, $domain) $domain .= $rootDomain; } break; - } else if (false !== strpos($key, '*')) { + } elseif (false !== strpos($key, '*')) { if (!empty($rootDomain)) { $domain .= $rootDomain; } @@ -289,18 +291,18 @@ protected static function parseSuffix($suffix) public static function getRuleUrl($rule, &$vars = []) { foreach ($rule as $item) { - list($url, $pattern, $domain) = $item; + list($url, $pattern, $domain, $suffix) = $item; if (empty($pattern)) { - return [$url, $domain]; + return [$url, $domain, $suffix]; } foreach ($pattern as $key => $val) { if (isset($vars[$key])) { $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url); unset($vars[$key]); - $result = [$url, $domain]; + $result = [$url, $domain, $suffix]; } elseif (2 == $val) { $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url); - $result = [$url, $domain]; + $result = [$url, $domain, $suffix]; } else { break; } diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index 28f7c35..718ed7a 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,10 +11,7 @@ namespace think; -use think\File; -use think\Lang; -use think\Request; -use think\Session; +use think\exception\ClassNotFoundException; class Validate { @@ -105,7 +102,7 @@ class Validate protected $batch = false; /** - * 架构函数 + * 构造函数 * @access public * @param array $rules 验证规则 * @param array $message 验证提示信息 @@ -438,7 +435,7 @@ protected function confirm($value, $rule, $data, $field = '') $rule = $field . '_confirm'; } } - return $this->getDataValue($data, $rule) == $value; + return $this->getDataValue($data, $rule) === $value; } /** @@ -571,7 +568,7 @@ protected function is($value, $rule, $data = []) break; case 'ip': // 是否为IP地址 - $result = $this->filter($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + $result = $this->filter($value, [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6]); break; case 'url': // 是否为一个URL地址 @@ -594,7 +591,7 @@ protected function is($value, $rule, $data = []) break; case 'boolean': // 是否为布尔值 - $result = in_array($value, [0, 1, true, false]); + $result = in_array($value, [true, false, 0, 1, '0', '1'], true); break; case 'array': // 是否为数组 @@ -659,7 +656,7 @@ protected function ip($value, $rule) if (!in_array($rule, ['ipv4', 'ipv6'])) { $rule = 'ipv4'; } - return $this->filter($value, FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4); + return $this->filter($value, [FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4]); } /** @@ -812,7 +809,16 @@ protected function unique($value, $rule, $data, $field) if (is_string($rule)) { $rule = explode(',', $rule); } - $db = Db::name($rule[0]); + if (false !== strpos($rule[0], '\\')) { + // 指定模型类 + $db = new $rule[0]; + } else { + try { + $db = Loader::model($rule[0]); + } catch (ClassNotFoundException $e) { + $db = Db::name($rule[0]); + } + } $key = isset($rule[1]) ? $rule[1] : $field; if (strpos($key, '^')) { @@ -866,6 +872,7 @@ protected function filter($value, $rule) list($rule, $param) = explode(',', $rule); } elseif (is_array($rule)) { $param = isset($rule[1]) ? $rule[1] : null; + $rule = $rule[0]; } else { $param = null; } @@ -1216,9 +1223,9 @@ protected function getRuleMsg($attribute, $title, $type, $rule) $msg = Lang::get(substr($msg, 2, -1)); } - if (is_string($msg) && is_string($rule) && false !== strpos($msg, ':')) { + if (is_string($msg) && is_scalar($rule) && false !== strpos($msg, ':')) { // 变量替换 - if (strpos($rule, ',')) { + if (is_string($rule) && strpos($rule, ',')) { $array = array_pad(explode(',', $rule), 3, ''); } else { $array = array_pad([], 3, ''); diff --git a/thinkphp/library/think/View.php b/thinkphp/library/think/View.php index b724ba9..96ae56c 100644 --- a/thinkphp/library/think/View.php +++ b/thinkphp/library/think/View.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -11,9 +11,6 @@ namespace think; -use think\Loader; -use think\Request; - class View { // 视图实例 @@ -28,7 +25,7 @@ class View protected $replace = []; /** - * 架构函数 + * 构造函数 * @access public * @param array $engine 模板引擎参数 * @param array $replace 字符串替换参数 diff --git a/thinkphp/library/think/cache/Driver.php b/thinkphp/library/think/cache/Driver.php index a2a5779..688507a 100644 --- a/thinkphp/library/think/cache/Driver.php +++ b/thinkphp/library/think/cache/Driver.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -105,7 +105,7 @@ public function pull($name) $this->rm($name); return $result; } else { - return null; + return; } } diff --git a/thinkphp/library/think/cache/driver/File.php b/thinkphp/library/think/cache/driver/File.php index 5c98d79..5aadb2b 100644 --- a/thinkphp/library/think/cache/driver/File.php +++ b/thinkphp/library/think/cache/driver/File.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -21,14 +21,14 @@ class File extends Driver { protected $options = [ 'expire' => 0, - 'cache_subdir' => false, + 'cache_subdir' => true, 'prefix' => '', 'path' => CACHE_PATH, 'data_compress' => false, ]; /** - * 架构函数 + * 构造函数 * @param array $options */ public function __construct($options = []) @@ -225,6 +225,7 @@ public function clear($tag = null) foreach ($files as $path) { if (is_dir($path)) { array_map('unlink', glob($path . '/*.php')); + rmdir($path); } else { unlink($path); } diff --git a/thinkphp/library/think/cache/driver/Lite.php b/thinkphp/library/think/cache/driver/Lite.php index b9d1009..eeb96a1 100644 --- a/thinkphp/library/think/cache/driver/Lite.php +++ b/thinkphp/library/think/cache/driver/Lite.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -26,7 +26,7 @@ class Lite extends Driver ]; /** - * 架构函数 + * 构造函数 * @access public * * @param array $options diff --git a/thinkphp/library/think/cache/driver/Memcache.php b/thinkphp/library/think/cache/driver/Memcache.php index bddf715..1fbd3b0 100644 --- a/thinkphp/library/think/cache/driver/Memcache.php +++ b/thinkphp/library/think/cache/driver/Memcache.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,7 +12,6 @@ namespace think\cache\driver; use think\cache\Driver; -use think\Exception; class Memcache extends Driver { @@ -26,7 +25,7 @@ class Memcache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public * @throws \BadFunctionCallException diff --git a/thinkphp/library/think/cache/driver/Memcached.php b/thinkphp/library/think/cache/driver/Memcached.php index 4c4505b..fa312e8 100644 --- a/thinkphp/library/think/cache/driver/Memcached.php +++ b/thinkphp/library/think/cache/driver/Memcached.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -27,7 +27,7 @@ class Memcached extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public */ diff --git a/thinkphp/library/think/cache/driver/Redis.php b/thinkphp/library/think/cache/driver/Redis.php index e7f7876..4f61878 100644 --- a/thinkphp/library/think/cache/driver/Redis.php +++ b/thinkphp/library/think/cache/driver/Redis.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -34,7 +34,7 @@ class Redis extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public */ diff --git a/thinkphp/library/think/cache/driver/Sqlite.php b/thinkphp/library/think/cache/driver/Sqlite.php index 6dbd41f..305fd9e 100644 --- a/thinkphp/library/think/cache/driver/Sqlite.php +++ b/thinkphp/library/think/cache/driver/Sqlite.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,7 +12,6 @@ namespace think\cache\driver; use think\cache\Driver; -use think\Exception; /** * Sqlite缓存驱动 @@ -29,7 +28,7 @@ class Sqlite extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @throws \BadFunctionCallException * @access public diff --git a/thinkphp/library/think/cache/driver/Wincache.php b/thinkphp/library/think/cache/driver/Wincache.php index a9cc7d2..3076fc1 100644 --- a/thinkphp/library/think/cache/driver/Wincache.php +++ b/thinkphp/library/think/cache/driver/Wincache.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,7 +12,6 @@ namespace think\cache\driver; use think\cache\Driver; -use think\Exception; /** * Wincache缓存驱动 @@ -26,9 +25,9 @@ class Wincache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 - * @throws Exception + * @throws \BadFunctionCallException * @access public */ public function __construct($options = []) diff --git a/thinkphp/library/think/cache/driver/Xcache.php b/thinkphp/library/think/cache/driver/Xcache.php index 6b18b3f..2a0e07a 100644 --- a/thinkphp/library/think/cache/driver/Xcache.php +++ b/thinkphp/library/think/cache/driver/Xcache.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- @@ -12,7 +12,6 @@ namespace think\cache\driver; use think\cache\Driver; -use think\Exception; /** * Xcache缓存驱动 @@ -26,7 +25,7 @@ class Xcache extends Driver ]; /** - * 架构函数 + * 构造函数 * @param array $options 缓存参数 * @access public * @throws \BadFunctionCallException diff --git a/thinkphp/library/think/config/driver/Ini.php b/thinkphp/library/think/config/driver/Ini.php index d8dc558..a223a57 100644 --- a/thinkphp/library/think/config/driver/Ini.php +++ b/thinkphp/library/think/config/driver/Ini.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/config/driver/Json.php b/thinkphp/library/think/config/driver/Json.php index ec2419f..557f75f 100644 --- a/thinkphp/library/think/config/driver/Json.php +++ b/thinkphp/library/think/config/driver/Json.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/config/driver/Xml.php b/thinkphp/library/think/config/driver/Xml.php index 5bc9301..b573a56 100644 --- a/thinkphp/library/think/config/driver/Xml.php +++ b/thinkphp/library/think/config/driver/Xml.php @@ -2,7 +2,7 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- diff --git a/thinkphp/library/think/console/Input.php b/thinkphp/library/think/console/Input.php index 269196e..2482dfd 100644 --- a/thinkphp/library/think/console/Input.php +++ b/thinkphp/library/think/console/Input.php @@ -252,7 +252,7 @@ public function getFirstArgument() return $token; } - return null; + return; } /** diff --git a/thinkphp/library/think/console/command/Help.php b/thinkphp/library/think/console/command/Help.php index eb0858a..bae2c65 100644 --- a/thinkphp/library/think/console/command/Help.php +++ b/thinkphp/library/think/console/command/Help.php @@ -16,7 +16,6 @@ use think\console\input\Argument as InputArgument; use think\console\input\Option as InputOption; use think\console\Output; -use think\console\helper\Descriptor as DescriptorHelper; class Help extends Command { @@ -67,4 +66,4 @@ protected function execute(Input $input, Output $output) $this->command = null; } -} \ No newline at end of file +} diff --git a/thinkphp/library/think/console/command/Lists.php b/thinkphp/library/think/console/command/Lists.php index ffbee07..084ddaa 100644 --- a/thinkphp/library/think/console/command/Lists.php +++ b/thinkphp/library/think/console/command/Lists.php @@ -71,4 +71,4 @@ private function createDefinition() new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list') ]); } -} \ No newline at end of file +} diff --git a/thinkphp/library/think/console/command/optimize/Autoload.php b/thinkphp/library/think/console/command/optimize/Autoload.php index 0481e17..7311aaf 100644 --- a/thinkphp/library/think/console/command/optimize/Autoload.php +++ b/thinkphp/library/think/console/command/optimize/Autoload.php @@ -278,4 +278,4 @@ protected function findClasses($path) return $classes; } -} \ No newline at end of file +} diff --git a/thinkphp/library/think/console/command/optimize/Schema.php b/thinkphp/library/think/console/command/optimize/Schema.php index a1f40cd..6ac38a3 100644 --- a/thinkphp/library/think/console/command/optimize/Schema.php +++ b/thinkphp/library/think/console/command/optimize/Schema.php @@ -50,7 +50,7 @@ protected function execute(Input $input, Output $output) } $output->writeln('