mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-02 14:25:38 +08:00
separate ZMPlugin to traits
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ class OneBot12Adapter extends ZMPlugin
|
||||
|
||||
public function __construct(string $submodule = '', ?AnnotationParser $parser = null)
|
||||
{
|
||||
parent::__construct(__DIR__);
|
||||
switch ($submodule) {
|
||||
case '':
|
||||
case 'onebot12':
|
||||
|
||||
25
src/ZM/Plugin/Traits/BotActionTrait.php
Normal file
25
src/ZM/Plugin/Traits/BotActionTrait.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\OneBot\BotAction;
|
||||
|
||||
trait BotActionTrait
|
||||
{
|
||||
protected array $bot_actions = [];
|
||||
|
||||
public function onBotAction(BotAction $bot_action_annotation): void
|
||||
{
|
||||
$this->bot_actions[] = $bot_action_annotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getBotActions(): array
|
||||
{
|
||||
return $this->bot_actions;
|
||||
}
|
||||
}
|
||||
26
src/ZM/Plugin/Traits/BotCommandTrait.php
Normal file
26
src/ZM/Plugin/Traits/BotCommandTrait.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\OneBot\BotCommand;
|
||||
|
||||
trait BotCommandTrait
|
||||
{
|
||||
/** @var array 机器人指令列表 */
|
||||
protected array $bot_commands = [];
|
||||
|
||||
/**
|
||||
* 添加一个 OneBot 机器人命令
|
||||
*
|
||||
* @param BotCommand $command BotCommand 注解对象
|
||||
*/
|
||||
public function addBotCommand(BotCommand $command): void
|
||||
{
|
||||
$this->bot_commands[] = $command;
|
||||
}
|
||||
|
||||
public function getBotCommands(): array
|
||||
{
|
||||
return $this->bot_commands;
|
||||
}
|
||||
}
|
||||
27
src/ZM/Plugin/Traits/BotEventTrait.php
Normal file
27
src/ZM/Plugin/Traits/BotEventTrait.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\OneBot\BotEvent;
|
||||
|
||||
trait BotEventTrait
|
||||
{
|
||||
/** @var array 机器人事件列表 */
|
||||
protected array $bot_events = [];
|
||||
|
||||
/**
|
||||
* 添加一个 OneBot 机器人事件
|
||||
* @param BotEvent $event BotEvent 注解对象
|
||||
*/
|
||||
public function addBotEvent(BotEvent $event): void
|
||||
{
|
||||
$this->bot_events[] = $event;
|
||||
}
|
||||
|
||||
public function getBotEvents(): array
|
||||
{
|
||||
return $this->bot_events;
|
||||
}
|
||||
}
|
||||
31
src/ZM/Plugin/Traits/CronTrait.php
Normal file
31
src/ZM/Plugin/Traits/CronTrait.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\Framework\Cron;
|
||||
|
||||
trait CronTrait
|
||||
{
|
||||
/** @var Cron[] 计划任务列表 */
|
||||
protected array $crons = [];
|
||||
|
||||
/**
|
||||
* 添加一个计划任务
|
||||
*
|
||||
* @param Cron $cron 计划任务注解对象
|
||||
* @return void
|
||||
*/
|
||||
public function addCron(Cron $cron): void
|
||||
{
|
||||
$this->crons[] = $cron;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @return array
|
||||
*/
|
||||
public function getCrons(): array
|
||||
{
|
||||
return $this->crons;
|
||||
}
|
||||
}
|
||||
25
src/ZM/Plugin/Traits/EventTrait.php
Normal file
25
src/ZM/Plugin/Traits/EventTrait.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
trait EventTrait
|
||||
{
|
||||
/** @var array 全局的事件列表 */
|
||||
protected array $events = [];
|
||||
|
||||
/**
|
||||
* 添加一个框架底层的事件
|
||||
*/
|
||||
public function addEvent(string $event_name, callable $callback, int $level = 20): void
|
||||
{
|
||||
$this->events[] = [$event_name, $callback, $level];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getEvents(): array
|
||||
{
|
||||
return $this->events;
|
||||
}
|
||||
}
|
||||
34
src/ZM/Plugin/Traits/PluginLoadTrait.php
Normal file
34
src/ZM/Plugin/Traits/PluginLoadTrait.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\AnnotationParser;
|
||||
|
||||
trait PluginLoadTrait
|
||||
{
|
||||
/** @var null|mixed 插件加载时的回调 */
|
||||
protected mixed $on_plugin_load = null;
|
||||
|
||||
/**
|
||||
* 设置当前插件的插件加载前回调
|
||||
*
|
||||
* @param callable $callback 回调函数
|
||||
*/
|
||||
public function onPluginLoad(callable $callback): void
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/ZM/Plugin/Traits/RouteTrait.php
Normal file
30
src/ZM/Plugin/Traits/RouteTrait.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Plugin\Traits;
|
||||
|
||||
use ZM\Annotation\Http\Route;
|
||||
|
||||
trait RouteTrait
|
||||
{
|
||||
/** @var array 注册的路由列表 */
|
||||
protected array $routes = [];
|
||||
|
||||
/**
|
||||
* 添加一个 HTTP 路由
|
||||
*
|
||||
* @param Route $route Route 注解对象
|
||||
*/
|
||||
public function addHttpRoute(Route $route): void
|
||||
{
|
||||
$this->routes[] = $route;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getRoutes(): array
|
||||
{
|
||||
return $this->routes;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user