This commit is contained in:
2021-02-23 14:31:16 +08:00
parent f36ceb77de
commit dad53e797e
2 changed files with 13 additions and 27 deletions
+11 -26
View File
@@ -5,6 +5,7 @@ namespace Annotation;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute; use ReflectionAttribute;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
@@ -67,19 +68,6 @@ class Annotation extends Component
} }
/**
* @param string $alias
* @return array
*/
public function getAlias(string $alias = 'root'): array
{
if (!isset($this->_annotations[$alias])) {
return [];
}
return $this->_annotations[$alias];
}
/** /**
* @param array $paths * @param array $paths
* @param string $namespace * @param string $namespace
@@ -87,12 +75,10 @@ class Annotation extends Component
* @return $this * @return $this
* @throws ReflectionException|NotFindPropertyException * @throws ReflectionException|NotFindPropertyException
* @throws NotFindClassException * @throws NotFindClassException
* @throws Exception
*/ */
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])) {
$this->_annotations[$alias] = [];
}
foreach ($paths as $path) { foreach ($paths as $path) {
$explode = explode('/', $path); $explode = explode('/', $path);
@@ -191,7 +177,7 @@ class Annotation extends Component
/** /**
* @param string $class * @param string $class
* @return ReflectionClass|null * @return ReflectionClass|null
* @throws ReflectionException * @throws ReflectionException|NotFindClassException
*/ */
private function reflectClass(string $class): ?ReflectionClass private function reflectClass(string $class): ?ReflectionClass
{ {
@@ -213,8 +199,8 @@ class Annotation extends Component
} }
$name = get_class($object) . '_' . $method->getName(); $name = get_class($object) . '_' . $method->getName();
if (!isset($this->_annotations[$alias][$name])) { if (!isset($this->_annotations[$name])) {
$this->_annotations[$alias][$name] = []; $this->_annotations[$name] = [];
} }
$array = []; $array = [];
@@ -227,7 +213,7 @@ class Annotation extends Component
$array[] = $class->execute([$object, $method->getName()]); $array[] = $class->execute([$object, $method->getName()]);
} }
$this->_annotations[$alias][$name][] = $array; $this->_annotations[$name][] = $array;
return []; return [];
} }
@@ -235,18 +221,17 @@ class Annotation extends Component
/** /**
* @param $class * @param $class
* @param $method * @param $method
* @param string $alias
* @return mixed * @return mixed
*/ */
public function getAnnotationByMethod($class, $method, $alias = ''): mixed #[Pure] public function getAnnotationByMethod($class, $method): array
{ {
if (!isset($this->_annotations[$alias])) {
return [];
}
if (is_object($class)) { if (is_object($class)) {
$class = get_class($class); $class = get_class($class);
} }
return $this->_annotations[$alias][$class . '_' . $method] ?? []; if (!isset($this->_annotations[$class . '_' . $method])) {
return [];
}
return $this->_annotations[$class . '_' . $method];
} }
+2 -1
View File
@@ -72,10 +72,11 @@ class Middleware
} }
[$controller, $action] = $node->handler; [$controller, $action] = $node->handler;
$attributes = Snowflake::app()->getAttributes(); $attributes = Snowflake::app()->getAttributes();
$annotation = $attributes->getAnnotationByMethod($controller, $action, 'controllers'); $annotation = $attributes->getAnnotationByMethod($controller, $action);
if (count($annotation) < 1) { if (count($annotation) < 1) {
return; return;
} }
var_dump(count($annotation));
foreach ($annotation as $attribute) { foreach ($annotation as $attribute) {
if ($attribute instanceof Interceptor) { if ($attribute instanceof Interceptor) {
$node->addInterceptor($attribute->interceptor); $node->addInterceptor($attribute->interceptor);