This commit is contained in:
2021-02-22 17:44:24 +08:00
parent 3b09b9a308
commit 2d1f83cf09
34 changed files with 732 additions and 222 deletions
+90 -32
View File
@@ -4,13 +4,12 @@
namespace Annotation;
use Annotation\Model\Get;
use Database\ActiveRecord;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\NotFindPropertyException;
use Snowflake\Snowflake;
/**
@@ -29,17 +28,40 @@ class Annotation extends Component
private array $_targets = [];
private array $_methods = [];
/**
* @param array $handler
* @param $name
*/
public function addMethodAttribute(array $handler, $name)
{
$this->_methods[get_class($handler[0])][$name] = $handler;
}
/**
* @param string $className
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className): array
{
return $this->_methods[$className] ?? [];
}
/**
* @param string $path
* @param string $namespace
* @param string $alias
* @return $this
* @throws ReflectionException
* @throws ReflectionException|NotFindPropertyException
*/
public function readControllers(string $path, string $namespace, string $alias = 'root'): static
{
$this->scanDir(glob($path . '*'), $namespace, $alias);
return $this;
return $this->scanDir(glob($path . '*'), $namespace, $alias);
}
@@ -56,25 +78,12 @@ class Annotation extends Component
}
/**
* @param string $target
* @return mixed
*/
public function getTarget(string $target): mixed
{
if (!isset($this->_targets[$target])) {
return [];
}
return $this->_targets[$target];
}
/**
* @param array $paths
* @param string $namespace
* @param string $alias
* @return $this
* @throws ReflectionException
* @throws ReflectionException|NotFindPropertyException
*/
private function scanDir(array $paths, string $namespace, string $alias): static
{
@@ -104,26 +113,74 @@ class Annotation extends Component
* @param string $alias
* @return array
* @throws ReflectionException
* @throws NotFindPropertyException
*/
private function getReflect(string $class, string $alias): array
{
$reflect = $this->reflectClass($class);
if ($reflect->isInstantiable()) {
$object = $reflect->newInstance();
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class != $class) {
continue;
}
if (!$reflect->isInstantiable()) {
return $this->targets($reflect);
}
$object = $reflect->newInstance();
$this->resolveMethod($reflect, $class, $alias, $object);
return $this->targets($reflect);
}
$tmp = $this->resolveAnnotations($method, $alias, $object);
if (empty($tmp)) {
continue;
}
$this->_classes[$reflect->getName()][$method->getName()] = $tmp;
/**
* @param ReflectionClass $reflect
* @param $class
* @param $alias
* @param $object
* @throws NotFindPropertyException
*/
private function resolveMethod(ReflectionClass $reflect, $class, $alias, $object)
{
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class != $class) {
continue;
}
$tmp = $this->resolveAnnotations($method, $alias, $object);
if (empty($tmp)) {
continue;
}
$this->_classes[$reflect->getName()][$method->getName()] = $tmp;
}
$this->resolveProperty($reflect, $object);
}
/**
* @param ReflectionClass $reflectionClass
* @param $object
* @throws NotFindPropertyException
*/
private function resolveProperty(ReflectionClass $reflectionClass, $object)
{
$property = $reflectionClass->getProperties();
foreach ($property as $value) {
$attributes = $value->getAttributes();
if (count($attributes) < 1) {
continue;
}
foreach ($attributes as $attribute) {
/** @var IAnnotation $annotation */
$annotation = $attribute->newInstance()->execute([$object, $value->getName()]);
if ($value->isStatic()) {
$object::{$value->getName()} = $annotation;
} else if ($value->isPublic()) {
$object->{$value->getName()} = $annotation;
} else {
$name = 'set' . ucfirst($value->getName());
if (!method_exists($object, $name)) {
throw new NotFindPropertyException('set property need method ' . $name);
}
$object->$name($annotation);
}
}
}
return $this->targets($reflect);
}
@@ -153,11 +210,12 @@ class Annotation extends Component
$names = [];
foreach ($attributes as $attribute) {
/** @var IAnnotation $class */
$class = $this->instance($attribute);
if ($class === null) {
continue;
}
$names[$attribute->getName()] = $class;
$names[$attribute->getName()] = $class->execute([$object, $method->getName()]);
}
$tmp['handler'] = [$object, $method->getName()];
+16 -2
View File
@@ -4,6 +4,7 @@
namespace Annotation;
use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
@@ -12,7 +13,7 @@ use Snowflake\Snowflake;
* Class Event
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Event
#[\Attribute(\Attribute::TARGET_METHOD)] class Event implements IAnnotation
{
@@ -20,11 +21,24 @@ use Snowflake\Snowflake;
* Event constructor.
* @param string $name
* @param array $params
* @throws ComponentException
*/
public function __construct(public string $name, public array $params = [])
{
}
/**
* @param array $handler
* @return \Snowflake\Event
* @throws ComponentException
* @throws Exception
*/
public function execute(array $handler): \Snowflake\Event
{
// TODO: Implement execute() method.
$event = Snowflake::app()->getEvent();
$event->on($this->name, $handler, $this->params);
return $event;
}
}
+2 -3
View File
@@ -9,12 +9,11 @@ use Closure;
interface IAnnotation
{
/**
* @param array|Closure $handler
* @param array $handler
* @return mixed
*/
public function setHandler(array|Closure $handler): mixed;
public function execute(array $handler): mixed;
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Annotation;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Inject
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject implements IAnnotation
{
/**
* Inject constructor.
* @param string $className
* @param array $args
*/
public function __construct(private string $className, private array $args = [])
{
}
/**
* @param array $handler
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException|ComponentException
*/
public function execute(array $handler): mixed
{
if (Snowflake::app()->has($this->className)) {
return Snowflake::app()->get($this->className);
}
return Snowflake::createObject($this->className, $this->args);
}
}
+20 -1
View File
@@ -4,15 +4,19 @@
namespace Annotation\Model;
use Annotation\Annotation;
use Annotation\IAnnotation;
use Attribute;
use Database\ActiveRecord;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
/**
* Class Get
* @package Annotation\Model
*/
#[Attribute(Attribute::TARGET_METHOD)] class Get
#[Attribute(Attribute::TARGET_METHOD)] class Get implements IAnnotation
{
@@ -27,4 +31,19 @@ use Database\ActiveRecord;
}
/**
* @param array $handler
* @return Annotation
* @throws ComponentException
*/
public function execute(array $handler): Annotation
{
// TODO: Implement execute() method.
$annotation = Snowflake::app()->getAttributes();
$annotation->addMethodAttribute($handler, $this->name);
return $annotation;
}
}
+11 -1
View File
@@ -11,7 +11,7 @@ use validator\Validator;
* Class RequestValidator
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class RequestValidator
#[\Attribute(\Attribute::TARGET_METHOD)] class RequestValidator implements IAnnotation
{
/**
@@ -24,4 +24,14 @@ use validator\Validator;
}
/**
* @param array $handler
* @return bool
*/
public function execute(array $handler): bool
{
return true;
}
}
+21 -15
View File
@@ -4,13 +4,16 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class After
#[\Attribute(\Attribute::TARGET_METHOD)] class After implements IAnnotation
{
@@ -18,27 +21,30 @@ use Snowflake\Snowflake;
* Interceptor constructor.
* @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after
* @throws
* if ($object instanceof Interceptor) {
$classes[$key] = [$object, 'Interceptor'];
}
if ($object instanceof Limits) {
$classes[$key] = [$object, 'next'];
}
if ($object instanceof After) {
$classes[$key] = [$object, 'onHandler'];
}
if ($object instanceof Middleware) {
$classes[$key] = [$object, 'onHandler'];
}
*/
public function __construct(public string|array $after)
{
if (is_string($this->after)) {
$this->after = [$this->after];
if (!is_string($this->after)) {
return;
}
$this->after = [$this->after];
}
/**
* @param array $handler
* @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): array|string
{
// TODO: Implement execute() method.
foreach ($this->after as $key => $item) {
$this->after[$key] = [Snowflake::createObject($item), 'onHandler'];
}
return $this->after;
}
}
+14 -1
View File
@@ -4,11 +4,13 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
/**
* Class Document
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Document
#[\Attribute(\Attribute::TARGET_METHOD)] class Document implements IAnnotation
{
const INTEGER = 'int';
@@ -31,4 +33,15 @@ namespace Annotation\Route;
{
}
/**
* @param array $handler
* @return array
*/
public function execute(array $handler): array
{
// TODO: Implement execute() method.
return [$this->request, $this->response];
}
}
+20 -3
View File
@@ -4,13 +4,16 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor implements IAnnotation
{
@@ -21,12 +24,26 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $interceptor)
{
if (is_string($this->interceptor)) {
$this->interceptor = [$this->interceptor];
if (!is_string($this->interceptor)) {
return;
}
$this->interceptor = [$this->interceptor];
}
/**
* @param array $handler
* @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): array|string
{
// TODO: Implement execute() method.
foreach ($this->interceptor as $key => $item) {
$this->interceptor[$key] = [Snowflake::createObject($item), 'Interceptor'];
}
return $this->interceptor;
}
}
+20 -3
View File
@@ -4,13 +4,16 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Limits
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits implements IAnnotation
{
@@ -21,12 +24,26 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $limits)
{
if (is_string($this->limits)) {
$this->limits = [$this->limits];
if (!is_string($this->limits)) {
return;
}
$this->limits = [$this->limits];
}
/**
* @param array $handler
* @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): array|string
{
// TODO: Implement execute() method.
foreach ($this->limits as $key => $item) {
$this->limits[$key] = [Snowflake::createObject($item), 'next'];
}
return $this->limits;
}
+22 -3
View File
@@ -4,13 +4,17 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Middleware
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware implements IAnnotation
{
@@ -21,12 +25,27 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
if (!is_string($this->middleware)) {
return;
}
$this->middleware = [$this->middleware];
}
/**
* @param array $handler
* @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): array|string
{
// TODO: Implement execute() method.
foreach ($this->middleware as $key => $item) {
$this->middleware[$key] = [Snowflake::createObject($item), 'onHandler'];
}
return $this->middleware;
}
}
+8 -6
View File
@@ -7,6 +7,7 @@ namespace Annotation\Route;
use Closure;
use Exception;
use HttpServer\Route\Node;
use HttpServer\Route\Router;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
@@ -29,18 +30,19 @@ use Annotation\IAnnotation;
/**
* @param array|Closure $handler
* @return ?Node
* @param array $handler
* @return Router
* @throws ComponentException
* @throws ConfigException
* @throws Exception
*/
public function setHandler(array|Closure $handler): ?Node
public function execute(array $handler): Router
{
$router = Snowflake::app()->getRouter();
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
return $router->addRoute($this->uri, $handler, $this->method);
$router->addRoute($this->uri, $handler, $this->method);
return $router;
}
+8 -6
View File
@@ -8,6 +8,7 @@ use Annotation\IAnnotation;
use Closure;
use Exception;
use HttpServer\Route\Node;
use HttpServer\Route\Router;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
@@ -39,20 +40,21 @@ use Snowflake\Snowflake;
/**
* @param array|Closure $handler
* @return Node|null
* @param array $handler
* @return Router
* @throws ComponentException
* @throws ConfigException
* @throws Exception
*/
public function setHandler(array|Closure $handler): ?Node
public function execute(array $handler): Router
{
$router = Snowflake::app()->getRouter();
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
return $router->addRoute($method, $handler, 'sw::socket');
$router->addRoute($method, $handler, 'sw::socket');
return $router;
}
}