diff --git a/Annotation/After.php b/Annotation/After.php new file mode 100644 index 00000000..2c63417f --- /dev/null +++ b/Annotation/After.php @@ -0,0 +1,33 @@ +handler; + } + + +} diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php new file mode 100644 index 00000000..e26c3992 --- /dev/null +++ b/Annotation/Annotation.php @@ -0,0 +1,161 @@ +scanDir($dir, $alias); + } + return $this; + } + + + /** + * @param string $alias + * @return array + */ + public function getAlias(string $alias = 'root'): array + { + if (!isset($this->_annotations[$alias])) { + return []; + } + return $this->_annotations[$alias]; + } + + + /** + * @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 $alias + * @return $this + * @throws ReflectionException + */ + private function scanDir(array $paths, string $alias): static + { + if (!isset($this->_annotations[$alias])) { + $this->_annotations[$alias] = []; + } + foreach ($paths as $path) { + if (is_file($path)) { + $this->_annotations[$alias][] = $this->getReflect($path); + } else { + $this->scanDir($path, $alias); + } + } + return $this; + } + + + /** + * @param $class + * @return array + * @throws ReflectionException + */ + private function getReflect($class): array + { + $reflect = Snowflake::getDi()->getReflect($class); + + $this->targets($reflect); + + $annotations = []; + $object = $reflect->newInstance(); + foreach ($reflect->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { + $names = []; + $attributes = $method->getAttributes(); + if (count($attributes) < 1) { + continue; + } + foreach ($attributes as $attribute) { + $names[$attribute->getName()] = $this->instance($attribute); + } + $tmp['handler'] = [$object, $method->getName()]; + $tmp['attributes'] = $names; + + $annotations[] = $tmp; + } + return $annotations; + } + + /** + * @param \ReflectionClass $reflect + */ + private function targets(\ReflectionClass $reflect) + { + $attributes = $reflect->getAttributes(); + if (count($attributes) < 1) { + return; + } + + if (!isset($this->_targets[$reflect->getName()])) { + $this->_targets[$reflect->getName()] = []; + } + + foreach ($attributes as $attribute) { + $this->_targets[$reflect->getName()][] = $this->instance($attribute); + } + } + + + /** + * @param ReflectionAttribute $attribute + * @return array|object + */ + private function instance(ReflectionAttribute $attribute): array|object + { + $instance = $attribute->newInstance(); + if ($instance instanceof Middleware) { + $instance = [$instance, 'onHandler']; + } + if ($instance instanceof Interceptor) { + $instance = [$instance, 'onHandler']; + } + if ($instance instanceof Limits) { + $instance = [$instance, 'onHandler']; + } + if ($instance instanceof After) { + $instance = [$instance, 'onHandler']; + } + return $instance; + } + + +} diff --git a/Annotation/IAnnotation.php b/Annotation/IAnnotation.php new file mode 100644 index 00000000..fd5b3e88 --- /dev/null +++ b/Annotation/IAnnotation.php @@ -0,0 +1,20 @@ +handler; + } + + +} diff --git a/Annotation/Limits.php b/Annotation/Limits.php new file mode 100644 index 00000000..19f167bb --- /dev/null +++ b/Annotation/Limits.php @@ -0,0 +1,33 @@ +handler; + } + + +} diff --git a/Annotation/Middleware.php b/Annotation/Middleware.php new file mode 100644 index 00000000..5bff8a95 --- /dev/null +++ b/Annotation/Middleware.php @@ -0,0 +1,33 @@ +handler; + } + + +} diff --git a/Annotation/Route.php b/Annotation/Route.php new file mode 100644 index 00000000..e1def214 --- /dev/null +++ b/Annotation/Route.php @@ -0,0 +1,42 @@ +getRouter(); + // TODO: Implement setHandler() method. + + return $router->addRoute($this->uri, $closure, $this->method); + } + + +} diff --git a/Annotation/Socket.php b/Annotation/Socket.php new file mode 100644 index 00000000..51b87eed --- /dev/null +++ b/Annotation/Socket.php @@ -0,0 +1,31 @@ +=7.4", + "php": "=7.4", "swoole/ide-helper": "@dev", "ext-json": "*", "phpmailer/phpmailer": "^6.1", @@ -40,7 +40,8 @@ "Console\\": "Console/", "Database\\": "Database/", "Gii\\": "Gii/", - "Kafka\\": "Kafka/" + "Kafka\\": "Kafka/", + "Annotation\\": "Annotation/" }, "files": [ "error.php",