update to 3.0.0-alpha2 (build 610): refactor driver

This commit is contained in:
crazywhalecc
2022-08-01 16:31:54 +08:00
committed by Jerry Ma
parent f2a12634a4
commit 41b058aeaf
186 changed files with 1922 additions and 15444 deletions

View File

@@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation;
use Generator;
use Throwable;
use ZM\Annotation\Middleware\Middleware;
use ZM\Exception\InterruptException;
/**
* 注解调用器,原 EventDispatcher
*/
class AnnotationHandler
{
public const STATUS_NORMAL = 0; // 正常结束
public const STATUS_INTERRUPTED = 1; // 被interrupt了不管在什么地方
public const STATUS_EXCEPTION = 2; // 执行过程中抛出了异常
public const STATUS_BEFORE_FAILED = 3; // 中间件HandleBefore返回了false所以不执行此方法
public const STATUS_RULE_FAILED = 4; // 判断事件执行的规则函数判定为false所以不执行此方法
/** @var AnnotationBase|string */
private $annotation_class;
/** @var callable */
private $rule_callback;
/** @var callable */
private $return_callback;
/** @var int */
private $status = self::STATUS_NORMAL;
/** @var mixed */
private $return_val;
public function __construct(string $annotation_class)
{
$this->annotation_class = $annotation_class;
logger()->debug('开始分发注解 {annotation}', ['annotation' => $annotation_class]);
}
public static function interrupt($return_var = null)
{
throw new InterruptException($return_var);
}
public function setRuleCallback(callable $rule): AnnotationHandler
{
logger()->debug('注解调用器设置事件ruleFunc: {annotation}', ['annotation' => $this->annotation_class]);
$this->rule_callback = $rule;
return $this;
}
public function setReturnCallback(callable $return): AnnotationHandler
{
logger()->debug('注解调用器设置事件returnFunc: {annotation}', ['annotation' => $this->annotation_class]);
$this->return_callback = $return;
return $this;
}
/**
* @param mixed ...$params
* @throws Throwable
*/
public function handleAll(...$params)
{
try {
foreach ((AnnotationMap::$_list[$this->annotation_class] ?? []) as $v) {
$this->handle($v, $this->rule_callback, ...$params);
if ($this->status == self::STATUS_BEFORE_FAILED || $this->status == self::STATUS_RULE_FAILED) {
$this->status = self::STATUS_NORMAL;
continue;
}
if (is_callable($this->return_callback) && $this->status === self::STATUS_NORMAL) {
($this->return_callback)($this->return_val);
}
}
} catch (InterruptException $e) {
$this->return_val = $e->return_var;
$this->status = self::STATUS_INTERRUPTED;
} catch (Throwable $e) {
$this->status = self::STATUS_EXCEPTION;
throw $e;
}
}
public function handle(AnnotationBase $v, ?callable $rule_callback = null, ...$params): bool
{
$target_class = resolve($v->class);
$target_method = $v->method;
// 先执行规则
if ($rule_callback !== null && !$rule_callback($this, $params)) {
$this->status = self::STATUS_RULE_FAILED;
return false;
}
// 检查中间件
$mid_obj = [];
$before_result = true;
foreach ($this->getRegisteredMiddlewares($target_class, $target_method) as $v) {
$mid_obj[] = $v[0]; // 投喂中间件
if ($v[1] !== '') { // 顺带执行before
if (function_exists('container')) {
$before_result = container()->call([$v[0], $v[1]], $params);
} else {
$before_result = call_user_func([$v[0], $v[1]], $params);
}
if ($before_result === false) {
break;
}
}
}
$mid_obj_cnt1 = count($mid_obj) - 1;
if ($before_result) { // before全部通过了
try {
// 执行注解绑定的方法
// TODO: 记得完善好容器后把这里的这个if else去掉
if (function_exists('container')) {
$this->return_val = container()->call([$target_class, $target_method], $params);
} else {
$this->return_val = call_user_func([$target_class, $target_method], $params);
}
} catch (Throwable $e) {
if ($e instanceof InterruptException) {
throw $e;
}
for ($i = $mid_obj_cnt1; $i >= 0; --$i) {
$obj = $mid_obj[$i];
foreach ($obj[3] as $name => $method) {
if ($e instanceof $name) {
$obj[0]->{$method}($e);
return false;
}
}
}
throw $e;
}
} else {
$this->status = self::STATUS_BEFORE_FAILED;
}
for ($i = $mid_obj_cnt1; $i >= 0; --$i) {
if ($mid_obj[$i][2] !== '') {
$mid_obj[$i][0]->{$mid_obj[$i][2]}($this->return_val);
}
}
return true;
}
/**
* 获取注册过的中间件
*
* @param object|string $class 类对象
* @param string $method 方法名称
*/
private function getRegisteredMiddlewares($class, string $method): Generator
{
foreach (AnnotationMap::$_map[get_class($class)][$method] ?? [] as $annotation) {
if ($annotation instanceof Middleware) {
$name = $annotation->name;
$reg_mid = AnnotationMap::$_middleware_map[$name]['class'] ?? null;
if ($reg_mid === null) {
logger()->error('Not a valid middleware name: {name}', ['name' => $name]);
continue;
}
$obj = new $reg_mid($annotation->params);
yield [
$obj,
AnnotationMap::$_middleware_map[$name]['before'] ?? '',
AnnotationMap::$_middleware_map[$name]['after'] ?? '',
AnnotationMap::$_middleware_map[$name]['exceptions'] ?? [],
];
}
}
return [];
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation;
/**
* 注解全局存取位置
*/
class AnnotationMap
{
/**
* 存取注解对象的列表
*
* @var array<string, array<AnnotationBase>>
* @internal
*/
public static $_list = [];
/**
* @var array<string, array<string, array<AnnotationBase>>>
* @internal
*/
public static $_map = [];
/**
* @var array
* @internal
*/
public static $_middleware_map = [];
}

View File

@@ -10,21 +10,20 @@ use Koriym\Attributes\DualReader;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ZM\Annotation\Http\HandleAfter;
use ZM\Annotation\Http\HandleBefore;
use ZM\Annotation\Http\HandleException;
use ZM\Annotation\Http\Middleware;
use ZM\Annotation\Http\MiddlewareClass;
use ZM\Annotation\Http\RequestMapping;
use Symfony\Component\Routing\RouteCollection;
use ZM\Annotation\Http\Controller;
use ZM\Annotation\Http\Route;
use ZM\Annotation\Interfaces\ErgodicAnnotation;
use ZM\Annotation\Interfaces\Level;
use ZM\Annotation\Module\Closed;
use ZM\Annotation\Middleware\HandleAfter;
use ZM\Annotation\Middleware\HandleBefore;
use ZM\Annotation\Middleware\HandleException;
use ZM\Annotation\Middleware\Middleware;
use ZM\Annotation\Middleware\MiddlewareClass;
use ZM\Config\ZMConfig;
use ZM\Event\EventManager;
use ZM\Exception\AnnotationException;
use ZM\Utils\Manager\RouteManager;
use ZM\Utils\ZMUtil;
use function server;
use ZM\Exception\ConfigException;
use ZM\Store\FileSystem;
use ZM\Store\InternalGlobals;
class AnnotationParser
{
@@ -60,14 +59,15 @@ class AnnotationParser
/**
* 注册各个模块类的注解和模块level的排序
* @throws ReflectionException
* @throws ConfigException
*/
public function registerMods()
public function parseAll()
{
foreach ($this->path_list as $path) {
logger()->debug('parsing annotation in ' . $path[0] . ':' . $path[1]);
$all_class = ZMUtil::getClassesPsr4($path[0], $path[1]);
$all_class = FileSystem::getClassesPsr4($path[0], $path[1]);
$conf = ZMConfig::get('global', 'runtime')['annotation_reader_ignore'] ?? [];
$conf = ZMConfig::get('global.runtime.annotation_reader_ignore');
if (isset($conf['name']) && is_array($conf['name'])) {
foreach ($conf['name'] as $v) {
AnnotationReader::addGlobalIgnoredName($v);
@@ -145,7 +145,7 @@ class AnnotationParser
/* @var AnnotationBase $method_anno */
$method_anno->class = $v;
$method_anno->method = $method_name;
if (!($method_anno instanceof Middleware) && ($middlewares = ZMConfig::get('global', 'runtime')['global_middleware_binding'][get_class($method_anno)] ?? []) !== []) {
if (!($method_anno instanceof Middleware) && ($middlewares = ZMConfig::get('global.global_middleware_binding')[get_class($method_anno)] ?? []) !== []) {
if (!isset($inserted[$v][$method_name])) {
// 在这里在其他中间件前插入插入全局的中间件
foreach ($middlewares as $middleware) {
@@ -156,12 +156,12 @@ class AnnotationParser
}
$inserted[$v][$method_name] = true;
}
} elseif ($method_anno instanceof RequestMapping) {
RouteManager::importRouteByAnnotation($method_anno, $method_name, $v, $methods_annotations);
} elseif ($method_anno instanceof Route) {
$this->addRouteAnnotation($method_anno, $method_name, $v, $methods_annotations);
} elseif ($method_anno instanceof Middleware) {
$this->middleware_map[$method_anno->class][$method_anno->method][] = $method_anno;
} else {
EventManager::$event_map[$method_anno->class][$method_anno->method][] = $method_anno;
AnnotationMap::$_map[$method_anno->class][$method_anno->method][] = $method_anno;
}
}
}
@@ -214,9 +214,7 @@ class AnnotationParser
*/
public function addRegisterPath(string $path, string $indoor_name)
{
if (server()->worker_id === 0) {
logger()->debug('Add register path: ' . $path . ' => ' . $indoor_name);
}
logger()->debug('Add register path: ' . $path . ' => ' . $indoor_name);
$this->path_list[] = [$path, $indoor_name];
}
@@ -238,26 +236,7 @@ class AnnotationParser
}
}
/**
* @throws AnnotationException
*/
public function verifyMiddlewares()
{
if ((ZMConfig::get('global', 'runtime')['middleware_error_policy'] ?? 1) === 2) {
// 我承认套三层foreach很不优雅但是这个会很快的。
foreach ($this->middleware_map as $v) {
foreach ($v as $vs) {
foreach ($vs as $mid) {
if (!isset($this->middlewares[$mid->middleware])) {
throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$mid->middleware}\"!");
}
}
}
}
}
}
public function getRunTime()
public function getUsedTime()
{
return microtime(true) - $this->start_time;
}
@@ -287,4 +266,27 @@ class AnnotationParser
}
return $result;
}
private function addRouteAnnotation(Route $vss, $method, $class, $methods_annotations)
{
if (InternalGlobals::$routes === null) {
InternalGlobals::$routes = new RouteCollection();
}
// 拿到所属方法的类上面有没有控制器的注解
$prefix = '';
foreach ($methods_annotations as $annotation) {
if ($annotation instanceof Controller) {
$prefix = $annotation->prefix;
break;
}
}
$tail = trim($vss->route, '/');
$route_name = $prefix . ($tail === '' ? '' : '/') . $tail;
logger()->debug('添加路由:' . $route_name);
$route = new \Symfony\Component\Routing\Route($route_name, ['_class' => $class, '_method' => $method]);
$route->setMethods($vss->request_method);
InternalGlobals::$routes->add(md5($route_name), $route);
}
}

