拦截一切Pointcut playground

写一条规则,看它匹配什么。

按名称、可见性、返回类型或注解选中代码。编辑 pointcut(切入点)和 PHP 源码,每个匹配都会直接在代码中高亮。

Selects 12 joinpoints · 2 can’t be intercepted

Start with an example. Make it your own.

Playground.phpeditable

Selected joinpoints

This matcher runs in your browser and reads one file, following the documented grammar. The weaver itself reflects your real classes, their parents and traits.

Pointcut 速查表

playground 支持的写法,取自框架的 pointcut 参考。参数从不参与匹配:参数列表总是 (*)。

Pointcut

execution(public App\Service->save(*))
实例方法的调用
execution(public App\Service::create(*))
静态方法的调用
execution(App\Service\file_get_contents(*))
该命名空间内的代码对 PHP 函数的调用
access(public App\Entity\User->email)
属性的读和写
within(App\Model\*)
匹配类的每个方法和属性
initialization(App\Model\Order)
用 new 创建对象
staticinitialization(App\Model\*)
匹配类的首次加载
@execution(App\Attribute\Logged)
带有该注解的方法
@access(App\Attribute\Audited)
带有该注解的属性
@within(App\Attribute\Monitored)
带有该注解的类的每个成员

通配符与运算符

*
名称中一段内的任意字符,不会跨越 \
**
任意命名空间深度:App\** 表示 App 下的每个类
get*|is*
名称中一段内的多个备选
Foo+
Foo、它的子类和实现类,以及使用 trait Foo 的类
public|protected
修饰符的备选;也支持 final、readonly、private(set)
: App\Entity|null
限定声明的返回类型
&& || ! ( )
组合、取反和分组 pointcut
matchInherited()
只匹配继承来的或取自 trait 的成员
完整的 pointcut 参考
注解驱动

定义你自己的行为注解。

创建自定义 PHP 注解,为它们挂接行为,只标记需要它们的方法。你的应用代码始终独立于框架。

IDEA 插件直接在你的注解中提供 pointcut 语法高亮和自动补全。

src/Attribute/Transactional.php你的代码
#[\Attribute(\Attribute::TARGET_METHOD)]final class Transactional {}
src/Aspect/TransactionAspect.php事务
final class TransactionAspect implements Aspect{    #[Around('@execution(App\Attribute\Transactional) && !within(App\Legacy\**)')]    public function inTransaction(MethodInvocation $invocation): mixed    {        return $this->db->transactional(            fn () => $invocation->proceed()        );    }}
src/Order/OrderService.php领域
#[Transactional]public function placeOrder(Cart $cart): Order{    return $this->orders->create($cart);}