Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持在 PointCut 中指定优先级 #685

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions doc/components/aop/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ AOP 可以很好地解决这个问题,不仅可以在编写上不用事先定

普通的类,你要切入的类。

**参数:**

名称 | 描述 | 默认值
-|-|-
priority | 优先级,越大越先执行 | `0`

### 切入点 PointCut

普通类中的方法,你要切入的方法。
Expand All @@ -72,6 +78,7 @@ AOP 可以很好地解决这个问题,不仅可以在编写上不用事先定
type | 切入点类型,PointCutType::XXX | PointCutType::METHOD
allow | 允许的切入点 | `[]`
deny | 不允许的切入点,即使包含中有的,也可以被排除 | `[]`
priority | 优先级,越大越先执行,为 `null` 时使用 `Aspect` 设置 | `null`

**切入点类型(`\Imi\Aop\PointCutType`):**

Expand Down Expand Up @@ -252,7 +259,7 @@ return [
]
],
'after' => [

]
]
]
Expand Down Expand Up @@ -380,15 +387,15 @@ class TestClass
*/
#[Inject(name: \XXX\Model\User::class)]
protected $model;

/**
* 某Model对象,通过注释类型注入
*
*
* @var XXX\Model\User
*/
#[Inject]
protected $model2;

/**
* 某Model对象,类型声明注入
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Aop/Annotation/PointCut.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public function __construct(
/**
* 不允许的切入点,即使包含中有的,也可以被排除.
*/
public array $deny = []
public array $deny = [],
/**
* 优先级,越大越先执行.
* 为 null 时使用 Aspect 设置.
*/
public ?int $priority = null
) {
}
}
20 changes: 10 additions & 10 deletions src/Aop/AopAnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,39 @@
'deny' => $pointCut->deny,
'extra' => $beforeAnnotation->toArray(),
];
AopManager::addBefore($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addBefore($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($afterAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterAnnotation->toArray(),
];
AopManager::addAfter($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfter($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($aroundAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $aroundAnnotation->toArray(),
];
AopManager::addAround($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAround($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($afterReturningAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterReturningAnnotation->toArray(),
];
AopManager::addAfterReturning($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfterReturning($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($afterThrowingAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterThrowingAnnotation->toArray(),
];
AopManager::addAfterThrowing($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfterThrowing($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
}
break;
Expand Down Expand Up @@ -143,39 +143,39 @@
'deny' => $pointCut->deny,
'extra' => $beforeAnnotation->toArray(),
];
AopManager::addBefore($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addBefore($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($afterAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterAnnotation->toArray(),
];
AopManager::addAfter($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfter($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($aroundAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $aroundAnnotation->toArray(),
];
AopManager::addAround($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAround($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);
}
if ($afterReturningAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterReturningAnnotation->toArray(),
];
AopManager::addAfterReturning($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfterReturning($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);

Check warning on line 170 in src/Aop/AopAnnotationLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Aop/AopAnnotationLoader.php#L170

Added line #L170 was not covered by tests
}
if ($afterThrowingAnnotation)
{
$options = [
'deny' => $pointCut->deny,
'extra' => $afterThrowingAnnotation->toArray(),
];
AopManager::addAfterThrowing($class, $method, $callback, $aspectAnnotation->priority, $options);
AopManager::addAfterThrowing($class, $method, $callback, $pointCut->priority ?? $aspectAnnotation->priority, $options);

Check warning on line 178 in src/Aop/AopAnnotationLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Aop/AopAnnotationLoader.php#L178

Added line #L178 was not covered by tests
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/Component/Aop/Aop/PriorityAop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Imi\Test\Component\Aop\Aop;

use Imi\Aop\Annotation\Around;
use Imi\Aop\Annotation\Aspect;
use Imi\Aop\Annotation\PointCut;
use Imi\Aop\AroundJoinPoint;

#[Aspect(1000)]
class PriorityAop
{
#[Around]
#[PointCut(allow: ['Imi\\Test\\Component\\Aop\\Classes\\TestPriorityClass::test'])]
public function aop1(AroundJoinPoint $joinPoint): mixed
{
$args = $joinPoint->getArgs();
$args[0][] = 1;
$joinPoint->proceed();

return 1;
}

#[Around]
#[PointCut(allow: ['Imi\\Test\\Component\\Aop\\Classes\\TestPriorityClass::test'], priority: 500)]
public function aop2(AroundJoinPoint $joinPoint): mixed
{
$args = $joinPoint->getArgs();
$args[0][] = 2;
$joinPoint->proceed();

return 2;
}

#[Around]
#[PointCut(allow: ['Imi\\Test\\Component\\Aop\\Classes\\TestPriorityClass::test'], priority: 1500)]
public function aop3(AroundJoinPoint $joinPoint): mixed
{
$args = $joinPoint->getArgs();
$args[0][] = 3;
$joinPoint->proceed();

return 3;
}
}
15 changes: 15 additions & 0 deletions tests/unit/Component/Aop/Classes/TestPriorityClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Imi\Test\Component\Aop\Classes;

class TestPriorityClass
{
public function test(array &$list): int
{
$list[] = 0;

return 0;
}
}
10 changes: 10 additions & 0 deletions tests/unit/Component/Tests/AopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Imi\App;
use Imi\Test\BaseTest;
use Imi\Test\Component\Aop\Classes\TestPriorityClass;

/**
* @testdox Aop
Expand Down Expand Up @@ -87,4 +88,13 @@ public function testAfterThrowing(): void
$this->assertNotNull($throwable);
$this->assertEquals('test', $throwable->getMessage());
}

public function testPriority(): void
{
$test = App::getBean(TestPriorityClass::class);
$list = [];
$result = $test->test($list);
$this->assertEquals(3, $result);
$this->assertEquals([3, 1, 2, 0], $list);
}
}
Loading