diff --git a/composer.json b/composer.json index 8dfac7a1..86962ebd 100644 --- a/composer.json +++ b/composer.json @@ -36,18 +36,20 @@ "ext-json": "*", "ext-posix": "*", "doctrine/annotations": "~1.12 || ~1.4.0", - "symfony/polyfill-ctype": "^1.19", - "symfony/polyfill-mbstring": "^1.19", - "symfony/console": "~5.0 || ~4.0 || ~3.0", - "symfony/routing": "~5.0 || ~4.0 || ~3.0", - "zhamao/console": "^1.0", - "zhamao/config": "^1.0", - "zhamao/request": "^1.1", - "zhamao/connection-manager": "^1.0", + "doctrine/dbal": "^2.13.1", "jelix/version": "^2.0", + "koriym/attributes": "^1.0", "league/climate": "^3.6", "psy/psysh": "@stable", - "doctrine/dbal": "^2.13.1" + "symfony/console": "~5.0 || ~4.0 || ~3.0", + "symfony/polyfill-ctype": "^1.19", + "symfony/polyfill-mbstring": "^1.19", + "symfony/polyfill-php80": "^1.16", + "symfony/routing": "~5.0 || ~4.0 || ~3.0", + "zhamao/config": "^1.0", + "zhamao/connection-manager": "^1.0", + "zhamao/console": "^1.0", + "zhamao/request": "^1.1" }, "suggest": { "ext-ctype": "Use C/C++ extension instead of polyfill will be more efficient", @@ -88,4 +90,4 @@ "analyse": "phpstan analyse --memory-limit 300M -l 0 ./src", "cs-fix": "php-cs-fixer fix $1" } -} \ No newline at end of file +} diff --git a/src/Custom/Annotation/Example.php b/src/Custom/Annotation/Example.php index c95908ba..0de28bac 100644 --- a/src/Custom/Annotation/Example.php +++ b/src/Custom/Annotation/Example.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Custom\Annotation; +use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; use ZM\Annotation\Interfaces\CustomAnnotation; @@ -11,10 +13,17 @@ use ZM\Annotation\Interfaces\CustomAnnotation; /** * Class CustomAnnotation * @Annotation + * @NamedArgumentConstructor() * @Target("ALL") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] class Example extends AnnotationBase implements CustomAnnotation { /** @var string */ public $str = ''; + + public function __construct($str = '') + { + $this->str = $str; + } } diff --git a/src/ZM/Annotation/AnnotationBase.php b/src/ZM/Annotation/AnnotationBase.php index ba5b4a89..51bc966d 100644 --- a/src/ZM/Annotation/AnnotationBase.php +++ b/src/ZM/Annotation/AnnotationBase.php @@ -8,9 +8,9 @@ use Closure; abstract class AnnotationBase { - public $method; + public $method = ''; - public $class; + public $class = ''; public function __toString() { diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index 27a9c42f..73a41ce2 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace ZM\Annotation; use Doctrine\Common\Annotations\AnnotationReader; +use Koriym\Attributes\AttributeReader; +use Koriym\Attributes\DualReader; use ReflectionClass; use ReflectionException; use ReflectionMethod; @@ -63,13 +65,12 @@ class AnnotationParser foreach ($this->path_list as $path) { Console::debug('parsing annotation in ' . $path[0] . ':' . $path[1]); $all_class = ZMUtil::getClassesPsr4($path[0], $path[1]); - $this->reader = new AnnotationReader(); + $this->reader = new DualReader(new AnnotationReader(), new AttributeReader()); foreach ($all_class as $v) { Console::debug('正在检索 ' . $v); $reflection_class = new ReflectionClass($v); $methods = $reflection_class->getMethods(ReflectionMethod::IS_PUBLIC); $class_annotations = $this->reader->getClassAnnotations($reflection_class); - // 这段为新加的:start //这里将每个类里面所有的类注解、方法注解通通加到一颗大树上,后期解析 /* @@ -133,8 +134,7 @@ class AnnotationParser if (!isset($inserted[$v][$method_name])) { // 在这里在其他中间件前插入插入全局的中间件 foreach ($middlewares as $middleware) { - $mid_class = new Middleware(); - $mid_class->middleware = $middleware; + $mid_class = new Middleware($middleware); $mid_class->class = $v; $mid_class->method = $method_name; $this->middleware_map[$v][$method_name][] = $mid_class; diff --git a/src/ZM/Annotation/CQ/CQAPIResponse.php b/src/ZM/Annotation/CQ/CQAPIResponse.php index 45d4b89a..2afe201f 100644 --- a/src/ZM/Annotation/CQ/CQAPIResponse.php +++ b/src/ZM/Annotation/CQ/CQAPIResponse.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\AnnotationBase; /** * Class CQAPIResponse * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class CQAPIResponse extends AnnotationBase { /** @@ -20,4 +24,9 @@ class CQAPIResponse extends AnnotationBase * @Required() */ public $retcode; + + public function __construct($retcode) + { + $this->retcode = $retcode; + } } diff --git a/src/ZM/Annotation/CQ/CQAfter.php b/src/ZM/Annotation/CQ/CQAfter.php index 7e80f751..0822564d 100644 --- a/src/ZM/Annotation/CQ/CQAfter.php +++ b/src/ZM/Annotation/CQ/CQAfter.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\Interfaces\Level; /** * Class CQAfter * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class CQAfter extends AnnotationBase implements Level { /** @@ -23,6 +27,12 @@ class CQAfter extends AnnotationBase implements Level public $level = 20; + public function __construct($cq_event, $level = 20) + { + $this->cq_event = $cq_event; + $this->level = $level; + } + /** * @return mixed */ diff --git a/src/ZM/Annotation/CQ/CQBefore.php b/src/ZM/Annotation/CQ/CQBefore.php index 3fa93f72..a56b40c6 100644 --- a/src/ZM/Annotation/CQ/CQBefore.php +++ b/src/ZM/Annotation/CQ/CQBefore.php @@ -4,6 +4,8 @@ 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; @@ -12,8 +14,10 @@ use ZM\Annotation\Interfaces\Level; /** * Class CQBefore * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class CQBefore extends AnnotationBase implements Level { /** @@ -24,6 +28,12 @@ class CQBefore extends AnnotationBase implements Level public $level = 20; + public function __construct($cq_event, $level = 20) + { + $this->cq_event = $cq_event; + $this->level = $level; + } + /** * @return mixed */ diff --git a/src/ZM/Annotation/CQ/CQCommand.php b/src/ZM/Annotation/CQ/CQCommand.php index 68bbb7d6..bb8f62bb 100644 --- a/src/ZM/Annotation/CQ/CQCommand.php +++ b/src/ZM/Annotation/CQ/CQCommand.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ 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 */ @@ -51,6 +55,22 @@ class CQCommand extends AnnotationBase implements Level /** @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; diff --git a/src/ZM/Annotation/CQ/CQMessage.php b/src/ZM/Annotation/CQ/CQMessage.php index cc847750..9f071ad7 100644 --- a/src/ZM/Annotation/CQ/CQMessage.php +++ b/src/ZM/Annotation/CQ/CQMessage.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\Interfaces\Level; /** * Class CQMessage * @Annotation + * @NamedArgumentConstructor() * @Target("ALL") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] class CQMessage extends AnnotationBase implements Level { /** @@ -38,6 +42,17 @@ class CQMessage extends AnnotationBase implements Level /** @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; diff --git a/src/ZM/Annotation/CQ/CQMetaEvent.php b/src/ZM/Annotation/CQ/CQMetaEvent.php index 7cb91a22..f6b7b6b6 100644 --- a/src/ZM/Annotation/CQ/CQMetaEvent.php +++ b/src/ZM/Annotation/CQ/CQMetaEvent.php @@ -4,6 +4,8 @@ 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; @@ -12,18 +14,26 @@ 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 = ''; + public $meta_event_type; /** @var int */ - public $level; + public $level = 20; + + public function __construct($meta_event_type, $level = 20) + { + $this->meta_event_type = $meta_event_type; + $this->level = $level; + } /** * @return mixed diff --git a/src/ZM/Annotation/CQ/CQNotice.php b/src/ZM/Annotation/CQ/CQNotice.php index 481bde05..511e4723 100644 --- a/src/ZM/Annotation/CQ/CQNotice.php +++ b/src/ZM/Annotation/CQ/CQNotice.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ 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 */ @@ -30,6 +34,15 @@ class CQNotice extends AnnotationBase implements Level /** @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; diff --git a/src/ZM/Annotation/CQ/CQRequest.php b/src/ZM/Annotation/CQ/CQRequest.php index a58eec76..a09069d0 100644 --- a/src/ZM/Annotation/CQ/CQRequest.php +++ b/src/ZM/Annotation/CQ/CQRequest.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ 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 */ @@ -30,6 +34,15 @@ class CQRequest extends AnnotationBase implements Level /** @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; diff --git a/src/ZM/Annotation/Command/TerminalCommand.php b/src/ZM/Annotation/Command/TerminalCommand.php index 1497231b..be099e1d 100644 --- a/src/ZM/Annotation/Command/TerminalCommand.php +++ b/src/ZM/Annotation/Command/TerminalCommand.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\AnnotationBase; /** * Class TerminalCommand * @Annotation + * @NamedArgumentConstructor * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class TerminalCommand extends AnnotationBase { /** @@ -27,4 +31,11 @@ class TerminalCommand extends AnnotationBase * @var string */ public $description = ''; + + public function __construct($command, $alias = '', $description = '') + { + $this->command = $command; + $this->alias = $alias; + $this->description = $description; + } } diff --git a/src/ZM/Annotation/Http/Controller.php b/src/ZM/Annotation/Http/Controller.php index 55c5452b..0b3db4b4 100644 --- a/src/ZM/Annotation/Http/Controller.php +++ b/src/ZM/Annotation/Http/Controller.php @@ -4,6 +4,8 @@ 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; @@ -12,8 +14,10 @@ use ZM\Annotation\Interfaces\ErgodicAnnotation; /** * Class Controller * @Annotation + * @NamedArgumentConstructor() * @Target("CLASS") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)] class Controller extends AnnotationBase implements ErgodicAnnotation { /** @@ -21,4 +25,9 @@ class Controller extends AnnotationBase implements ErgodicAnnotation * @Required() */ public $prefix = ''; + + public function __construct(string $prefix) + { + $this->prefix = $prefix; + } } diff --git a/src/ZM/Annotation/Http/HandleAfter.php b/src/ZM/Annotation/Http/HandleAfter.php index 03a2d4c5..a7c97e4a 100644 --- a/src/ZM/Annotation/Http/HandleAfter.php +++ b/src/ZM/Annotation/Http/HandleAfter.php @@ -4,14 +4,18 @@ declare(strict_types=1); namespace ZM\Annotation\Http; +use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; /** * Class HandleAfter * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class HandleAfter extends AnnotationBase { } diff --git a/src/ZM/Annotation/Http/HandleBefore.php b/src/ZM/Annotation/Http/HandleBefore.php index 562978d9..f46c30d7 100644 --- a/src/ZM/Annotation/Http/HandleBefore.php +++ b/src/ZM/Annotation/Http/HandleBefore.php @@ -4,14 +4,18 @@ declare(strict_types=1); namespace ZM\Annotation\Http; +use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; /** * Class HandleBefore * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class HandleBefore extends AnnotationBase { } diff --git a/src/ZM/Annotation/Http/HandleException.php b/src/ZM/Annotation/Http/HandleException.php index e93231f3..46f45d83 100644 --- a/src/ZM/Annotation/Http/HandleException.php +++ b/src/ZM/Annotation/Http/HandleException.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace ZM\Annotation\Http; +use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Target; use Exception; use ZM\Annotation\AnnotationBase; @@ -11,12 +13,19 @@ use ZM\Annotation\AnnotationBase; /** * Class HandleException * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class HandleException extends AnnotationBase { /** * @var string */ public $class_name = Exception::class; + + public function __construct($class_name = Exception::class) + { + $this->class_name = $class_name; + } } diff --git a/src/ZM/Annotation/Http/Middleware.php b/src/ZM/Annotation/Http/Middleware.php index f13311c6..f16d914a 100644 --- a/src/ZM/Annotation/Http/Middleware.php +++ b/src/ZM/Annotation/Http/Middleware.php @@ -4,6 +4,8 @@ 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; @@ -12,8 +14,10 @@ use ZM\Annotation\Interfaces\ErgodicAnnotation; /** * Class Middleware * @Annotation + * @NamedArgumentConstructor() * @Target("ALL") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] class Middleware extends AnnotationBase implements ErgodicAnnotation { /** @@ -26,4 +30,10 @@ class Middleware extends AnnotationBase implements ErgodicAnnotation * @var string[] */ public $params = []; + + public function __construct($middleware, $params = []) + { + $this->middleware = $middleware; + $this->params = $params; + } } diff --git a/src/ZM/Annotation/Http/MiddlewareClass.php b/src/ZM/Annotation/Http/MiddlewareClass.php index a3e3713d..2815c70c 100644 --- a/src/ZM/Annotation/Http/MiddlewareClass.php +++ b/src/ZM/Annotation/Http/MiddlewareClass.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\AnnotationBase; /** * Class MiddlewareClass * @Annotation + * @NamedArgumentConstructor() * @Target("CLASS") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)] class MiddlewareClass extends AnnotationBase { /** @@ -20,4 +24,9 @@ class MiddlewareClass extends AnnotationBase * @Required() */ public $name = ''; + + public function __construct($name) + { + $this->name = $name; + } } diff --git a/src/ZM/Annotation/Http/RequestMapping.php b/src/ZM/Annotation/Http/RequestMapping.php index dc545660..4b9c49a2 100644 --- a/src/ZM/Annotation/Http/RequestMapping.php +++ b/src/ZM/Annotation/Http/RequestMapping.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\AnnotationBase; /** * Class RequestMapping * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class RequestMapping extends AnnotationBase { /** @@ -36,4 +40,12 @@ class RequestMapping extends AnnotationBase * @var array */ public $params = []; + + public function __construct($route, $name = '', $request_method = [RequestMethod::GET, RequestMethod::POST], $params = []) + { + $this->route = $route; + $this->name = $name; + $this->request_method = $request_method; + $this->params = $params; + } } diff --git a/src/ZM/Annotation/Http/RequestMethod.php b/src/ZM/Annotation/Http/RequestMethod.php index be3b09a5..68014c9d 100644 --- a/src/ZM/Annotation/Http/RequestMethod.php +++ b/src/ZM/Annotation/Http/RequestMethod.php @@ -4,13 +4,19 @@ 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'; @@ -32,4 +38,9 @@ class RequestMethod extends AnnotationBase * @Required() */ public $method = self::GET; + + public function __construct($method) + { + $this->method = $method; + } } diff --git a/src/ZM/Annotation/Module/Closed.php b/src/ZM/Annotation/Module/Closed.php index 9bd46fda..04efffdb 100644 --- a/src/ZM/Annotation/Module/Closed.php +++ b/src/ZM/Annotation/Module/Closed.php @@ -4,14 +4,18 @@ declare(strict_types=1); namespace ZM\Annotation\Module; +use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; /** * Class Closed * @Annotation + * @NamedArgumentConstructor() * @Target("CLASS") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)] class Closed extends AnnotationBase { } diff --git a/src/ZM/Annotation/Swoole/OnCloseEvent.php b/src/ZM/Annotation/Swoole/OnCloseEvent.php index f2d4398c..37886ff4 100644 --- a/src/ZM/Annotation/Swoole/OnCloseEvent.php +++ b/src/ZM/Annotation/Swoole/OnCloseEvent.php @@ -4,17 +4,28 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnManagerStartEvent.php b/src/ZM/Annotation/Swoole/OnManagerStartEvent.php index 303b15c8..9a291397 100644 --- a/src/ZM/Annotation/Swoole/OnManagerStartEvent.php +++ b/src/ZM/Annotation/Swoole/OnManagerStartEvent.php @@ -4,13 +4,22 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnMessageEvent.php b/src/ZM/Annotation/Swoole/OnMessageEvent.php index 2816bb5f..1d0daf51 100644 --- a/src/ZM/Annotation/Swoole/OnMessageEvent.php +++ b/src/ZM/Annotation/Swoole/OnMessageEvent.php @@ -4,17 +4,28 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnOpenEvent.php b/src/ZM/Annotation/Swoole/OnOpenEvent.php index 07655793..9f414ac6 100644 --- a/src/ZM/Annotation/Swoole/OnOpenEvent.php +++ b/src/ZM/Annotation/Swoole/OnOpenEvent.php @@ -4,17 +4,28 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnPipeMessageEvent.php b/src/ZM/Annotation/Swoole/OnPipeMessageEvent.php index 8def50ae..32a206b7 100644 --- a/src/ZM/Annotation/Swoole/OnPipeMessageEvent.php +++ b/src/ZM/Annotation/Swoole/OnPipeMessageEvent.php @@ -4,6 +4,8 @@ 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; @@ -11,8 +13,10 @@ use ZM\Annotation\AnnotationBase; /** * Class OnPipeMessageEvent * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class OnPipeMessageEvent extends AnnotationBase { /** @@ -20,4 +24,9 @@ class OnPipeMessageEvent extends AnnotationBase * @Required() */ public $action; + + public function __construct($action) + { + $this->action = $action; + } } diff --git a/src/ZM/Annotation/Swoole/OnRequestEvent.php b/src/ZM/Annotation/Swoole/OnRequestEvent.php index 324ba678..f5425e93 100644 --- a/src/ZM/Annotation/Swoole/OnRequestEvent.php +++ b/src/ZM/Annotation/Swoole/OnRequestEvent.php @@ -4,13 +4,22 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnSave.php b/src/ZM/Annotation/Swoole/OnSave.php index 480cda5c..43cccd48 100644 --- a/src/ZM/Annotation/Swoole/OnSave.php +++ b/src/ZM/Annotation/Swoole/OnSave.php @@ -4,14 +4,18 @@ 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 { } diff --git a/src/ZM/Annotation/Swoole/OnSetup.php b/src/ZM/Annotation/Swoole/OnSetup.php index a56fec94..ee22f561 100644 --- a/src/ZM/Annotation/Swoole/OnSetup.php +++ b/src/ZM/Annotation/Swoole/OnSetup.php @@ -4,14 +4,18 @@ 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 ZMSetup * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class OnSetup extends AnnotationBase { } diff --git a/src/ZM/Annotation/Swoole/OnStart.php b/src/ZM/Annotation/Swoole/OnStart.php index e0728828..f9528919 100644 --- a/src/ZM/Annotation/Swoole/OnStart.php +++ b/src/ZM/Annotation/Swoole/OnStart.php @@ -4,18 +4,27 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnSwooleEvent.php b/src/ZM/Annotation/Swoole/OnSwooleEvent.php index 395fcdcf..5193cb1b 100644 --- a/src/ZM/Annotation/Swoole/OnSwooleEvent.php +++ b/src/ZM/Annotation/Swoole/OnSwooleEvent.php @@ -4,14 +4,18 @@ 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 { /** @@ -20,6 +24,13 @@ class OnSwooleEvent extends OnSwooleEventBase */ 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; diff --git a/src/ZM/Annotation/Swoole/OnTask.php b/src/ZM/Annotation/Swoole/OnTask.php index 4f45c7ca..b0d8c361 100644 --- a/src/ZM/Annotation/Swoole/OnTask.php +++ b/src/ZM/Annotation/Swoole/OnTask.php @@ -4,6 +4,8 @@ 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; @@ -12,8 +14,10 @@ use ZM\Annotation\Interfaces\Rule; /** * Class OnTask * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") */ +#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)] class OnTask extends AnnotationBase implements Rule { /** @@ -27,6 +31,12 @@ class OnTask extends AnnotationBase implements Rule */ public $rule = ''; + public function __construct($task_name, $rule = '') + { + $this->task_name = $task_name; + $this->rule = $rule; + } + /** * @return mixed */ diff --git a/src/ZM/Annotation/Swoole/OnTaskEvent.php b/src/ZM/Annotation/Swoole/OnTaskEvent.php index 4194051e..512f4422 100644 --- a/src/ZM/Annotation/Swoole/OnTaskEvent.php +++ b/src/ZM/Annotation/Swoole/OnTaskEvent.php @@ -4,13 +4,22 @@ 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; + } } diff --git a/src/ZM/Annotation/Swoole/OnTick.php b/src/ZM/Annotation/Swoole/OnTick.php index d94534ff..26965dca 100644 --- a/src/ZM/Annotation/Swoole/OnTick.php +++ b/src/ZM/Annotation/Swoole/OnTick.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ZM\Annotation\Swoole; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Required; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; @@ -11,6 +12,7 @@ use ZM\Annotation\AnnotationBase; /** * Class OnTick * @Annotation + * @NamedArgumentConstructor() * @Target("METHOD") * @since 1.2 */ @@ -26,4 +28,10 @@ class OnTick extends AnnotationBase * @var int */ public $worker_id = 0; + + public function __construct($tick_ms, $worker_id = 0) + { + $this->tick_ms = $tick_ms; + $this->worker_id = $worker_id; + } } diff --git a/src/ZM/Annotation/Swoole/SwooleHandler.php b/src/ZM/Annotation/Swoole/SwooleHandler.php index 25f9da1f..4e5c6bc5 100644 --- a/src/ZM/Annotation/Swoole/SwooleHandler.php +++ b/src/ZM/Annotation/Swoole/SwooleHandler.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ZM\Annotation\Swoole; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Doctrine\Common\Annotations\Annotation\Required; use Doctrine\Common\Annotations\Annotation\Target; use ZM\Annotation\AnnotationBase; @@ -11,6 +12,7 @@ use ZM\Annotation\AnnotationBase; /** * Class SwooleHandler * @Annotation + * @NamedArgumentConstructor() * @Target("ALL") */ class SwooleHandler extends AnnotationBase @@ -20,4 +22,13 @@ class SwooleHandler extends AnnotationBase * @Required() */ public $event; + + /** @var string */ + public $params = ''; + + public function __construct($event, $params = '') + { + $this->event = $event; + $this->params = $params; + } } diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 8fe0346f..7c801783 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -28,9 +28,9 @@ use ZM\Exception\InitException; class ConsoleApplication extends Application { - public const VERSION_ID = 438; + public const VERSION_ID = 439; - public const VERSION = '2.7.0-beta3'; + public const VERSION = '2.7.0-beta4'; private static $obj; diff --git a/src/ZM/Event/EventTracer.php b/src/ZM/Event/EventTracer.php index 1ca84d70..7c277c9d 100644 --- a/src/ZM/Event/EventTracer.php +++ b/src/ZM/Event/EventTracer.php @@ -30,7 +30,10 @@ class EventTracer public static function getCurrentEventMiddlewares() { $current_event = self::getCurrentEvent(); - if (!isset($current_event->class, $current_event->method)) { + if (empty($current_event->class)) { + return null; + } + if (empty($current_event->method)) { return null; } return EventManager::$middleware_map[$current_event->class][$current_event->method] ?? []; diff --git a/src/ZM/Event/SwooleEvent/OnRequest.php b/src/ZM/Event/SwooleEvent/OnRequest.php index 07536ee2..8290d531 100644 --- a/src/ZM/Event/SwooleEvent/OnRequest.php +++ b/src/ZM/Event/SwooleEvent/OnRequest.php @@ -39,7 +39,7 @@ class OnRequest implements SwooleEvent $dis1 = new EventDispatcher(OnRequestEvent::class); $dis1->setRuleFunction(function ($v) { - return eval('return ' . $v->getRule() . ';') ? true : false; + return (bool) eval('return ' . $v->getRule() . ';'); }); $dis = new EventDispatcher(OnSwooleEvent::class); @@ -61,8 +61,7 @@ class OnRequest implements SwooleEvent if ($result === true) { ctx()->setCache('params', $params); $dispatcher = new EventDispatcher(RequestMapping::class); - $div = new RequestMapping(); - $div->route = $node['route']; + $div = new RequestMapping($node['route']); $div->params = $params; $div->method = $node['method']; $div->request_method = $node['request_method']; diff --git a/src/ZM/Exception/InvalidArgumentException.php b/src/ZM/Exception/InvalidArgumentException.php index cb2f8190..76c95a0b 100644 --- a/src/ZM/Exception/InvalidArgumentException.php +++ b/src/ZM/Exception/InvalidArgumentException.php @@ -4,6 +4,12 @@ declare(strict_types=1); namespace ZM\Exception; +use Throwable; + class InvalidArgumentException extends ZMException { + public function __construct($message = '', $code = 0, Throwable $previous = null) + { + parent::__construct(zm_internal_errcode('E00074') . $message, $code, $previous); + } } diff --git a/src/ZM/Http/Response.php b/src/ZM/Http/Response.php index 28ee8887..20bdb1b0 100644 --- a/src/ZM/Http/Response.php +++ b/src/ZM/Http/Response.php @@ -76,6 +76,7 @@ class Response /** * @param mixed ...$params + * @param mixed $name * @return mixed */ public function rawcookie($name, ...$params) diff --git a/test/ZMTest/PassedTest/AnnotationParserRegisterTest.php b/test/ZMTest/PassedTest/AnnotationParserRegisterTest.php index 684881e0..46f4b762 100644 --- a/test/ZMTest/PassedTest/AnnotationParserRegisterTest.php +++ b/test/ZMTest/PassedTest/AnnotationParserRegisterTest.php @@ -35,6 +35,7 @@ class AnnotationParserRegisterTest extends TestCase public function testAnnotation() { ob_start(); $gen = $this->parser->generateAnnotationEvents(); + //zm_dump($gen); $m = $gen[OnStart::class][0]->method; $class = $gen[OnStart::class][0]->class; $c = new $class(); @@ -43,12 +44,10 @@ class AnnotationParserRegisterTest extends TestCase } catch (Exception $e) { } $result = ob_get_clean(); - echo $result; $this->assertStringContainsString("我开始了!", $result); } public function testAnnotation2() { - foreach ($this->parser->generateAnnotationEvents() as $k => $v) { foreach ($v as $vs) { $this->assertTrue($vs->method === null || $vs->method != ''); @@ -57,22 +56,9 @@ class AnnotationParserRegisterTest extends TestCase } } - public function testAnnotationMap() { - $map = $this->parser->getMiddlewareMap(); - $this->assertContainsEquals("timer", $map[Hello::class]["timer"]); - } - public function testMiddlewares() { $wares = $this->parser->getMiddlewares(); + zm_dump($wares); $this->assertArrayHasKey("timer", $wares); } - - public function testReqMapping() { - $mapping = $this->parser->getReqMapping(); - $this->assertEquals("index", $mapping["method"]); - } - - public function testTracer() { - - } }