Skip to content

Commit

Permalink
up: expose annotation and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Player626 committed Mar 4, 2021
1 parent e548596 commit 7b27449
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/Annotation/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

class Parser implements ParserInterface
{
private $parser;
private $annotation;

function __construct(?Annotation $annotation = null)
{
if($annotation == null){
if ($annotation == null) {
$annotation = new Annotation();
static::preDefines([
"POST" => "POST",
Expand All @@ -47,9 +47,9 @@ function __construct(?Annotation $annotation = null)
'DI' => 'DI',
'CONTEXT' => 'CONTEXT',
'RAW' => 'RAW',
'JSON'=>'JSON',
'SESSION'=>'SESSION',
'ROUTER_PARAMS'=>'ROUTER_PARAMS'
'JSON' => 'JSON',
'SESSION' => 'SESSION',
'ROUTER_PARAMS' => 'ROUTER_PARAMS'
]);
$annotation->addParserTag(new Api());
$annotation->addParserTag(new ApiAuth());
Expand All @@ -70,79 +70,86 @@ function __construct(?Annotation $annotation = null)
$annotation->addParserTag(new Method());
$annotation->addParserTag(new Param());
}
$this->parser = $annotation;
$this->annotation = $annotation;
}

/**
* @return Annotation|null
*/
public function getAnnotation(): Annotation
{
return $this->annotation;
}

function parseObject(\ReflectionClass $reflectionClass): ObjectAnnotation
{
$parent = $reflectionClass->getParentClass();
if($parent && $parent->getName() !== AnnotationController::class){
if(Cache::getInstance()->get($parent->getName())){
if ($parent && $parent->getName() !== AnnotationController::class) {
if (Cache::getInstance()->get($parent->getName())) {
$parent = Cache::getInstance()->get($parent->getName());
}else{
} else {
$parent = $this->parseObject($parent);
}
}
$cache = Cache::getInstance()->get($reflectionClass->getName());
if($cache){
$cache = Cache::getInstance()->get($reflectionClass->getName());
if ($cache) {
return $cache;
}
$objectAnnotation = new ObjectAnnotation();

if($parent instanceof ObjectAnnotation){
if($parent->getApiGroupTag()){
if ($parent instanceof ObjectAnnotation) {
if ($parent->getApiGroupTag()) {
$objectAnnotation->addAnnotationTag($parent->getApiGroupTag());
}
if($parent->getApiGroupDescriptionTag()){
if ($parent->getApiGroupDescriptionTag()) {
$objectAnnotation->addAnnotationTag($parent->getApiGroupDescriptionTag());
}
foreach ($parent->getGroupAuthTag() as $tag){
foreach ($parent->getGroupAuthTag() as $tag) {
$objectAnnotation->addAnnotationTag($tag);
}
foreach ($parent->getParamTag() as $tag){
foreach ($parent->getParamTag() as $tag) {
$objectAnnotation->addAnnotationTag($tag);
}
foreach ($parent->getOtherTags() as $otherTag){
foreach ($parent->getOtherTags() as $otherTag) {
$objectAnnotation->addAnnotationTag($otherTag);
}
}
//获取class的标签注解
$tagList = $this->parser->getAnnotation($reflectionClass);
array_walk_recursive($tagList,function ($item)use($objectAnnotation){
$tagList = $this->annotation->getAnnotation($reflectionClass);
array_walk_recursive($tagList, function ($item) use ($objectAnnotation) {
$objectAnnotation->addAnnotationTag($item);
});

//获取class的方法注解
if($parent instanceof ObjectAnnotation){
foreach ($parent->getMethod() as $method){
if ($parent instanceof ObjectAnnotation) {
foreach ($parent->getMethod() as $method) {
$objectAnnotation->addMethod($method);
}
}
foreach ($reflectionClass->getMethods() as $method){
$tagList = $this->parser->getAnnotation($method);
foreach ($reflectionClass->getMethods() as $method) {
$tagList = $this->annotation->getAnnotation($method);
$method = new MethodAnnotation($method->getName());
array_walk_recursive($tagList,function ($item)use($method){
array_walk_recursive($tagList, function ($item) use ($method) {
$method->addAnnotationTag($item);
});
$objectAnnotation->addMethod($method);
}

//获取class的成员属性注解
if($parent instanceof ObjectAnnotation){
foreach ($parent->getProperty() as $property){
if ($parent instanceof ObjectAnnotation) {
foreach ($parent->getProperty() as $property) {
$objectAnnotation->addProperty($property);
}
}
foreach ($reflectionClass->getProperties() as $property)
{
$tagList = $this->parser->getAnnotation($property);
foreach ($reflectionClass->getProperties() as $property) {
$tagList = $this->annotation->getAnnotation($property);
$property = new PropertyAnnotation($property->getName());
array_walk_recursive($tagList,function ($item)use($property){
array_walk_recursive($tagList, function ($item) use ($property) {
$property->addAnnotationTag($item);
});
$objectAnnotation->addProperty($property);
}
Cache::getInstance()->set($reflectionClass->getName(),$objectAnnotation);
Cache::getInstance()->set($reflectionClass->getName(), $objectAnnotation);
return $objectAnnotation;
}

Expand All @@ -155,4 +162,4 @@ public static function preDefines($defines = [])
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/Utility/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class Scanner
{
/** @var Parser|ParserInterface|null */
protected $parser;

function __construct(?ParserInterface $parser = null)
Expand All @@ -27,6 +28,14 @@ function __construct(?ParserInterface $parser = null)
$this->parser = $parser;
}

/**
* @return Parser|ParserInterface|null
*/
public function getParser()
{
return $this->parser;
}

function getObjectAnnotation(string $class): ObjectAnnotation
{
$ref = new \ReflectionClass($class);
Expand Down

0 comments on commit 7b27449

Please sign in to comment.