Skip to content

Commit

Permalink
支持在 PointCut 中指定优先级
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 20, 2024
1 parent 061bfb2 commit 739a62e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
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 @@ public static function load(bool $force = true): void
'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 @@ public static function load(bool $force = true): void
'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

0 comments on commit 739a62e

Please sign in to comment.