Преглед изворни кода

refactor(core): 移除已弃用的助手类并更新依赖版本

- 删除已弃用的 Helper 类及其所有静态方法
- 更新 PHP 最低版本要求从 8.3 到 8.4
- 升级 topthink/framework 从 ^8.1 到 ^8.1.3
- 升级 topthink/think-orm 从 ^4.0 到 ^4.0.50
- 升级 opis/closure 从 ^4.3.1 到 ^4.4.0
- 移除不再需要的 polyfills/array-first-array-last 依赖
- 移除已弃用的 topthink/think-helper 依赖
- 更新 README 文档,详细说明核心组件使用方法
- 添加扩展开发完整示例代码和说明
- 补充属性注解、实体基类和请求类使用文档
- 完善辅助函数列表及调用方式说明
runphp пре 3 месеци
родитељ
комит
792c62bf32
3 измењених фајлова са 197 додато и 129 уклоњено
  1. 193 1
      README.md
  2. 4 6
      composer.json
  3. 0 122
      src/Helper.php

+ 193 - 1
README.md

@@ -1,9 +1,201 @@
 # SixShop Core扩展
 
-这个扩展为后端应用提供了核心功能
+这个扩展为后端应用提供了核心功能,是构建SixShop电商系统扩展的基础组件。
+
+## 功能特性
+
+- 扩展生命周期管理(安装、卸载、启动)
+- 统一的请求处理类
+- 常用辅助函数
+- 属性注解支持(定时任务、钩子)
+- 实体基类
+- 核心服务提供者
 
 ## 安装
 
 ```shell
 composer require six-shop/core
+```
+
+## 核心组件
+
+### 1. 扩展抽象类 (ExtensionAbstract)
+
+所有SixShop扩展都需要继承 `SixShop\Core\ExtensionAbstract` 类并实现其抽象方法:
+
+```php
+use SixShop\Core\ExtensionAbstract;
+
+class MyExtension extends ExtensionAbstract
+{
+    protected function getBaseDir(): string
+    {
+        return __DIR__;
+    }
+}
+```
+
+扩展类需要实现的方法:
+- `getBaseDir()`: 返回扩展的根目录路径
+- `install()`: 扩展安装时执行的逻辑
+- `uninstall()`: 扩展卸载时执行的逻辑
+- `getInfo()`: 获取扩展基本信息
+- `getConfig()`: 获取扩展配置
+- `getCommands()`: 获取扩展命令
+- `getHooks()`: 获取扩展钩子
+- `getRoutes()`: 获取扩展路由
+- `getCronJobs()`: 获取扩展定时任务
+- `boot()`: 扩展启动时执行的逻辑
+
+### 2. 请求类 (Request)
+
+扩展了ThinkPHP的请求类,提供了额外的属性和方法:
+
+```php
+use SixShop\Core\Request;
+
+public function myAction(Request $request)
+{
+    // 获取管理员ID
+    $adminID = $request->adminID;
+    
+    // 获取用户ID
+    $userID = $request->userID;
+    
+    // 获取分页参数
+    [$page, $limit] = $request->pageAndLimit();
+}
+```
+
+### 3. 辅助函数
+
+提供了一系列常用的辅助函数:
+
+#### 响应函数
+- `success_response()`: 返回成功响应
+- `error_response()`: 返回错误响应
+- `page_response()`: 返回分页响应
+
+#### 其他函数
+- `throw_logic_exception()`: 抛出逻辑异常
+- `build_tree_options()`: 构建树形结构选项
+- `secret_password()`: 生成随机密码
+- `extension_path()`: 获取扩展路径
+- `extension_name_list()`: 获取扩展名称列表
+- `extension_composer_info()`: 获取扩展Composer信息
+
+### 4. 属性注解
+
+#### 定时任务注解 (Cron)
+用于标记定时任务类或方法:
+
+```php
+use SixShop\Core\Attribute\Cron;
+
+#[Cron('0 */5 * * * *', 'my_task')]
+class MyCronTask
+{
+    public function execute()
+    {
+        // 定时任务逻辑
+    }
+}
+```
+
+#### 钩子注解 (Hook)
+用于标记钩子处理方法:
+
+```php
+use SixShop\Core\Attribute\Hook;
+
+#[Hook(MyEvent::class)]
+public function handleMyEvent(MyEvent $event)
+{
+    // 钩子处理逻辑
+}
+```
+
+### 5. 实体基类 (BaseEntity)
+
+所有实体类都应该继承 `SixShop\Core\Entity\BaseEntity`:
+
+```php
+use SixShop\Core\Entity\BaseEntity;
+
+/**
+ * @mixin \app\Model\MyModel
+ */
+class MyEntity extends BaseEntity
+{
+    // 实体逻辑
+}
+```
+
+## 使用示例
+
+### 创建扩展
+
+```php
+<?php
+declare(strict_types=1);
+
+namespace MyVendor\MyExtension;
+
+use SixShop\Core\ExtensionAbstract;
+
+class Extension extends ExtensionAbstract
+{
+    protected function getBaseDir(): string
+    {
+        return dirname(__DIR__);
+    }
+    
+    public function boot(): void
+    {
+        // 扩展启动逻辑
+    }
+}
+```
+
+### 控制器中使用Request
+
+```php
+<?php
+declare(strict_types=1);
+
+namespace MyVendor\MyExtension\Controller\Api;
+
+use SixShop\Core\Request;
+use function SixShop\Core\success_response;
+use function SixShop\Core\page_response;
+
+class MyController
+{
+    public function index(Request $request)
+    {
+        // 获取请求参数
+        $params = $request->param();
+        
+        // 获取分页参数
+        [$page, $limit] = $request->pageAndLimit();
+        
+        // 处理业务逻辑
+        $data = $this->getService()->getList($params, $page, $limit);
+        
+        // 返回分页数据
+        return page_response($data);
+    }
+    
+    public function create(Request $request)
+    {
+        // 获取POST数据
+        $data = $request->post();
+        
+        // 处理业务逻辑
+        $result = $this->getService()->create($data);
+        
+        // 返回成功响应
+        return success_response('创建成功', $result);
+    }
+}
 ```

