编程

使用 Laravel Introspect 包分析 Laravel 代码库

28 2025-07-12 07:09:00

使用 Laravel Introspect 扩展包,你可以分析 Laravel 代码库,使用类型安全的流畅 API (fluent API) 直接从代码库中查询视图、模型、路由、类等:

你是否正在进行复杂的重构工作,需要查找特定视图的所有使用位置?你是否正在构建开发工具或其他需要代码库信息的工具?是否需要 Eloquent 数据模型的结构化架构信息?

这些都是需要自省代码库并找出使用位置、使用方式以及用途的用例。此扩展包正是为此而设计的。

使用此扩展包,你可以以编程方式查询 Laravel 应用的代码库,以查找特定的视图、路由、类、模型等:

use Mateffy\Introspect\Facades\Introspect;
 
$views = Introspect::views()
    ->whereNameEquals('components.*.button')
    ->whereUsedBy('pages.admin.*')
    ->get();
 
$routes = Introspect::routes()
    ->whereUsesController(MyController::class)
    ->whereUsesMiddleware('auth')
    ->whereUsesMethod('POST')
    ->get();
 
$classes = Introspect::classes()
    ->whereImplements(MyInterface::class)
    ->whereUses(MyTrait::class)
    ->get();
 
$models = Introspect::models()
    ->whereHasProperties(['name', 'email'])
    ->whereHasFillable('password')
    ->get();
 
// Access Eloquent properties, relationships, casts, etc. directly
$detail = Introspect::model(User::class);
 
// Model to JSON schema
$schema = $detail->schema();

主要特性

  • 使用流畅的 API 查询视图、路由、类和模型
  • 使用通配符 (*) 匹配多个视图、路由、类和模型
  • 直接从 Eloquent 模型代码解析属性、关系及其类型等
  • 将查询序列化为 JSON 或从 JSON 反序列化查询(非常适合 LLM 工具调用)

👨‍💻GitHub: capevace/laravel-introspect