一枚放大镜透过层层玻璃,显出其中青色的结构。
特性 · 可调试

没有你无法单步调试的魔法。

生成的代码不该暗藏玄机。框架生成可读的 PHP,你可以用平常的工具查看、对比、调试和单步执行。

你的类,以及实际运行的代码

一个带缓存行为的报表服务,以及框架为它写出的两个文件。它们正是下方可单步调试的会话中用到的真实文件。

你的源码

用 #[Cacheable] 挂接一个缓存行为。这就是你打开、阅读并设置断点的文件。

ReportService.php你的代码

        
        
        <?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 中。

ReportServiceOriginalTrait.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(连接点)只构建一次,然后运行它。

ReportService.php生成

        
        
        <?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]);
        运行中间件
        
        
      
        
        
            }
        
        
        
      
        
        
        }
        
        
        
      

像调试自己写的代码一样调试它

同一个类的一次真实调试会话,录制下来并在你的浏览器中回放。单步进入调用,在行号栏设置断点,打开任意栈帧。向导会说明每一步发生了什么。

Debug: php bin/report.phpreplay
bin/report.php
<?php
declare(strict_types=1);
use App\Report\OrderRepository;
use App\Report\ReportService;
require dirname(__DIR__) . '/bootstrap.php';
$reports = new ReportService(new OrderRepository());
foreach (['2026-10', '2026-10'] as $month) {
$start = hrtime(true);
$cents = $reports->monthlyRevenue($month);
printf("%s: %d cents in %.2f ms\n", $month, $cents, (hrtime(true) - $start) / 1e6);
}

Frames

    Variables

    Console timed under the debugger

    stop 1 of 84

    从控制台

    问问织入器做了什么

    框架自带的控制台能回答关于内核的问题:它有哪些行为类,每个行为类作用到哪里。它还能提前织入缓存。滚动浏览这些命令,终端显示的是它们针对上面这个应用的真实输出。

    1. Run the app

      php bin/report.php

      The 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.

    2. List the behavior classes

      vendor/bin/aspect debug:aspect bootstrap.php

      Every 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.

    3. List the advisors

      vendor/bin/aspect debug:advisor bootstrap.php

      Each advisor with its pointcut. Here there is one: cache(), for every method marked #[Cacheable].

    4. 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.

    5. Weave ahead of time

      vendor/bin/aspect cache:warmup:aop bootstrap.php

      Weaves every file at deploy time, so production serves the prebuilt cache. It fails when a file can’t be woven; --workers=8 weaves in parallel.

    Terminalrecorded
    php bin/report.php
    2026-10: 21199 cents in 156.16 ms
    2026-10: 21199 cents in 0.01 ms
    vendor/bin/aspect debug:aspect bootstrap.php
    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
     --------- ---------------------------------
    vendor/bin/aspect debug:advisor bootstrap.php
    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
    vendor/bin/aspect debug:advisor bootstrap.php --advisor='App\Aspect\CachingAspect->cache'
    Advisor debug information
    =========================
    
    Total 6 files to analyze.
      -> matching method App\Report\ReportService->monthlyRevenue
    vendor/bin/aspect cache:warmup:aop bootstrap.php
    Total 6 files to process.
    
    [DONE]: Total processed 6, 0 errors in 5 ms.

    构建行为,保持一切可调试。

    大多数框架要你相信魔法。在这里,运行的每一行代码你都可以打开、查看并单步执行。