This commit is contained in:
2020-08-31 23:18:50 +08:00
parent 8ccfe00b0f
commit 6d9543098d
2 changed files with 43 additions and 25 deletions
+13
View File
@@ -34,6 +34,19 @@ abstract class BaseAnnotation extends Component
} }
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param array $rules * @param array $rules
+17 -12
View File
@@ -60,13 +60,14 @@ class Annotation extends BaseAnnotation
/** /**
* @param $path * @param $path
* @param $namespace * @param $namespace
* @param $class
* @throws ReflectionException * @throws ReflectionException
*/ */
public function registration_notes($path, $namespace) public function registration_notes($path, $namespace, $class)
{ {
$path = rtrim($path, '/'); $path = rtrim($path, '/');
foreach (glob($path . '/*') as $item) { foreach (glob($path . '/*') as $item) {
$this->scanning($item, $namespace); $this->scanning($item, $namespace, $class);
} }
} }
@@ -92,27 +93,30 @@ class Annotation extends BaseAnnotation
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @Message(updatePosition) * @param string $className
* @throws ReflectionException
* @throws Exception * @throws Exception
* @Message(updatePosition)
*/ */
public function resolve(ReflectionClass $reflect) public function resolve(ReflectionClass $reflect, string $className)
{ {
$controller = $reflect->newInstance(); $controller = $reflect->newInstance();
$methods = $this->getPrivates($reflect); $annotations = $this->getAnnotation($className);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $function) { foreach ($methods as $function) {
foreach ($annotations as $annotation) {
$comment = $function->getDocComment(); $comment = $function->getDocComment();
$methodName = $function->getName(); $methodName = $function->getName();
preg_match('/@(' . $function . ')\((.*?)\)/', $comment, $events); preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) { if (!isset($events[1])) {
continue; continue;
} }
if (!$this->isLegitimate($events)) { if (!$this->isLegitimate($events)) {
continue; continue;
} }
$_key = $this->getName($function, $events); $_key = $this->getName($annotation, $events);
if (empty($events[2])) { if (empty($events[2])) {
$this->push($_key, [$controller, $methodName]); $this->push($_key, [$controller, $methodName]);
} else { } else {
@@ -122,6 +126,7 @@ class Annotation extends BaseAnnotation
} }
} }
} }
}
/** /**
@@ -160,22 +165,22 @@ class Annotation extends BaseAnnotation
/** /**
* @param string $path * @param string $path
* @param $namespace * @param $namespace
* @param $className
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
protected function scanning($path, $namespace) protected function scanning($path, $namespace, $className)
{ {
$di = Snowflake::getDi(); $di = Snowflake::getDi();
foreach (glob($path . '/*') as $file) { foreach (glob($path . '/*') as $file) {
if (is_dir($file)) { if (is_dir($file)) {
$this->scanning($path, $namespace); $this->scanning($path, $namespace, $className);
} }
$explode = explode('/', $file); $explode = explode('/', $file);
$class = str_replace('.php', '', end($explode)); $class = str_replace('.php', '', end($explode));
$this->resolve($di->getReflect($namespace . '\\' . $class)); $this->resolve($di->getReflect($namespace . '\\' . $class), $className);
} }
} }