diff --git a/src/ZM/Annotation/Framework/Cron.php b/src/ZM/Annotation/Framework/Cron.php index 727c5453..ecd5fc81 100644 --- a/src/ZM/Annotation/Framework/Cron.php +++ b/src/ZM/Annotation/Framework/Cron.php @@ -31,4 +31,9 @@ class Cron extends AnnotationBase ) { $this->expression = new CronExpression($expression); } + + public static function make(string $expression, int $worker_id = 0, bool $no_overlap = false): Cron + { + return new Cron($expression, $worker_id, $no_overlap); + } } diff --git a/src/ZM/Plugin/CommandManual/CommandManualPlugin.php b/src/ZM/Plugin/CommandManual/CommandManualPlugin.php index 6e7a89c5..6b928fe7 100644 --- a/src/ZM/Plugin/CommandManual/CommandManualPlugin.php +++ b/src/ZM/Plugin/CommandManual/CommandManualPlugin.php @@ -51,8 +51,6 @@ class CommandManualPlugin extends ZMPlugin public function __construct(AnnotationParser $parser) { - parent::__construct(__DIR__); - if (config('command_manual.template') !== null) { $this->template = config('command_manual.template'); } diff --git a/src/ZM/Plugin/OneBot12Adapter.php b/src/ZM/Plugin/OneBot12Adapter.php index 42a450ce..e0619932 100644 --- a/src/ZM/Plugin/OneBot12Adapter.php +++ b/src/ZM/Plugin/OneBot12Adapter.php @@ -51,7 +51,6 @@ class OneBot12Adapter extends ZMPlugin public function __construct(string $submodule = '', ?AnnotationParser $parser = null) { - parent::__construct(__DIR__); switch ($submodule) { case '': case 'onebot12': diff --git a/src/ZM/Plugin/Traits/BotActionTrait.php b/src/ZM/Plugin/Traits/BotActionTrait.php new file mode 100644 index 00000000..b53c46aa --- /dev/null +++ b/src/ZM/Plugin/Traits/BotActionTrait.php @@ -0,0 +1,25 @@ +bot_actions[] = $bot_action_annotation; + } + + /** + * @internal + */ + public function getBotActions(): array + { + return $this->bot_actions; + } +} diff --git a/src/ZM/Plugin/Traits/BotCommandTrait.php b/src/ZM/Plugin/Traits/BotCommandTrait.php new file mode 100644 index 00000000..0a0de8ae --- /dev/null +++ b/src/ZM/Plugin/Traits/BotCommandTrait.php @@ -0,0 +1,26 @@ +bot_commands[] = $command; + } + + public function getBotCommands(): array + { + return $this->bot_commands; + } +} diff --git a/src/ZM/Plugin/Traits/BotEventTrait.php b/src/ZM/Plugin/Traits/BotEventTrait.php new file mode 100644 index 00000000..330c5127 --- /dev/null +++ b/src/ZM/Plugin/Traits/BotEventTrait.php @@ -0,0 +1,27 @@ +bot_events[] = $event; + } + + public function getBotEvents(): array + { + return $this->bot_events; + } +} diff --git a/src/ZM/Plugin/Traits/CronTrait.php b/src/ZM/Plugin/Traits/CronTrait.php new file mode 100644 index 00000000..03ee1e41 --- /dev/null +++ b/src/ZM/Plugin/Traits/CronTrait.php @@ -0,0 +1,31 @@ +crons[] = $cron; + } + + /** + * @internal + * @return array + */ + public function getCrons(): array + { + return $this->crons; + } +} diff --git a/src/ZM/Plugin/Traits/EventTrait.php b/src/ZM/Plugin/Traits/EventTrait.php new file mode 100644 index 00000000..c18066f7 --- /dev/null +++ b/src/ZM/Plugin/Traits/EventTrait.php @@ -0,0 +1,25 @@ +events[] = [$event_name, $callback, $level]; + } + + /** + * @internal + */ + public function getEvents(): array + { + return $this->events; + } +} diff --git a/src/ZM/Plugin/Traits/PluginLoadTrait.php b/src/ZM/Plugin/Traits/PluginLoadTrait.php new file mode 100644 index 00000000..51b49cd6 --- /dev/null +++ b/src/ZM/Plugin/Traits/PluginLoadTrait.php @@ -0,0 +1,34 @@ +on_plugin_load = $callback; + } + + /** + * 调用插件加载前回调(需要在解析插件的注解时调用,并传入注解解析器) + * + * @param AnnotationParser $parser 注解解析器 + * @internal + */ + public function emitPluginLoad(AnnotationParser $parser): void + { + if (is_callable($this->on_plugin_load)) { + ($this->on_plugin_load)($parser); + } + } +} diff --git a/src/ZM/Plugin/Traits/RouteTrait.php b/src/ZM/Plugin/Traits/RouteTrait.php new file mode 100644 index 00000000..2420bfed --- /dev/null +++ b/src/ZM/Plugin/Traits/RouteTrait.php @@ -0,0 +1,30 @@ +routes[] = $route; + } + + + /** + * @internal + */ + public function getRoutes(): array + { + return $this->routes; + } +} diff --git a/src/ZM/Plugin/ZMPlugin.php b/src/ZM/Plugin/ZMPlugin.php index d8a6052b..3ab14f7d 100644 --- a/src/ZM/Plugin/ZMPlugin.php +++ b/src/ZM/Plugin/ZMPlugin.php @@ -4,77 +4,17 @@ declare(strict_types=1); namespace ZM\Plugin; -use ZM\Annotation\Http\Route; -use ZM\Annotation\OneBot\BotCommand; -use ZM\Annotation\OneBot\BotEvent; - /** * 单文件插件声明类 */ class ZMPlugin { - /** @var string 插件目录 */ - protected string $dir; - - /** @var array 机器人事件列表 */ - protected array $bot_events = []; - - /** @var array 机器人指令列表 */ - protected array $bot_commands = []; - - /** @var array 全局的事件列表 */ - protected array $events = []; - - /** @var array 注册的路由列表 */ - protected array $routes = []; - - public function __construct(string $dir) - { - $this->dir = $dir; - } - - public function getDir(): string - { - return $this->dir; - } - - public function addBotEvent(BotEvent $event) - { - $this->bot_events[] = $event; - } - - public function addBotCommand(BotCommand $command) - { - $this->bot_commands[] = $command; - } - - public function addEvent(string $event_name, callable $callback, int $level = 20) - { - $this->events[] = [$event_name, $callback, $level]; - } - - public function addHttpRoute(Route $route) - { - $this->routes[] = $route; - } - - public function getBotEvents(): array - { - return $this->bot_events; - } - - public function getBotCommands(): array - { - return $this->bot_commands; - } - - public function getEvents(): array - { - return $this->events; - } - - public function getRoutes(): array - { - return $this->routes; - } + use Traits\BotActionTrait; + use Traits\BotCommandTrait; + use Traits\BotEventTrait; + use Traits\CronTrait; + use Traits\EventTrait; + use Traits\InitTrait; + use Traits\PluginLoadTrait; + use Traits\RouteTrait; }