Skip to content

Commit

Permalink
修复自定义注解无法使用Aop切面编程的BUG (swoft-cloud/swoft-component#139)
Browse files Browse the repository at this point in the history
* 增加自定义Aop注解测试

* 增加自定义方法注解,支持PointAnnotation

* 完善单测

* 删除CustomMethod注解

* 通过自定义Parser收集注解测试

* 删除CustomMethod注解
  • Loading branch information
limingxinleo authored and huangzhhui committed Jul 24, 2018
1 parent 668b1f9 commit b60ec7f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
12 changes: 10 additions & 2 deletions framework/src/Bean/Wrapper/AbstractWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ private function parseMethodAnnotations(string $className, \ReflectionMethod $me
// 循环方法注解解析
foreach ($methodAnnotations[$methodName] as $methodAnnotationAry) {
foreach ($methodAnnotationAry as $methodAnnotation) {
$annotationClass = get_class($methodAnnotation);
if (!in_array($annotationClass, $this->getMethodAnnotations())) {
if (!$this->inMethodAnnotations($methodAnnotation)) {
continue;
}

Expand All @@ -223,6 +222,15 @@ private function parseMethodAnnotations(string $className, \ReflectionMethod $me
}
}

/**
* @return bool
*/
protected function inMethodAnnotations($methodAnnotation): bool
{
$annotationClass = get_class($methodAnnotation);
return in_array($annotationClass, $this->getMethodAnnotations());
}

/**
* 方法没有配置路由注解解析
*
Expand Down
7 changes: 6 additions & 1 deletion framework/src/Bean/Wrapper/BeanWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function isParsePropertyAnnotations(array $annotations): bool
*/
public function isParseMethodAnnotations(array $annotations): bool
{
return isset($annotations[Cacheable::class]) || isset($annotations[CachePut::class]);
return true;
}

protected function inMethodAnnotations($methodAnnotation): bool
{
return true;
}
}
32 changes: 32 additions & 0 deletions framework/test/Cases/Aop/Annotation/DemoAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace SwoftTest\Aop\Annotation;

/**
* Class DemoAnnotation
* @Annotation
* @Target("METHOD")
* @package SwoftTest\Aop\Annotation
*/
class DemoAnnotation
{
/**
* @var string
*/
private $name;

public function __construct(array $values)
{
if (isset($values['name'])) {
$this->name = $values['name'];
}
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
10 changes: 10 additions & 0 deletions framework/test/Cases/Aop/AnnotationAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Cacheable;
use Swoft\Bean\Annotation\CachePut;
use SwoftTest\Aop\Annotation\DemoAnnotation;

/**
*
Expand Down Expand Up @@ -41,4 +42,13 @@ public function cacheable()
{
return 'cacheable';
}

/**
* @DemoAnnotation(name=" hello")
* @return string
*/
public function demoAnnotation()
{
return 'demo';
}
}
17 changes: 14 additions & 3 deletions framework/test/Cases/Aop/AnnotationAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use Swoft\Bean\Annotation\PointAnnotation;
use Swoft\Bean\Annotation\Cacheable;
use Swoft\Bean\Annotation\CachePut;
use SwoftTest\Aop\Annotation\DemoAnnotation;
use SwoftTest\Aop\Collector\DemoCollector;

/**
* @Aspect
* @PointAnnotation(
* include={
* Cacheable::class,
* CachePut::class
* CachePut::class,
* DemoAnnotation::class
* }
* )
* @uses AnnotationAspect
Expand All @@ -37,9 +40,17 @@ class AnnotationAspect
*/
public function around(ProceedingJoinPoint $proceedingJoinPoint)
{
$tag = ' around before ';
$class = $proceedingJoinPoint->getTarget();
$method = $proceedingJoinPoint->getMethod();

$tag = '';
if ($annotation = DemoCollector::$methodAnnotations[get_class($class)][$method] ?? null) {
$tag .= $annotation->getName();
}

$tag .= ' around before ';
$result = $proceedingJoinPoint->proceed();
$tag .= ' around after ';
return $result.$tag;
return $result . $tag;
}
}
12 changes: 12 additions & 0 deletions framework/test/Cases/Aop/Collector/DemoCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace SwoftTest\Aop\Collector;

class DemoCollector
{
/**
* The annotations of method
*
* @var array
*/
public static $methodAnnotations = [];
}
16 changes: 16 additions & 0 deletions framework/test/Cases/Aop/Parser/DemoAnnotationParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SwoftTest\Aop\Parser;

use Swoft\Bean\Collector;
use SwoftTest\Aop\Collector\DemoCollector;

class DemoAnnotationParser
{
public function parser(string $className, $objectAnnotation = null, string $propertyName = "", string $methodName = "", $propertyValue = null)
{
Collector::$methodAnnotations[$className][$methodName][] = get_class($objectAnnotation);
DemoCollector::$methodAnnotations[$className][$methodName] = $objectAnnotation;
return null;
}
}
8 changes: 8 additions & 0 deletions framework/test/Cases/AopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function testAnnotationAop()
$this->assertEquals('cachePut around before around after ', $result);
}

public function testCustomAnnotationAop()
{
/* @var AnnotationAop $annotationBean*/
$annotationBean = App::getBean(AnnotationAop::class);
$result = $annotationBean->demoAnnotation();
$this->assertEquals('demo hello around before around after ', $result);
}

public function testRegAop()
{
/* @var RegBean $annotationBean*/
Expand Down

0 comments on commit b60ec7f

Please sign in to comment.