From 48f169b0aead42c719afc26af0e1b552680c3047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8E=8C?= Date: Thu, 27 Dec 2018 14:22:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B05132=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 10 +-- thinkphp/library/think/App.php | 6 +- thinkphp/library/think/Controller.php | 2 + thinkphp/library/think/Model.php | 3 +- thinkphp/library/think/Validate.php | 10 ++- thinkphp/library/think/cache/Driver.php | 18 +++-- thinkphp/library/think/cache/driver/File.php | 2 +- thinkphp/library/think/cache/driver/Lite.php | 2 +- .../library/think/cache/driver/Memcache.php | 5 +- .../library/think/cache/driver/Memcached.php | 65 ++++++++++++++++++- thinkphp/library/think/cache/driver/Redis.php | 64 ++++++++++++++++-- .../library/think/cache/driver/Sqlite.php | 2 +- .../library/think/cache/driver/Wincache.php | 15 +++-- .../library/think/cache/driver/Xcache.php | 4 +- thinkphp/library/think/db/Query.php | 3 - .../library/think/model/concern/Attribute.php | 6 +- .../think/model/relation/BelongsToMany.php | 4 +- .../library/think/model/relation/HasMany.php | 4 +- .../think/model/relation/MorphMany.php | 2 +- .../library/think/model/relation/MorphOne.php | 4 +- thinkphp/library/think/route/Rule.php | 16 +++-- .../library/think/route/dispatch/Module.php | 8 --- thinkphp/library/think/route/dispatch/Url.php | 4 ++ vendor/composer/installed.json | 12 ++-- 24 files changed, 206 insertions(+), 65 deletions(-) diff --git a/composer.lock b/composer.lock index c077bb83..f7ed9c6b 100644 --- a/composer.lock +++ b/composer.lock @@ -1053,16 +1053,16 @@ }, { "name": "topthink/framework", - "version": "v5.1.31", + "version": "v5.1.32", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "93339b1a4df5a73e0143db0847a4c5e0b2e46fb0" + "reference": "88a2ab625b35e047896718db320e08375cf021da" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/93339b1a4df5a73e0143db0847a4c5e0b2e46fb0", - "reference": "93339b1a4df5a73e0143db0847a4c5e0b2e46fb0", + "url": "https://api.github.com/repos/top-think/framework/zipball/88a2ab625b35e047896718db320e08375cf021da", + "reference": "88a2ab625b35e047896718db320e08375cf021da", "shasum": "", "mirrors": [ { @@ -1106,7 +1106,7 @@ "orm", "thinkphp" ], - "time": "2018-12-09T12:41:21+00:00" + "time": "2018-12-23T13:42:11+00:00" }, { "name": "topthink/think-captcha", diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php index cfa2601e..ac8b0e91 100644 --- a/thinkphp/library/think/App.php +++ b/thinkphp/library/think/App.php @@ -20,7 +20,7 @@ use think\route\Dispatch; */ class App extends Container { - const VERSION = '5.1.31 LTS'; + const VERSION = '5.1.32 LTS'; /** * 当前模块路径 @@ -722,9 +722,9 @@ class App extends Container list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix); if (class_exists($class)) { - return $this->__get($class); + return $this->make($class, true); } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) { - return $this->__get($emptyClass); + return $this->make($emptyClass, true); } throw new ClassNotFoundException('class not exists:' . $class, $class); diff --git a/thinkphp/library/think/Controller.php b/thinkphp/library/think/Controller.php index a57da9e3..d16a1ed5 100644 --- a/thinkphp/library/think/Controller.php +++ b/thinkphp/library/think/Controller.php @@ -67,6 +67,8 @@ class Controller // 控制器初始化 $this->initialize(); + $this->registerMiddleware(); + // 前置操作方法 即将废弃 foreach ((array) $this->beforeActionList as $method => $options) { is_numeric($method) ? diff --git a/thinkphp/library/think/Model.php b/thinkphp/library/think/Model.php index e0dcecf6..9896f9ff 100644 --- a/thinkphp/library/think/Model.php +++ b/thinkphp/library/think/Model.php @@ -392,11 +392,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 设置数据是否存在 * @access public * @param bool $exists - * @return void + * @return $this */ public function exists($exists) { $this->exists = $exists; + return $this; } /** diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index 4672a5cb..54c3a737 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -516,7 +516,7 @@ class Validate $rules = array_merge($rules, $this->append[$field]); } - $i = 0; + $i = 0; $result = true; foreach ($rules as $key => $rule) { @@ -1002,10 +1002,14 @@ class Validate // 支持多个字段验证 $fields = explode('^', $key); foreach ($fields as $key) { - $map[] = [$key, '=', $data[$key]]; + if (isset($data[$key])) { + $map[] = [$key, '=', $data[$key]]; + } } - } else { + } elseif (isset($data[$field])) { $map[] = [$key, '=', $data[$field]]; + } else { + $map = []; } $pk = !empty($rule[3]) ? $rule[3] : $db->getPk(); diff --git a/thinkphp/library/think/cache/Driver.php b/thinkphp/library/think/cache/Driver.php index f0ec7baf..f4c5dcbf 100644 --- a/thinkphp/library/think/cache/Driver.php +++ b/thinkphp/library/think/cache/Driver.php @@ -219,7 +219,7 @@ abstract class Driver } elseif (is_null($keys)) { $this->tag = $name; } else { - $key = 'tag_' . md5($name); + $key = $this->getTagkey($name); if (is_string($keys)) { $keys = explode(',', $keys); @@ -248,14 +248,19 @@ abstract class Driver protected function setTagItem($name) { if ($this->tag) { - $key = 'tag_' . md5($this->tag); + $key = $this->getTagkey($this->tag); $prev = $this->tag; $this->tag = null; if ($this->has($key)) { $value = explode(',', $this->get($key)); $value[] = $name; - $value = implode(',', array_unique($value)); + + if (count($value) > 1000) { + array_shift($value); + } + + $value = implode(',', array_unique($value)); } else { $value = $name; } @@ -273,7 +278,7 @@ abstract class Driver */ protected function getTagItem($tag) { - $key = 'tag_' . md5($tag); + $key = $this->getTagkey($tag); $value = $this->get($key); if ($value) { @@ -283,6 +288,11 @@ abstract class Driver } } + protected function getTagKey($tag) + { + return 'tag_' . md5($tag); + } + /** * 序列化数据 * @access protected diff --git a/thinkphp/library/think/cache/driver/File.php b/thinkphp/library/think/cache/driver/File.php index 7c5661e3..93d321f2 100644 --- a/thinkphp/library/think/cache/driver/File.php +++ b/thinkphp/library/think/cache/driver/File.php @@ -266,7 +266,7 @@ class File extends Driver foreach ($keys as $key) { $this->unlink($key); } - $this->rm('tag_' . md5($tag)); + $this->rm($this->getTagKey($tag)); return true; } diff --git a/thinkphp/library/think/cache/driver/Lite.php b/thinkphp/library/think/cache/driver/Lite.php index 544663c0..0cfe3907 100644 --- a/thinkphp/library/think/cache/driver/Lite.php +++ b/thinkphp/library/think/cache/driver/Lite.php @@ -198,7 +198,7 @@ class Lite extends Driver unlink($key); } - $this->rm('tag_' . md5($tag)); + $this->rm($this->getTagKey($tag)); return true; } diff --git a/thinkphp/library/think/cache/driver/Memcache.php b/thinkphp/library/think/cache/driver/Memcache.php index 162ca520..1c535597 100644 --- a/thinkphp/library/think/cache/driver/Memcache.php +++ b/thinkphp/library/think/cache/driver/Memcache.php @@ -188,11 +188,13 @@ class Memcache extends Driver if ($tag) { // 指定标签清除 $keys = $this->getTagItem($tag); + foreach ($keys as $key) { $this->handler->delete($key); } - $this->rm('tag_' . md5($tag)); + $tagName = $this->getTagKey($tag); + $this->rm($tagName); return true; } @@ -200,4 +202,5 @@ class Memcache extends Driver return $this->handler->flush(); } + } diff --git a/thinkphp/library/think/cache/driver/Memcached.php b/thinkphp/library/think/cache/driver/Memcached.php index d04fac08..6af60d19 100644 --- a/thinkphp/library/think/cache/driver/Memcached.php +++ b/thinkphp/library/think/cache/driver/Memcached.php @@ -204,7 +204,7 @@ class Memcached extends Driver $keys = $this->getTagItem($tag); $this->handler->deleteMulti($keys); - $this->rm('tag_' . md5($tag)); + $this->rm($this->getTagKey($tag)); return true; } @@ -213,4 +213,67 @@ class Memcached extends Driver return $this->handler->flush(); } + + /** + * 缓存标签 + * @access public + * @param string $name 标签名 + * @param string|array $keys 缓存标识 + * @param bool $overlay 是否覆盖 + * @return $this + */ + public function tag($name, $keys = null, $overlay = false) + { + if (is_null($keys)) { + $this->tag = $name; + } else { + $tagName = $this->getTagKey($name); + if ($overlay) { + $this->handler->delete($tagName); + } + + if (!$this->handler->has($tagName)) { + $this->handler->set($tagName, ''); + } + + foreach ($keys as $key) { + $this->handler->append($tagName, ',' . $key); + } + } + + return $this; + } + + /** + * 更新标签 + * @access protected + * @param string $name 缓存标识 + * @return void + */ + protected function setTagItem($name) + { + if ($this->tag) { + $tagName = $this->getTagKey($this->tag); + + if ($this->handler->has($tagName)) { + $this->handler->append($tagName, ',' . $name); + } else { + $this->handler->set($tagName, $name); + } + + $this->tag = null; + } + } + + /** + * 获取标签包含的缓存标识 + * @access public + * @param string $tag 缓存标签 + * @return array + */ + public function getTagItem($tag) + { + $tagName = $this->getTagKey($tag); + return explode(',', trim($this->handler->get($tagName), ',')); + } } diff --git a/thinkphp/library/think/cache/driver/Redis.php b/thinkphp/library/think/cache/driver/Redis.php index b924ec3d..813746e7 100644 --- a/thinkphp/library/think/cache/driver/Redis.php +++ b/thinkphp/library/think/cache/driver/Redis.php @@ -70,6 +70,10 @@ class Redis extends Driver } } + if ('' == $this->options['password']) { + unset($this->options['password']); + } + $this->handler = new \Predis\Client($this->options, $params); $this->options['prefix'] = ''; @@ -187,7 +191,7 @@ class Redis extends Driver { $this->writeTimes++; - return $this->handler->delete($this->getCacheKey($name)); + return $this->handler->del($this->getCacheKey($name)); } /** @@ -202,11 +206,10 @@ class Redis extends Driver // 指定标签清除 $keys = $this->getTagItem($tag); - foreach ($keys as $key) { - $this->handler->delete($key); - } + $this->handler->del($keys); - $this->rm('tag_' . md5($tag)); + $tagName = $this->getTagKey($tag); + $this->handler->del($tagName); return true; } @@ -215,4 +218,55 @@ class Redis extends Driver return $this->handler->flushDB(); } + /** + * 缓存标签 + * @access public + * @param string $name 标签名 + * @param string|array $keys 缓存标识 + * @param bool $overlay 是否覆盖 + * @return $this + */ + public function tag($name, $keys = null, $overlay = false) + { + if (is_null($keys)) { + $this->tag = $name; + } else { + $tagName = $this->getTagKey($name); + if ($overlay) { + $this->handler->del($tagName); + } + + foreach ($keys as $key) { + $this->handler->sAdd($tagName, $key); + } + } + + return $this; + } + + /** + * 更新标签 + * @access protected + * @param string $name 缓存标识 + * @return void + */ + protected function setTagItem($name) + { + if ($this->tag) { + $tagName = $this->getTagKey($this->tag); + $this->handler->sAdd($tagName, $name); + } + } + + /** + * 获取标签包含的缓存标识 + * @access protected + * @param string $tag 缓存标签 + * @return array + */ + protected function getTagItem($tag) + { + $tagName = $this->getTagKey($tag); + return $this->handler->sMembers($tagName); + } } diff --git a/thinkphp/library/think/cache/driver/Sqlite.php b/thinkphp/library/think/cache/driver/Sqlite.php index 7e78ec12..f57361e3 100644 --- a/thinkphp/library/think/cache/driver/Sqlite.php +++ b/thinkphp/library/think/cache/driver/Sqlite.php @@ -216,7 +216,7 @@ class Sqlite extends Driver public function clear($tag = null) { if ($tag) { - $name = sqlite_escape_string($tag); + $name = sqlite_escape_string($this->getTagKey($tag)); $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE tag=\'' . $name . '\''; sqlite_query($this->handler, $sql); return true; diff --git a/thinkphp/library/think/cache/driver/Wincache.php b/thinkphp/library/think/cache/driver/Wincache.php index 10966e78..ef157841 100644 --- a/thinkphp/library/think/cache/driver/Wincache.php +++ b/thinkphp/library/think/cache/driver/Wincache.php @@ -160,15 +160,16 @@ class Wincache extends Driver { if ($tag) { $keys = $this->getTagItem($tag); - foreach ($keys as $key) { - wincache_ucache_delete($key); - } - $this->rm('tag_' . md5($tag)); + + wincache_ucache_delete($keys); + + $tagName = $this->getTagkey($tag); + $this->rm($tagName); return true; - } else { - $this->writeTimes++; - return wincache_ucache_clear(); } + + $this->writeTimes++; + return wincache_ucache_clear(); } } diff --git a/thinkphp/library/think/cache/driver/Xcache.php b/thinkphp/library/think/cache/driver/Xcache.php index 6d1bf3f6..4e698597 100644 --- a/thinkphp/library/think/cache/driver/Xcache.php +++ b/thinkphp/library/think/cache/driver/Xcache.php @@ -159,10 +159,12 @@ class Xcache extends Driver if ($tag) { // 指定标签清除 $keys = $this->getTagItem($tag); + foreach ($keys as $key) { xcache_unset($key); } - $this->rm('tag_' . md5($tag)); + + $this->rm($this->getTagKey($tag)); return true; } diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index 00483328..2704c8df 100644 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -628,9 +628,6 @@ class Query $result = (float) $result; } - // 查询完成后清空聚合字段信息 - $this->removeOption('field'); - return $result; } diff --git a/thinkphp/library/think/model/concern/Attribute.php b/thinkphp/library/think/model/concern/Attribute.php index 75b02ab5..b7a80fd3 100644 --- a/thinkphp/library/think/model/concern/Attribute.php +++ b/thinkphp/library/think/model/concern/Attribute.php @@ -371,7 +371,8 @@ trait Attribute case 'datetime': case 'date': $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime($format . '.u'); + $format .= strpos($format, 'u') || false !== strpos($format, '\\') ? '' : '.u'; + $value = $this->formatDateTime($format); break; case 'timestamp': case 'integer': @@ -384,7 +385,8 @@ trait Attribute 'date', 'timestamp', ])) { - $value = $this->formatDateTime($this->dateFormat . '.u'); + $format = strpos($this->dateFormat, 'u') || false !== strpos($this->dateFormat, '\\') ? '' : '.u'; + $value = $this->formatDateTime($this->dateFormat . $format); } else { $value = time(); } diff --git a/thinkphp/library/think/model/relation/BelongsToMany.php b/thinkphp/library/think/model/relation/BelongsToMany.php index b7cdebe1..747482b6 100644 --- a/thinkphp/library/think/model/relation/BelongsToMany.php +++ b/thinkphp/library/think/model/relation/BelongsToMany.php @@ -559,7 +559,9 @@ class BelongsToMany extends Relation foreach ($ids as $id) { $pivot[$this->foreignKey] = $id; - $this->pivot->replace()->save($pivot); + $this->pivot->replace() + ->exists(false) + ->save($pivot); $result[] = $this->newPivot($pivot, true); } diff --git a/thinkphp/library/think/model/relation/HasMany.php b/thinkphp/library/think/model/relation/HasMany.php index 72d83144..dbb8fa08 100644 --- a/thinkphp/library/think/model/relation/HasMany.php +++ b/thinkphp/library/think/model/relation/HasMany.php @@ -241,9 +241,9 @@ class HasMany extends Relation */ public function save($data, $replace = true) { - $model = $this->make($data); + $model = $this->make(); - return $model->replace($replace)->save() ? $model : false; + return $model->replace($replace)->save($data) ? $model : false; } /** diff --git a/thinkphp/library/think/model/relation/MorphMany.php b/thinkphp/library/think/model/relation/MorphMany.php index 1a7f15eb..a1f54889 100644 --- a/thinkphp/library/think/model/relation/MorphMany.php +++ b/thinkphp/library/think/model/relation/MorphMany.php @@ -277,7 +277,7 @@ class MorphMany extends Relation */ public function save($data) { - $model = $this->make($data); + $model = $this->make(); return $model->save($data) ? $model : false; } diff --git a/thinkphp/library/think/model/relation/MorphOne.php b/thinkphp/library/think/model/relation/MorphOne.php index 716539f4..775b2dfd 100644 --- a/thinkphp/library/think/model/relation/MorphOne.php +++ b/thinkphp/library/think/model/relation/MorphOne.php @@ -211,8 +211,8 @@ class MorphOne extends Relation */ public function save($data) { - $model = $this->make($data); - return $model->save() ? $model : false; + $model = $this->make(); + return $model->save($data) ? $model : false; } /** diff --git a/thinkphp/library/think/route/Rule.php b/thinkphp/library/think/route/Rule.php index 35730fee..fdfc29d1 100644 --- a/thinkphp/library/think/route/Rule.php +++ b/thinkphp/library/think/route/Rule.php @@ -722,13 +722,17 @@ abstract class Rule // 替换路由地址中的变量 if (is_string($route) && !empty($matches)) { - foreach ($matches as $key => $val) { - if (false !== strpos($route, '<' . $key . '>')) { - $route = str_replace('<' . $key . '>', $val, $route); - } elseif (false !== strpos($route, ':' . $key)) { - $route = str_replace(':' . $key, $val, $route); - } + $search = $replace = []; + + foreach ($matches as $key => $value) { + $search[] = '<' . $key . '>'; + $replace[] = $value; + + $search[] = ':' . $key; + $replace[] = $value; } + + $route = str_replace($search, $replace, $route); } // 解析额外参数 diff --git a/thinkphp/library/think/route/dispatch/Module.php b/thinkphp/library/think/route/dispatch/Module.php index dc1974ce..e8842cd3 100644 --- a/thinkphp/library/think/route/dispatch/Module.php +++ b/thinkphp/library/think/route/dispatch/Module.php @@ -69,10 +69,6 @@ class Module extends Dispatch // 获取控制器名 $controller = strip_tags($result[1] ?: $this->rule->getConfig('default_controller')); - if (!preg_match('/^[A-Za-z](\w|\.)*$/', $controller)) { - throw new HttpException(404, 'controller not exists:' . $controller); - } - $this->controller = $convert ? strtolower($controller) : $controller; // 获取操作名 @@ -97,10 +93,6 @@ class Module extends Dispatch $this->rule->getConfig('url_controller_layer'), $this->rule->getConfig('controller_suffix'), $this->rule->getConfig('empty_controller')); - - if ($instance instanceof Controller) { - $instance->registerMiddleware(); - } } catch (ClassNotFoundException $e) { throw new HttpException(404, 'controller not exists:' . $e->getClass()); } diff --git a/thinkphp/library/think/route/dispatch/Url.php b/thinkphp/library/think/route/dispatch/Url.php index 95ee9e53..00dc8cca 100644 --- a/thinkphp/library/think/route/dispatch/Url.php +++ b/thinkphp/library/think/route/dispatch/Url.php @@ -60,6 +60,10 @@ class Url extends Dispatch $controller = !empty($path) ? array_shift($path) : null; } + if ($controller && !preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) { + throw new HttpException(404, 'controller not exists:' . $controller); + } + // 解析操作 $action = !empty($path) ? array_shift($path) : null; diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index d4a5362c..fbc6b199 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -1078,17 +1078,17 @@ }, { "name": "topthink/framework", - "version": "v5.1.31", - "version_normalized": "5.1.31.0", + "version": "v5.1.32", + "version_normalized": "5.1.32.0", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "93339b1a4df5a73e0143db0847a4c5e0b2e46fb0" + "reference": "88a2ab625b35e047896718db320e08375cf021da" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/93339b1a4df5a73e0143db0847a4c5e0b2e46fb0", - "reference": "93339b1a4df5a73e0143db0847a4c5e0b2e46fb0", + "url": "https://api.github.com/repos/top-think/framework/zipball/88a2ab625b35e047896718db320e08375cf021da", + "reference": "88a2ab625b35e047896718db320e08375cf021da", "shasum": "", "mirrors": [ { @@ -1110,7 +1110,7 @@ "sebastian/phpcpd": "2.*", "squizlabs/php_codesniffer": "2.*" }, - "time": "2018-12-09T12:41:21+00:00", + "time": "2018-12-23T13:42:11+00:00", "type": "think-framework", "installation-source": "dist", "notification-url": "https://packagist.org/downloads/",