This commit is contained in:
2021-02-23 14:16:08 +08:00
parent 8de7e6cfbc
commit 9e6ac6d92d
8 changed files with 275 additions and 279 deletions
+256 -233
View File
@@ -21,275 +21,298 @@ use Snowflake\Snowflake;
class Annotation extends Component class Annotation extends Component
{ {
private array $_annotations = []; private array $_annotations = [];
private array $_classes = []; private array $_classes = [];
private array $_targets = []; private array $_targets = [];
private array $_methods = []; private array $_methods = [];
/** /**
* @param array $handler * @param array $handler
* @param $name * @param $name
*/ */
public function addMethodAttribute(array $handler, $name) public function addMethodAttribute(array $handler, $name)
{ {
$this->_methods[get_class($handler[0])][$name] = $handler; $this->_methods[get_class($handler[0])][$name] = $handler;
} }
/** /**
* @param string $className * @param string $className
* @return array 根据类名获取注解 * @return array 根据类名获取注解
* 根据类名获取注解 * 根据类名获取注解
*/ */
public function getMethods(string $className): array public function getMethods(string $className): array
{ {
return $this->_methods[$className] ?? []; return $this->_methods[$className] ?? [];
} }
/** /**
* @param string $path * @param string $path
* @param string $namespace * @param string $namespace
* @param string $alias * @param string $alias
* @return $this * @return $this
* @throws ReflectionException|NotFindPropertyException * @throws ReflectionException|NotFindPropertyException
*/ */
public function readControllers(string $path, string $namespace, string $alias = 'root'): static public function readControllers(string $path, string $namespace, string $alias = 'root'): static
{ {
return $this->scanDir(glob($path . '*'), $namespace, $alias); return $this->scanDir(glob($path . '*'), $namespace, $alias);
} }
/** /**
* @param string $alias * @param string $alias
* @return array * @return array
*/ */
public function getAlias(string $alias = 'root'): array public function getAlias(string $alias = 'root'): array
{ {
if (!isset($this->_annotations[$alias])) { if (!isset($this->_annotations[$alias])) {
return []; return [];
} }
return $this->_annotations[$alias]; return $this->_annotations[$alias];
} }
/** /**
* @param array $paths * @param array $paths
* @param string $namespace * @param string $namespace
* @param string $alias * @param string $alias
* @return $this * @return $this
* @throws ReflectionException|NotFindPropertyException * @throws ReflectionException|NotFindPropertyException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
private function scanDir(array $paths, string $namespace, string $alias): static private function scanDir(array $paths, string $namespace, string $alias): static
{ {
if (!isset($this->_annotations[$alias])) { if (!isset($this->_annotations[$alias])) {
$this->_annotations[$alias] = []; $this->_annotations[$alias] = [];
} }
foreach ($paths as $path) { foreach ($paths as $path) {
$explode = explode('/', $path); $explode = explode('/', $path);
$explode_pop = array_pop($explode); $explode_pop = array_pop($explode);
if (is_file($path)) { if (is_file($path)) {
if (!str_contains($path, '.php')) { if (!str_contains($path, '.php')) {
continue; continue;
} }
$explode_pop = str_replace('.php', '', $explode_pop); $explode_pop = str_replace('.php', '', $explode_pop);
$this->getReflect($namespace . '\\' . $explode_pop, $alias); $this->getReflect($namespace . '\\' . $explode_pop, $alias);
} else { } else {
$this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias); $this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
} }
} }
return $this; return $this;
} }
/** /**
* @param string $class * @param string $class
* @param string $alias * @param string $alias
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function getReflect(string $class, string $alias): array private function getReflect(string $class, string $alias): array
{ {
try { try {
$reflect = $this->reflectClass($class); $reflect = $this->reflectClass($class);
if (empty($reflect) || !$reflect->isInstantiable()) { if (empty($reflect) || !$reflect->isInstantiable()) {
return []; return [];
} }
$object = $reflect->newInstance(); $object = $reflect->newInstance();
$this->resolveMethod($reflect, $class, $alias, $object); $this->resolveMethod($reflect, $class, $alias, $object);
return $this->targets($reflect); return $this->targets($reflect);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$this->addError($throwable); $this->addError($throwable);
return []; return [];
} }
} }
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param $class * @param $class
* @param $alias * @param $alias
* @param $object * @param $object
* @throws NotFindPropertyException * @throws NotFindPropertyException
*/ */
private function resolveMethod(ReflectionClass $reflect, $class, $alias, $object) private function resolveMethod(ReflectionClass $reflect, $class, $alias, $object)
{ {
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class != $class) { if ($method->class != $class) {
continue; continue;
} }
$this->resolveAnnotations($method, $alias, $object); $this->resolveAnnotations($method, $alias, $object);
} }
$this->resolveProperty($reflect, $object); $this->resolveProperty($reflect, $object);
} }
/** /**
* @param ReflectionClass $reflectionClass * @param ReflectionClass $reflectionClass
* @param $object * @param $object
* @throws NotFindPropertyException * @throws NotFindPropertyException
*/ */
private function resolveProperty(ReflectionClass $reflectionClass, $object) private function resolveProperty(ReflectionClass $reflectionClass, $object)
{ {
$property = $reflectionClass->getProperties(); $property = $reflectionClass->getProperties();
foreach ($property as $value) { foreach ($property as $value) {
if ($value->isStatic()) continue; if ($value->isStatic()) continue;
$attributes = $value->getAttributes(); $attributes = $value->getAttributes();
if (count($attributes) < 1) { if (count($attributes) < 1) {
continue; continue;
} }
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
/** @var IAnnotation $annotation */ /** @var IAnnotation $annotation */
$annotation = $this->instance($attribute); $annotation = $this->instance($attribute);
if (empty($annotation)) { if (empty($annotation)) {
continue; continue;
} }
$annotation = $annotation->execute([$object, $value->getName()]); $annotation = $annotation->execute([$object, $value->getName()]);
if ($value->isPublic()) { if ($value->isPublic()) {
$object->{$value->getName()} = $annotation; $object->{$value->getName()} = $annotation;
} else { } else {
$name = 'set' . ucfirst($value->getName()); $name = 'set' . ucfirst($value->getName());
if (!method_exists($object, $name)) { if (!method_exists($object, $name)) {
throw new NotFindPropertyException('set property need method ' . $name); throw new NotFindPropertyException('set property need method ' . $name);
} }
$object->$name($annotation); $object->$name($annotation);
} }
} }
} }
} }
/** /**
* @param string $class * @param string $class
* @return ReflectionClass|null * @return ReflectionClass|null
* @throws ReflectionException * @throws ReflectionException
*/ */
private function reflectClass(string $class): ?ReflectionClass private function reflectClass(string $class): ?ReflectionClass
{ {
return Snowflake::getDi()->getReflect($class); return Snowflake::getDi()->getReflect($class);
} }
/** /**
* @param ReflectionMethod $method * @param ReflectionMethod $method
* @param $alias * @param $alias
* @param $object * @param $object
* @return array * @return array
*/ */
private function resolveAnnotations(ReflectionMethod $method, $alias, $object): array private function resolveAnnotations(ReflectionMethod $method, $alias, $object): array
{ {
$attributes = $method->getAttributes(); $attributes = $method->getAttributes();
if (count($attributes) < 1) { if (count($attributes) < 1) {
return []; return [];
} }
foreach ($attributes as $attribute) { $name = get_class($object) . '_' . $method->getName();
/** @var IAnnotation $class */ if (!isset($this->_annotations[$alias])) {
$class = $this->instance($attribute); $this->_annotations[$alias] = [];
if ($class === null) { }
continue; if (!isset($this->_annotations[$alias][$name])) {
} $this->_annotations[$alias][$name] = [];
$class->execute([$object, $method->getName()]); }
}
// $tmp['handler'] = [$object, $method->getName()]; $array = [];
// $tmp['attributes'] = $names; foreach ($attributes as $attribute) {
// /** @var IAnnotation $class */
// $this->_annotations[$alias][] = $tmp; $class = $this->instance($attribute);
if ($class === null) {
continue;
}
$array[] = $class->execute([$object, $method->getName()]);
}
return []; $this->_annotations[$alias][$name][] = $array;
} return [];
}
/** /**
* @param $className * @param $class
* @param string $method * @param $method
* @return array * @param string $alias
*/ * @return array|mixed
public function getByClass($className, $method = ''): array */
{ public function getAnnotationByMethod($class, $method, $alias = '')
if (!isset($this->_classes[$className])) { {
return []; if (!isset($this->_annotations[$alias])) {
} return [];
if (empty($method)) { }
return $this->_classes[$className]; if (is_object($class)) {
} $class = get_class($class);
foreach ($this->_classes[$className] as $_method => $class) { }
if ($method == $_method) { return $this->_annotations[$alias][$class . '_' . $method] ?? [];
return [$class]; }
}
}
return [];
}
/** /**
* @param ReflectionClass $reflect * @param $className
* @return array * @param string $method
*/ * @return array
private function targets(ReflectionClass $reflect): array */
{ public function getByClass($className, $method = ''): array
$name = $reflect->getName(); {
if (!isset($this->_classes[$name])) { if (!isset($this->_classes[$className])) {
$this->_classes[$name] = []; return [];
} }
$attributes = $reflect->getAttributes(); if (empty($method)) {
if (count($attributes) > 0) { return $this->_classes[$className];
if (!isset($this->_targets[$name])) { }
$this->_targets[$name] = []; foreach ($this->_classes[$className] as $_method => $class) {
} if ($method == $_method) {
foreach ($attributes as $attribute) { return [$class];
$class = $this->instance($attribute); }
if ($class === null) { }
continue; return [];
} }
$this->_targets[$name][] = $class;
}
}
return [];
}
/** /**
* @param ReflectionAttribute $attribute * @param ReflectionClass $reflect
* @return array|object|null * @return array
*/ */
private function instance(ReflectionAttribute $attribute): array|object|null private function targets(ReflectionClass $reflect): array
{ {
if (!class_exists($attribute->getName())) { $name = $reflect->getName();
return null; if (!isset($this->_classes[$name])) {
} $this->_classes[$name] = [];
return $attribute->newInstance(); }
} $attributes = $reflect->getAttributes();
if (count($attributes) > 0) {
if (!isset($this->_targets[$name])) {
$this->_targets[$name] = [];
}
foreach ($attributes as $attribute) {
$class = $this->instance($attribute);
if ($class === null) {
continue;
}
$this->_targets[$name][] = $class;
}
}
return [];
}
/**
* @param ReflectionAttribute $attribute
* @return array|object|null
*/
private function instance(ReflectionAttribute $attribute): array|object|null
{
if (!class_exists($attribute->getName())) {
return null;
}
return $attribute->newInstance();
}
} }
+1 -7
View File
@@ -34,16 +34,10 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param array $handler
* @return array|string * @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function execute(array $handler): array|string public function execute(array $handler): array|string
{ {
// TODO: Implement execute() method. return $this;
foreach ($this->after as $key => $item) {
$this->after[$key] = [Snowflake::createObject($item), 'onHandler'];
}
return $this->after;
} }
+1 -7
View File
@@ -34,16 +34,10 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param array $handler
* @return array|string * @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function execute(array $handler): array|string public function execute(array $handler): array|string
{ {
// TODO: Implement execute() method. return $this;
foreach ($this->interceptor as $key => $item) {
$this->interceptor[$key] = [Snowflake::createObject($item), 'Interceptor'];
}
return $this->interceptor;
} }
} }
+1 -7
View File
@@ -34,16 +34,10 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param array $handler
* @return array|string * @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function execute(array $handler): array|string public function execute(array $handler): array|string
{ {
// TODO: Implement execute() method. return $this;
foreach ($this->limits as $key => $item) {
$this->limits[$key] = [Snowflake::createObject($item), 'next'];
}
return $this->limits;
} }
+1 -7
View File
@@ -35,16 +35,10 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param array $handler
* @return array|string * @return array|string
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function execute(array $handler): array|string public function execute(array $handler): array|string
{ {
// TODO: Implement execute() method. return $this;
foreach ($this->middleware as $key => $item) {
$this->middleware[$key] = [Snowflake::createObject($item), 'onHandler'];
}
return $this->middleware;
} }
-1
View File
@@ -40,7 +40,6 @@ use Annotation\IAnnotation;
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter(); $router = Snowflake::app()->getRouter();
var_dump($this->uri, $handler, $this->method);
$router->addRoute($this->uri, $handler, $this->method); $router->addRoute($this->uri, $handler, $this->method);
return $router; return $router;
+12 -17
View File
@@ -72,27 +72,22 @@ class Middleware
} }
[$controller, $action] = $node->handler; [$controller, $action] = $node->handler;
$attributes = Snowflake::app()->getAttributes(); $attributes = Snowflake::app()->getAttributes();
$annotation = $attributes->getByClass(get_class($controller), $action); $annotation = $attributes->getAnnotationByMethod($controller, $action, 'controllers');
if (count($annotation) < 1) { if (count($annotation) < 1) {
return; return;
} }
foreach ($annotation as $item) { foreach ($annotation as $attribute) {
if (empty($item) || !isset($item['attributes'])) { if ($attribute instanceof Interceptor) {
continue; $node->addInterceptor($attribute->interceptor);
} }
foreach ($item['attributes'] as $attribute) { if ($attribute instanceof After) {
if ($attribute instanceof Interceptor) { $node->addAfter($attribute->after);
$node->addInterceptor($attribute->interceptor); }
} if ($attribute instanceof RMiddleware) {
if ($attribute instanceof After) { $node->addMiddleware($attribute->middleware);
$node->addAfter($attribute->after); }
} if ($attribute instanceof Limits) {
if ($attribute instanceof RMiddleware) { $node->addLimits($attribute->limits);
$node->addMiddleware($attribute->middleware);
}
if ($attribute instanceof Limits) {
$node->addLimits($attribute->limits);
}
} }
} }
} }
+3
View File
@@ -447,6 +447,9 @@ class Node extends HttpService
if (empty($this->handler)) { if (empty($this->handler)) {
return $this; return $this;
} }
if (!is_array($this->handler)) {
return $this;
}
/** @var Middleware $made */ /** @var Middleware $made */
$made = Snowflake::createObject(Middleware::class); $made = Snowflake::createObject(Middleware::class);
$made->getGenerate($this); $made->getGenerate($this);