View File

@@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class CQAPIResponse
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class CQAPIResponse extends AnnotationBase
{
/**
* @var int
* @Required()
*/
public $retcode;
public function __construct($retcode)
{
$this->retcode = $retcode;
}
}

View File

@@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQAfter
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class CQAfter extends AnnotationBase implements Level
{
/**
* @var string
* @Required()
*/
public $cq_event;
public $level = 20;
public function __construct($cq_event, $level = 20)
{
$this->cq_event = $cq_event;
$this->level = $level;
}
/**
* @return mixed
*/
public function getLevel()
{
return $this->level;
}
/**
* @param mixed $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,52 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQBefore
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class CQBefore extends AnnotationBase implements Level
{
/**
* @var string
* @Required()
*/
public $cq_event;
public $level = 20;
public function __construct($cq_event, $level = 20)
{
$this->cq_event = $cq_event;
$this->level = $level;
}
/**
* @return int 返回等级
*/
public function getLevel(): int
{
return $this->level;
}
/**
* @param mixed $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,86 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQCommand
* @Annotation
* @NamedArgumentConstructor
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CQCommand extends AnnotationBase implements Level
{
/** @var string */
public $match = '';
/** @var string */
public $pattern = '';
/** @var string */
public $regex = '';
/** @var string */
public $start_with = '';
/** @var string */
public $end_with = '';
/** @var string */
public $keyword = '';
/** @var string[] */
public $alias = [];
/** @var string */
public $message_type = '';
/** @var int */
public $user_id = 0;
/** @var int */
public $group_id = 0;
/** @var int */
public $discuss_id = 0;
/** @var int */
public $level = 20;
public function __construct($match = '', $pattern = '', $regex = '', $start_with = '', $end_with = '', $keyword = '', $alias = [], $message_type = '', $user_id = 0, $group_id = 0, $discuss_id = 0, $level = 20)
{
$this->match = $match;
$this->pattern = $pattern;
$this->regex = $regex;
$this->start_with = $start_with;
$this->end_with = $end_with;
$this->keyword = $keyword;
$this->alias = $alias;
$this->message_type = $message_type;
$this->user_id = $user_id;
$this->group_id = $group_id;
$this->discuss_id = $discuss_id;
$this->level = $level;
}
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQMessage
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CQMessage extends AnnotationBase implements Level
{
/**
* @var string
*/
public $message_type = '';
/** @var int */
public $user_id = 0;
/** @var int */
public $group_id = 0;
/** @var int */
public $discuss_id = 0;
/** @var string */
public $message = '';
/** @var string */
public $raw_message = '';
/** @var int */
public $level = 20;
public function __construct($message_type = '', $user_id = 0, $group_id = 0, $discuss_id = 0, $message = '', $raw_message = '', $level = 20)
{
$this->message_type = $message_type;
$this->user_id = $user_id;
$this->group_id = $group_id;
$this->discuss_id = $discuss_id;
$this->message = $message;
$this->raw_message = $raw_message;
$this->level = $level;
}
public function getLevel(): int
{
return $this->level;
}
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,53 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQMetaEvent
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CQMetaEvent extends AnnotationBase implements Level
{
/**
* @var string
* @Required()
*/
public $meta_event_type;
/** @var int */
public $level = 20;
public function __construct($meta_event_type, $level = 20)
{
$this->meta_event_type = $meta_event_type;
$this->level = $level;
}
/**
* @return int 返回等级
*/
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,58 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQNotice
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CQNotice extends AnnotationBase implements Level
{
/** @var string */
public $notice_type = '';
/** @var string */
public $sub_type = '';
/** @var int */
public $group_id = 0;
/** @var int */
public $operator_id = 0;
/** @var int */
public $level = 20;
public function __construct($notice_type = '', $sub_type = '', $group_id = 0, $operator_id = 0, $level = 20)
{
$this->notice_type = $notice_type;
$this->sub_type = $sub_type;
$this->group_id = $group_id;
$this->operator_id = $operator_id;
$this->level = $level;
}
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,58 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
/**
* Class CQRequest
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CQRequest extends AnnotationBase implements Level
{
/** @var string */
public $request_type = '';
/** @var string */
public $sub_type = '';
/** @var int */
public $user_id = 0;
/** @var string */
public $comment = '';
/** @var int */
public $level = 20;
public function __construct($request_type = '', $sub_type = '', $user_id = 0, $comment = '', $level = 20)
{
$this->request_type = $request_type;
$this->sub_type = $sub_type;
$this->user_id = $user_id;
$this->comment = $comment;
$this->level = $level;
}
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,146 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\CQ;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\ErgodicAnnotation;
use ZM\Exception\InvalidArgumentException;
use ZM\Exception\ZMKnownException;
/**
* Class CommandArgument
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CommandArgument extends AnnotationBase implements ErgodicAnnotation
{
/**
* @var string
* @Required()
*/
public $name;
/**
* @var string
*/
public $description = '';
/**
* @var string
*/
public $type = 'string';
/**
* @var bool
*/
public $required = false;
/**
* @var string
*/
public $prompt = '';
/**
* @var string
*/
public $default = '';
/**
* @var int
*/
public $timeout = 60;
/**
* @var int
*/
public $error_prompt_policy = 1;
/**
* @param string $name 参数名称(可以是中文)
* @param string $description 参数描述(默认为空)
* @param bool $required 参数是否必需如果是必需为true默认为false
* @param string $prompt 当参数为必需时,返回给用户的提示输入的消息(默认为"请输入$name"
* @param string $default 当required为false时未匹配到参数将自动使用default值默认为空
* @param int $timeout prompt超时时间默认为60秒
* @throws InvalidArgumentException|ZMKnownException
*/
public function __construct(
string $name,
string $description = '',
string $type = 'string',
bool $required = false,
string $prompt = '',
string $default = '',
int $timeout = 60,
int $error_prompt_policy = 1
) {
$this->name = $name;
$this->description = $description;
$this->type = $this->fixTypeName($type);
$this->required = $required;
$this->prompt = $prompt;
$this->default = $default;
$this->timeout = $timeout;
$this->error_prompt_policy = $error_prompt_policy;
if ($this->type === 'bool') {
if ($this->default === '') {
$this->default = 'yes';
}
if (!in_array($this->default, array_merge(TRUE_LIST, FALSE_LIST))) {
throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为布尔型,检测到非法的默认值 ' . $this->default);
}
} elseif ($this->type === 'number') {
if ($this->default === '') {
$this->default = '0';
}
if (!is_numeric($this->default)) {
throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为数字型,检测到非法的默认值 ' . $this->default);
}
}
}
public function getTypeErrorPrompt(): string
{
return '参数类型错误,请重新输入!';
}
public function getErrorQuitPrompt(): string
{
return '参数类型错误,停止输入!';
}
/**
* @throws ZMKnownException
*/
protected function fixTypeName(string $type): string
{
$table = [
'str' => 'string',
'string' => 'string',
'strings' => 'string',
'byte' => 'string',
'num' => 'number',
'number' => 'number',
'int' => 'number',
'float' => 'number',
'double' => 'number',
'boolean' => 'bool',
'bool' => 'bool',
'true' => 'bool',
'any' => 'any',
'all' => 'any',
'*' => 'any',
];
if (array_key_exists($type, $table)) {
return $table[$type];
}
throw new ZMKnownException(zm_internal_errcode('E00077') . 'Invalid argument type: ' . $type . ', only support any, string, number and bool !');
}
}

View File

@@ -2,12 +2,11 @@
declare(strict_types=1);
namespace ZM\Annotation\Module;
namespace ZM\Annotation;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class Closed

View File

@@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Command;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class TerminalCommand
* @Annotation
* @NamedArgumentConstructor
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class TerminalCommand extends AnnotationBase
{
/**
* @var string
* @Required()
*/
public $command;
public $alias = '';
/**
* @var string
*/
public $description = '';
public function __construct($command, $alias = '', $description = '')
{
$this->command = $command;
$this->alias = $alias;
$this->description = $description;
}
}

View File

@@ -1,85 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Cron;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class Cron extends AnnotationBase
{
/**
* @var string
* @Required()
*/
public $expression;
/**
* @var int
*/
public $worker_id = 0;
/**
* @var int
*/
public $check_delay_time = 20000;
/**
* @var int
*/
public $max_iteration_count = 1000;
/**
* @var int Cron执行状态
*/
private $status = 0;
private $record_next_time = 0;
public function __construct(string $expression, int $worker_id = 0, int $check_delay_time = 20000, int $max_iteration_count = 1000)
{
$this->expression = $expression;
$this->worker_id = $worker_id;
$this->check_delay_time = $check_delay_time;
$this->max_iteration_count = $max_iteration_count;
}
public function getStatus(): int
{
return $this->status;
}
/**
* @internal
*/
public function setStatus(int $status): void
{
$this->status = $status;
}
/**
* @internal
*/
public function getRecordNextTime(): int
{
return $this->record_next_time;
}
/**
* @internal
*/
public function setRecordNextTime(int $record_next_time): void
{
$this->record_next_time = $record_next_time;
}
}

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
namespace ZM\Annotation\Framework;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

View File

@@ -1,46 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Http;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class RequestMethod
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class RequestMethod extends AnnotationBase
{
public const GET = 'GET';
public const POST = 'POST';
public const PUT = 'PUT';
public const PATCH = 'PATCH';
public const DELETE = 'DELETE';
public const OPTIONS = 'OPTIONS';
public const HEAD = 'HEAD';
/**
* @var string
* @Required()
*/
public $method = self::GET;
public function __construct($method)
{
$this->method = $method;
}
}

View File

@@ -17,7 +17,7 @@ use ZM\Annotation\AnnotationBase;
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class RequestMapping extends AnnotationBase
class Route extends AnnotationBase
{
/**
* @var string
@@ -33,7 +33,7 @@ class RequestMapping extends AnnotationBase
/**
* @var array
*/
public $request_method = [RequestMethod::GET, RequestMethod::POST];
public $request_method = ['GET', 'POST'];
/**
* Routing path params binding. eg. {"id"="\d+"}
@@ -41,7 +41,7 @@ class RequestMapping extends AnnotationBase
*/
public $params = [];
public function __construct($route, $name = '', $request_method = [RequestMethod::GET, RequestMethod::POST], $params = [])
public function __construct($route, $name = '', $request_method = ['GET', 'POST'], $params = [])
{
$this->route = $route;
$this->name = $name;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Http;
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Http;
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Http;
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Http;
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
@@ -24,16 +24,16 @@ class Middleware extends AnnotationBase implements ErgodicAnnotation
* @var string
* @Required()
*/
public $middleware;
public $name;
/**
* @var string[]
*/
public $params = [];
public function __construct($middleware, $params = [])
public function __construct($name, $params = [])
{
$this->middleware = $middleware;
$this->name = $name;
$this->params = $params;
}
}

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace ZM\Annotation\Http;
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\OneBot;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* @Annotation
* @Target("METHOD")
* @NamedArgumentConstructor
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class OnOneBotEvent extends AnnotationBase
{
/** @var null|string */
public $type;
/** @var null|string */
public $detail_type;
/** @var null|string */
public $impl;
/** @var null|string */
public $platform;
/** @var null|string */
public $self_id;
/** @var null|string */
public $sub_type;
public function __construct(
?string $type = null,
?string $detail_type = null,
?string $impl = null,
?string $platform = null,
?string $self_id = null,
?string $sub_type = null
) {
$this->type = $type;
$this->detail_type = $detail_type;
$this->impl = $impl;
$this->platform = $platform;
$this->self_id = $self_id;
$this->sub_type = $sub_type;
}
}

View File

@@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
* Class OnCloseEvent
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnCloseEvent extends OnSwooleEventBase
{
/**
* @var string
*/
public $connect_type = 'default';
public function __construct($connect_type = 'default', $rule = '', $level = 20)
{
$this->connect_type = $connect_type;
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,25 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
* @since 2.7.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnManagerStartEvent extends OnSwooleEventBase
{
public function __construct($rule = '', $level = 20)
{
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @Target("METHOD")
* @NamedArgumentConstructor()
* Class OnMessageEvent
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnMessageEvent extends OnSwooleEventBase
{
/**
* @var string
*/
public $connect_type = 'default';
public function __construct($connect_type = 'default', $rule = '', $level = 20)
{
$this->connect_type = $connect_type;
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
* Class OnOpenEvent
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnOpenEvent extends OnSwooleEventBase
{
/**
* @var string
*/
public $connect_type = 'default';
public function __construct($connect_type = 'default', $rule = '', $level = 20)
{
$this->connect_type = $connect_type;
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class OnPipeMessageEvent
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnPipeMessageEvent extends AnnotationBase
{
/**
* @var string
* @Required()
*/
public $action;
public function __construct($action)
{
$this->action = $action;
}
}

View File

@@ -1,25 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
* Class OnRequestEvent
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnRequestEvent extends OnSwooleEventBase
{
public function __construct($rule = '', $level = 20)
{
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class OnSave
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnSave extends AnnotationBase
{
}

View File

@@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class OnWorkerStart
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnStart extends AnnotationBase
{
/**
* @var int
*/
public $worker_id = 0;
public function __construct($worker_id = 0)
{
$this->worker_id = $worker_id;
}
}

View File

@@ -1,43 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* Class OnSwooleEvent
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnSwooleEvent extends OnSwooleEventBase
{
/**
* @var string
* @Required
*/
public $type;
public function __construct($type, $rule = '', $level = 20)
{
$this->type = $type;
$this->rule = $rule;
$this->level = $level;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type)
{
$this->type = $type;
}
}

View File

@@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
use ZM\Annotation\Interfaces\Rule;
abstract class OnSwooleEventBase extends AnnotationBase implements Level, Rule
{
/**
* @var string
*/
public $rule = '';
/**
* @var int
*/
public $level = 20;
public function getRule()
{
return $this->rule !== '' ? $this->rule : true;
}
public function setRule(string $rule)
{
$this->rule = $rule;
}
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
}

View File

@@ -1,47 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Rule;
/**
* Class OnTask
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnTask extends AnnotationBase implements Rule
{
/**
* @var string
* @Required()
*/
public $task_name;
/**
* @var string
*/
public $rule = '';
public function __construct($task_name, $rule = '')
{
$this->task_name = $task_name;
$this->rule = $rule;
}
/**
* @return string 返回规则语句
*/
public function getRule(): string
{
return $this->rule;
}
}

View File

@@ -1,25 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* Class OnTaskEvent
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class OnTaskEvent extends OnSwooleEventBase
{
public function __construct($rule = '', $level = 20)
{
$this->rule = $rule;
$this->level = $level;
}
}

View File

@@ -1,39 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class OnTick
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
* @since 1.2
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class OnTick extends AnnotationBase
{
/**
* @var int
* @Required()
*/
public $tick_ms;
/**
* @var int
*/
public $worker_id = 0;
public function __construct($tick_ms, $worker_id = 0)
{
$this->tick_ms = $tick_ms;
$this->worker_id = $worker_id;
}
}

View File

@@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Swoole;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class SwooleHandler
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::TARGET_ALL)]
class SwooleHandler extends AnnotationBase
{
/**
* @var string
* @Required()
*/
public $event;
/** @var string */
public $params = '';
public function __construct($event, $params = '')
{
$this->event = $event;
$this->params = $params;
}
}