
你的代码与 PHP 之间没有任何隐藏
运行的就是普通 PHP:你可以阅读它、单步调试它,也可以随时查询它。
你的类,以及实际运行的代码
一个带缓存行为的报表服务,以及框架为它写出的两个文件。它们正是下方可单步调试的会话中用到的真实文件。
你的源码
用 #[Cacheable] 挂接一个缓存行为。这就是你打开、阅读并设置断点的文件。
<?php
declare(strict_types=1);
namespace App\Report;
use App\Attribute\Cacheable;
class ReportService
{
public function __construct(private OrderRepository $orders) {}
#[Cacheable(ttl: 300)]
已挂接缓存行为
public function monthlyRevenue(string $month): int
{
$total = 0;
此处断点会触发
foreach ($this->orders->forMonth($month) as $order) {
$total += $order->cents;
}
return $total;
}
}
织入后的源码
你的类体成为一个 trait。只有声明这一行有变化,所以每个行号都保持不变。在调试模式下,PHP 以你的原始路径加载它,因此调试器停在 src/Report/ReportService.php 中。
<?php
declare(strict_types=1);
namespace App\Report;
use App\Attribute\Cacheable;
trait ReportServiceOriginalTrait
原为:class ReportService
{
public function __construct(private OrderRepository $orders) {}
#[Cacheable(ttl: 300)]
public function monthlyRevenue(string $month): int
{
$total = 0;
foreach ($this->orders->forMonth($month) as $order) {
$total += $order->cents;
}
return $total;
}
}
include_once AOP_CACHE_DIR . '/Report/ReportService.php';
代理类
PHP 实际加载的类,沿用你的类名,所以调用栈中显示的是 ReportService。中间件就是一个可读的方法:joinpoint(连接点)只构建一次,然后运行它。
<?php
declare(strict_types=1);
namespace App\Report;
use Go\Aop\Framework\InterceptorInjector;
use Go\Aop\Framework\Interceptor;
use Go\Aop\Framework\The;
use App\Aspect\CachingAspect;
use Go\Aop\Intercept\DynamicMethodInvocation;
use App\Attribute\Cacheable as Cacheable;
class ReportService implements \Go\Aop\Proxy
你的类名
{
use ReportServiceOriginalTrait {
你的类体
ReportServiceOriginalTrait::monthlyRevenue as private monthlyRevenueOriginalAlias;
}
#[\App\Attribute\Cacheable(ttl: 300)]
public function monthlyRevenue(string $month): int
{
/** @var DynamicMethodInvocation<self, int> $__joinPoint */
static $__joinPoint = InterceptorInjector::forMethod(
首次调用时构建
self::class,
'monthlyRevenue',
[
Interceptor::around(The::aspect(CachingAspect::class)->cache(...)),
Ctrl-click 打开 CachingAspect::cache()
],
$this->monthlyRevenueOriginalAlias(...),
你的原始方法
);
return $__joinPoint->__invoke($this, [$month]);
运行中间件
}
}
像调试自己写的代码一样调试它
同一个类的一次真实调试会话,录制下来并在你的浏览器中回放。单步进入调用,在行号栏设置断点,打开任意栈帧。向导会说明每一步发生了什么。
Frames
Variables
Console timed under the debugger
问问织入器做了什么
框架自带的控制台能回答关于内核的问题:它有哪些行为类,每个行为类作用到哪里。它还能提前织入缓存。滚动浏览这些命令,终端显示的是它们针对上面这个应用的真实输出。
Run the app
php bin/report.phpThe same app, without the debugger. The first call runs the slow query; the second comes from the caching behavior in a hundredth of a millisecond.
List the behavior classes
vendor/bin/aspect debug:aspect bootstrap.phpEvery behavior class (an aspect, in the API) your kernel registers, the file it is defined in, and the advisors it declares. The loader file only boots the kernel; it must not handle a request.
List the advisors
vendor/bin/aspect debug:advisor bootstrap.phpEach advisor with its pointcut. Here there is one:
cache(), for every method marked#[Cacheable].See what an advisor reaches
vendor/bin/aspect debug:advisor bootstrap.php --advisor='App\Aspect\CachingAspect->cache'Analyzes every file below the include paths and lists the members the advisor matches:
monthlyRevenue(), and nothing else.Weave ahead of time
vendor/bin/aspect cache:warmup:aop bootstrap.phpWeaves every file at deploy time, so production serves the prebuilt cache. It fails when a file can’t be woven;
--workers=8weaves in parallel.
2026-10: 21199 cents in 156.16 ms 2026-10: 21199 cents in 0.01 ms
Aspect debug information ======================== App\AppKernel has following enabled aspects: App\Aspect\CachingAspect ------------------------ Defined in: /app/src/Aspect/CachingAspect.php Pointcuts and advices --------- --------------------------------- Type Identifier --------- --------------------------------- Advisor App\Aspect\CachingAspect->cache --------- ---------------------------------
Advisor debug information ========================= List of registered advisors in the container --------------------------------- ------------------------------------- Id Expression --------------------------------- ------------------------------------- App\Aspect\CachingAspect->cache @execution(App\Attribute\Cacheable) --------------------------------- ------------------------------------- If you want to query an information about concrete advisor, then just query it by adding --advisor="Advisor\Name" to the command
Advisor debug information ========================= Total 6 files to analyze. -> matching method App\Report\ReportService->monthlyRevenue
Total 6 files to process. [DONE]: Total processed 6, 0 errors in 5 ms.
构建行为,保持一切可调试。
大多数框架要你相信魔法。在这里,运行的每一行代码你都可以打开、查看并单步执行。