소스 검색

feat(core): 优化扩展启动逻辑并修复类型提示问题

- 修改 CoreService 启动逻辑,将 system 扩展实例存入 bootedExtensionMap
- 修复 functions.php 中 running_in_console 函数返回值缺失问题
- 统一多个服务类中的可空参数类型声明
- 新增 HelloCron 定时任务并注册到扩展中
- 优化 GatheringCrontabEventHook 中定时任务初始化逻辑
- 增强 Cron 注解解析与任务执行检查机制
runphp 3 달 전
부모
커밋
4d7354b313
2개의 변경된 파일13개의 추가작업 그리고 2개의 파일을 삭제
  1. 12 1
      src/Hook/GatheringCrontabEventHook.php
  2. 1 1
      src/Trait/ConfigTrait.php

+ 12 - 1
src/Hook/GatheringCrontabEventHook.php

@@ -11,6 +11,7 @@ use SixShop\System\Event\CrontabWorkerStartEvent;
 use SixShop\System\Event\GetCronJobsEvent;
 use SixShop\System\ExtensionManager;
 use think\App;
+use think\facade\Log;
 use Workerman\Crontab\Crontab;
 use function SixShop\Core\extension_name_list;
 
@@ -40,12 +41,22 @@ class GatheringCrontabEventHook
         $this->app->event->trigger($event);
         foreach ($event as $cronJobClass) {
             $ref = new ReflectionClass($cronJobClass);
+            $objectAttts = $ref->getAttributes(Cron::class);
+            $cronJob = $this->app->make($cronJobClass);
+            foreach ($objectAttts as $attribute) {
+                $cronInstance = $attribute->newInstance();
+                if (is_callable($cronJob)) {
+                    new Crontab($cronInstance->rule, $cronJob, $cronInstance->name?:$cronJobClass);
+                } else {
+                    Log::warning("Cron job {$cronJobClass} is not callable");
+                }
+            }
             foreach ($ref->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
                 $attributes = $method->getAttributes(Cron::class);
                 foreach ($attributes as $attribute) {
                     $cronInstance = $attribute->newInstance();
                     $name = $cronInstance->name ?: $cronJobClass . '@' . $method->getName();
-                    new Crontab($cronInstance->rule, [$this->app->make($cronJobClass), $method->getName()], $name);
+                    new Crontab($cronInstance->rule, [$cronJob, $method->getName()], $name);
                 }
             }
         }

+ 1 - 1
src/Trait/ConfigTrait.php

@@ -12,7 +12,7 @@ trait ConfigTrait
     {
     }
 
-    public function getConfig(string $key = null, mixed $default = null): mixed
+    public function getConfig(?string $key = null, mixed $default = null): mixed
     {
         if (empty($this->options)) {
             if (!method_exists($this, 'getExtensionID')) {