Просмотр исходного кода

refactor(sixshop-core): 删除扩展缓存只保留一个,提取静态方法改为函数

runphp 7 месяцев назад
Родитель
Сommit
9c58090962
4 измененных файлов с 132 добавлено и 11 удалено
  1. 120 1
      src/Helper.php
  2. 1 4
      src/Plugin.php
  3. 2 3
      src/Service/CommandService.php
  4. 9 3
      src/Service/CoreService.php

+ 120 - 1
src/Helper.php

@@ -10,11 +10,114 @@ use think\Container;
 use think\Paginator;
 use think\Response;
 
+// 函数版本的Helper方法
+
+
+/**
+ * 返回成功数据
+ */
+function success_response(mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success', string $type = 'json', string $xslt = ''): Response
+{
+    return Helper::success_response($data, $status, $code, $msg, $type, $xslt);
+}
+
+
+/**
+ * 返回分页数据
+ */
+function page_response(Paginator $page, mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success'): Response
+{
+    return Helper::page_response($page, $data, $status, $code, $msg);
+}
+
+
+/**
+ * 返回失败数据
+ */
+function error_response(string $msg = 'error', string $status = 'error', int $code = 1, mixed $data = [], int $httpCode = 400, $header = [], $options = []): Response
+{
+    return Helper::error_response($msg, $status, $code, $data, $httpCode, $header, $options);
+}
+
+
+/**
+ * 抛出逻辑异常
+ * @throws LogicException
+ */
+function throw_logic_exception(string $msg = 'error', int $code = 1, string $status = 'error', mixed $data = [], int $httpCode = 200, $header = [], $options = []): void
+{
+    Helper::throw_logic_exception($msg, $code, $status, $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 是否保留原始数据
+ */
+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 Helper::build_tree_options($data, $valueField, $labelField, $parentField, $parentId, $childrenKey, $preserveOriginal);
+}
+
+
+/**
+ * 生成随机密码
+ * @param int $length 密码长度
+ * @return string 生成的密码
+ */
+function secret_password(int $length = 16): string
+{
+    return Helper::secret_password($length);
+}
+
+
+/**
+ * 获取扩展路径
+ */
+function extension_path(string $extensionID = ''): string
+{
+    return Helper::extension_path($extensionID);
+}
+
+
+/**
+ * 获取扩展名称列表
+ */
+function extension_name_list(): array
+{
+    return CoreService::$extensionNameList;
+}
+
+
+/**
+ * 获取扩展Composer信息
+ */
+function extension_composer_info(string $extensionID): array
+{
+    return CoreService::$extensionComposerMap[$extensionID];
+}
+
+
 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
     {
@@ -39,6 +142,7 @@ final class Helper
 
     /**
      * 返回分页数据
+     * @deprecated 函数已弃用, 请使用 page_response()
      */
     public static function page_response(Paginator $page, mixed $data = [], string $status = 'ok', int $code = 200, string $msg = 'success'): Response
     {
@@ -53,6 +157,7 @@ final class Helper
 
     /**
      * 返回失败数据
+     * @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
     {
@@ -67,6 +172,7 @@ final class Helper
     /**
      * 抛出逻辑异常
      * @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
     {
@@ -82,6 +188,8 @@ final class Helper
      * @param int $parentId 父ID
      * @param string $childrenKey 子节点键
      * @param bool $preserveOriginal 是否保留原始数据
+     *
+     * @deprecated 函数已弃用, 请使用 build_tree_options()
      */
     public static function build_tree_options(
         array  $data,
@@ -129,6 +237,7 @@ final class Helper
      * 生成随机密码
      * @param int $length 密码长度
      * @return string 生成的密码
+     * @deprecated 函数已弃用, 请使用 secret_password()
      */
     public static function secret_password(int $length = 16): string
     {
@@ -154,6 +263,12 @@ final class Helper
         return str_shuffle($password);
     }
 
+    /**
+     * 获取插件目录
+     * @param string $extensionID 插件ID
+     * @return string 插件目录
+     * @deprecated 函数已弃用, 请使用 extension_path()
+     */
     public static function extension_path(string $extensionID = ''): string
     {
         if (!$extensionID) {
@@ -169,7 +284,11 @@ final class Helper
         return CoreService::$extensionPath . $extensionID . DIRECTORY_SEPARATOR;
     }
 
-    public static function extension_name_list()
+    /**
+     *
+     * @deprecated 废弃方法,请使用 extension_name_list 方法
+     */
+    public static function extension_name_list(): array
     {
         return CoreService::$extensionNameList;
     }

+ 1 - 4
src/Plugin.php

@@ -22,17 +22,14 @@ class Plugin implements PluginInterface, EventSubscriberInterface
     {
         $installer = new ExtensionInstaller($io, $composer, self::EXTENSION_TYPE);
         $composer->getInstallationManager()->addInstaller($installer);
-        $io->writeError('<info>activate: ' . $composer->getPackage()->getName() . '</info>');
     }
 
-    public function deactivate(Composer $composer, IOInterface $io)
+    public function deactivate(Composer $composer, IOInterface $io): void
     {
-        $io->writeError('<info>deactivate: ' . $composer->getPackage()->getName() . '</info>');
     }
 
     public function uninstall(Composer $composer, IOInterface $io): void
     {
-        $io->writeError('<info>uninstall: ' . $composer->getPackage()->getName() . '</info>');
     }
 
     public static function getSubscribedEvents(): array

+ 2 - 3
src/Service/CommandService.php

@@ -3,9 +3,8 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Service;
 
-use SixShop\Core\Helper;
-use think\App;
 use think\exception\ClassNotFoundException;
+use function SixShop\Core\extension_name_list;
 
 class CommandService
 {
@@ -16,7 +15,7 @@ class CommandService
     public function init(\Closure $closure): void
     {
         $commands = [];
-        foreach (Helper::extension_name_list() as $extensionName) {
+        foreach (extension_name_list() as $extensionName) {
             try {
                 $extension = $this->autoloadService->getExtension($extensionName);
             } catch (ClassNotFoundException $_) {

+ 9 - 3
src/Service/CoreService.php

@@ -45,7 +45,7 @@ class CoreService extends Service
         $this->app->make(HookAttributeService::class)->init();
         $this->app->event->trigger('hook_init', $this->app);
         $this->app->event->listen(HttpRun::class, function () {
-            $this->registerRoutes($this->app->make(RegisterRouteService::class)->init($this->app));
+            $this->registerRoutes($this->app->make(RegisterRouteService::class)->init());
         });
 
         $this->app->make(CommandService::class)->init(function ($commands) {
@@ -58,14 +58,20 @@ class CoreService extends Service
         if (!empty(self::$extensionComposerMap)) {
             return;
         }
+        $runtimePath = $this->app->getRootPath() . 'runtime/';
         $reference = Plugin::getInstalledSixShopExtensions()['root']['reference'];
-        $extensionComposerFile = $this->app->getRootPath() . 'runtime/extension_' .$reference.'.php';
+        $extensionComposerFile = $runtimePath . 'extension_' .$reference.'.php';
         if (file_exists($extensionComposerFile)) {
             self::$extensionComposerMap = require $extensionComposerFile;
             return;
         }
+        $files = array_diff(scandir($runtimePath), ['.', '..']);
+        foreach ($files as $file) {
+            if (strpos($file, 'extension_') === 0 && substr($file, -4) === '.php') {
+                unlink($runtimePath . $file);
+            }
+        }
         foreach (InstalledVersions::getInstalledPackagesByType(Plugin::EXTENSION_TYPE) as $item) {
-            //$version = InstalledVersions::getInstallPath($item);
             $installPath = InstalledVersions::getInstallPath($item);
             $composerJson = new JsonFile($installPath . '/composer.json');
             $composer = $composerJson->read();