From 388d49f1c27a43cc5615cccaa67d806978f15b9f Mon Sep 17 00:00:00 2001 From: ModStart Date: Sun, 12 Dec 2021 16:19:04 +0800 Subject: [PATCH] develop --- module/Vendor/Docs/release/2.1.0.md | 1 + .../IDManager/AbstractDbCacheIDManager.php | 73 +++++++++++++++++++ .../Provider/IDManager/AbstractIDManager.php | 23 ++++++ .../Vendor/Provider/IDManager/IDManager.php | 41 +++++++++++ .../modstart/src/Core/Dao/ModelUtil.php | 8 +- .../modstart/src/Core/Util/ReUtil.php | 2 +- .../modstart/views/core/grid/index.blade.php | 2 +- 7 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 module/Vendor/Provider/IDManager/AbstractDbCacheIDManager.php create mode 100644 module/Vendor/Provider/IDManager/AbstractIDManager.php create mode 100644 module/Vendor/Provider/IDManager/IDManager.php diff --git a/module/Vendor/Docs/release/2.1.0.md b/module/Vendor/Docs/release/2.1.0.md index 74b8ce0d..a8de813d 100644 --- a/module/Vendor/Docs/release/2.1.0.md +++ b/module/Vendor/Docs/release/2.1.0.md @@ -1,4 +1,5 @@ +- 新增:IDManager增强随机ID管理 - 优化:安装引导程序检测目录是否真实可写 diff --git a/module/Vendor/Provider/IDManager/AbstractDbCacheIDManager.php b/module/Vendor/Provider/IDManager/AbstractDbCacheIDManager.php new file mode 100644 index 00000000..fe284517 --- /dev/null +++ b/module/Vendor/Provider/IDManager/AbstractDbCacheIDManager.php @@ -0,0 +1,73 @@ +name() . '_All'); + } + + public function all() + { + return Cache::rememberForever($this->name() . '_All', function () { + return $this->dbCacheAll(); + }); + } + + public function add($ids) + { + $this->clearCache(); + } + + public function remove($ids) + { + $this->clearCache(); + } + + public function total() + { + return count($this->all()); + } + + private function idsPaginate(&$ids, $page, $pageSize) + { + $page = max($page, 1); + $offset = max($page - 1, 0) * $pageSize; + if (!isset($ids[$offset])) { + return []; + } + $results = []; + for ($i = $offset; $i < $offset + $pageSize; $i++) { + if (!isset($ids[$i])) { + break; + } + $results[] = $ids[$i]; + } + return $results; + } + + public function paginate($page, $pageSize) + { + $ids = $this->all(); + return $this->idsPaginate($ids, $page, $pageSize); + } + + public function paginateRandom($page, $pageSize, $cacheKey = 'all', $cacheMinutes = 60) + { + $ids = Cache::remember($this->name() . '_Random_' . $cacheKey, $cacheMinutes, function () { + $ids = $this->all(); + shuffle($ids); + return $ids; + }); + return $this->idsPaginate($ids, $page, $pageSize); + } + +} \ No newline at end of file diff --git a/module/Vendor/Provider/IDManager/AbstractIDManager.php b/module/Vendor/Provider/IDManager/AbstractIDManager.php new file mode 100644 index 00000000..5b04c015 --- /dev/null +++ b/module/Vendor/Provider/IDManager/AbstractIDManager.php @@ -0,0 +1,23 @@ + $v) { + if ($v instanceof \Closure) { + self::$instances[$k] = call_user_func($v); + } else if (is_string($v)) { + self::$instances[$k] = app($v); + } + } + return self::$instances; + } + + + public static function get($name) + { + $name = modstart_config($name, $name); + foreach (self::all() as $manager) { + if ($manager->name() == $name) { + return $manager; + } + } + return null; + } +} \ No newline at end of file diff --git a/vendor/modstart/modstart/src/Core/Dao/ModelUtil.php b/vendor/modstart/modstart/src/Core/Dao/ModelUtil.php index 5b758708..79eae89b 100644 --- a/vendor/modstart/modstart/src/Core/Dao/ModelUtil.php +++ b/vendor/modstart/modstart/src/Core/Dao/ModelUtil.php @@ -257,7 +257,7 @@ class ModelUtil return ArrayUtil::fetchSpecifiedKeyToArray($records, $idKey); } - public static function values($model, $field, $where = []) + public static function values($model, $field, $where = [], $order = null) { $flat = false; if (!is_array($field)) { @@ -266,7 +266,11 @@ class ModelUtil } else { $fields = $field; } - $ms = self::model($model)->where($where)->get($fields)->toArray(); + $ms = self::model($model)->where($where); + if (!empty($order)) { + $ms = $ms->orderBy($order[0], $order[1]); + } + $ms = $ms->get($fields)->toArray(); if ($flat) { return array_map(function ($item) use ($field) { return $item[$field]; diff --git a/vendor/modstart/modstart/src/Core/Util/ReUtil.php b/vendor/modstart/modstart/src/Core/Util/ReUtil.php index 4ea1885f..feecd56d 100644 --- a/vendor/modstart/modstart/src/Core/Util/ReUtil.php +++ b/vendor/modstart/modstart/src/Core/Util/ReUtil.php @@ -38,7 +38,7 @@ class ReUtil { $wild = str_replace('*', '__x__star__', $wild); $regex = '/^' . preg_quote($wild, '/') . '$/'; - $regex = str_replace('__x__star__', '[a-zA-Z0-9_]+', $regex); + $regex = str_replace('__x__star__', '.*', $regex); // echo "isWildMatch ==> $regex <-> $text\n"; return preg_match($regex, $text); } diff --git a/vendor/modstart/modstart/views/core/grid/index.blade.php b/vendor/modstart/modstart/views/core/grid/index.blade.php index 4f168641..185b702b 100644 --- a/vendor/modstart/modstart/views/core/grid/index.blade.php +++ b/vendor/modstart/modstart/views/core/grid/index.blade.php @@ -147,7 +147,7 @@ curr: data.page, count: data.total, limit: data.pageSize, - limits: [10,20,50,100,500,1000], + limits: [10,20,50,100], layout: [ 'limit', 'prev', 'page', 'next','count',], jump: function (obj, first) { if (!first) { -- GitLab