+ 4 - 6
composer.json

@@ -20,18 +20,16 @@
     }
   ],
   "require": {
-    "php": ">=8.3",
+    "php": ">=8.4",
     "composer-plugin-api": "^2.0",
     "composer/composer": "^2.9.2",
-    "polyfills/array-first-array-last": "^1.0.1",
-    "topthink/framework": "^8.1",
-    "topthink/think-orm": "^4.0",
-    "topthink/think-helper": "^3.1.11",
+    "topthink/framework": "^8.1.3",
+    "topthink/think-orm": "^4.0.50",
     "topthink/think-queue": "^3.0.12",
     "topthink/think-view": "^2.0.5",
     "topthink/think-worker": "^5.0.2",
     "topthink/think-cors": "^1.0.2",
-    "opis/closure": "^4.3.1",
+    "opis/closure": "^4.4.0",
     "workerman/crontab": "^1.0.7"
   },
   "extra": {

+ 0 - 122
src/Helper.php

@@ -1,122 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace SixShop\Core;
-
-use Random\RandomException;
-use SixShop\Core\Exception\LogicException;
-use think\Paginator;
-use think\Response;
-
-/**
- * 助手类
- * @deprecated 类已弃用, 请使用函数版本的助手方法
- */
-final class Helper
-{
-
-    /**
-     * 返回成功数据
-     * @deprecated 函数已弃用, 请使用 success_response()
-     */
-    public static function success_response(mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success', string $type = 'json', string $xslt = ''): Response
-    {
-        return success_response($data, $status, $code, $msg, $type, $xslt);
-    }
-
-    /**
-     * 返回分页数据
-     * @deprecated 函数已弃用, 请使用 page_response()
-     */
-    public static function page_response(Paginator $page, mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success'): Response
-    {
-        return json([
-            'code' => $code,
-            'status' => $status,
-            'msg' => $msg,
-            'page' => $page,
-            'data' => $data
-        ]);
-    }
-
-    /**
-     * 抛出逻辑异常
-     * @throws LogicException
-     * @deprecated 函数已弃用, 请使用 throw_logic_exception()
-     */
-    public static function throw_logic_exception(string $msg = 'error', int $code = 1, string $status = 'error', mixed $data = [], int $httpCode = 200, $header = [], $options = []): void
-    {
-        throw new LogicException(error_response($msg, $status, $code, $data, $httpCode, $header, $options));
-    }
-
-    /**
-     * 返回失败数据
-     * @deprecated 函数已弃用, 请使用 error_response()
-     */
-    public static function error_response(string $msg = 'error', string $status = 'error', int $code = 1, mixed $data = [], int $httpCode = 400, $header = [], $options = []): Response
-    {
-        return json([
-            'code' => $code,
-            'status' => $status,
-            'msg' => $msg,
-            'data' => $data
-        ], $httpCode, $header, $options);
-    }
-
-    /**
-     * 构建树形结构选项
-     * @param array $data 数据源
-     * @param string $valueField 值字段
-     * @param string $labelField 标签字段
-     * @param string $parentField 父字段
-     * @param int $parentId 父ID
-     * @param string $childrenKey 子节点键
-     * @param bool $preserveOriginal 是否保留原始数据
-     *
-     * @deprecated 函数已弃用, 请使用 build_tree_options()
-     */
-    public static function build_tree_options(
-        array  $data,
-        string $valueField = 'id',
-        string $labelField = 'name',
-        string $parentField = 'parent_id',
-        int    $parentId = 0,
-        string $childrenKey = 'children',
-        bool   $preserveOriginal = true
-    ): array
-    {
-       return build_tree_options($data, $valueField, $labelField, $parentField, $parentId, $childrenKey, $preserveOriginal);
-    }
-
-    /**
-     * 生成随机密码
-     * @param int $length 密码长度
-     * @return string 生成的密码
-     * @throws RandomException
-     * @deprecated 函数已弃用, 请使用 secret_password()
-     */
-    public static function secret_password(int $length = 16): string
-    {
-        return secret_password($length);
-    }
-
-    /**
-     * 获取插件目录
-     * @param string $extensionID 插件ID
-     * @return string 插件目录
-     * @deprecated 函数已弃用, 请使用 extension_path()
-     */
-    public static function extension_path(string $extensionID = ''): string
-    {
-        return extension_path($extensionID);
-    }
-
-    /**
-     *
-     * @deprecated 废弃方法,请使用 extension_name_list 方法
-     */
-    public static function extension_name_list(): array
-    {
-        return extension_name_list();
-    }
-}