Skip to content

示例工程 ​

本文档配套的可运行示例工程,把发布文档里的代码块落成能直接 php run.php 跑起来的程序。

为什么有它 ​

文档里的代码块如果只靠人读,很容易和代码漂移。示例工程把每个能独立跑通的片段变成程序, 并由 run-all.php 统一执行——文档与代码不一致时,示例会先失败。

本轮就靠它抓出了 9 处文档与代码的出入(中间件优先级方向、#[ErrorHandler] 数组写法的 ::class 解析陷阱、熔断器与重试器的嵌套语义等),已全部回写到文档。

目录结构 ​

examples/
├── README.md                  # 总说明
├── run-all.php                # 批量执行器:输出通过/失败矩阵
└── <module>/<doc>/<example-name>/
    ├── README.md              # 演示什么 + 对应文档章节 + 运行方式 + 预期输出
    ├── bootstrap.php          # 引导:复用 monorepo 的 vendor,注册示例自身命名空间
    ├── composer.json          # 声明式依赖(path repository 指向 kuiper 子包)
    ├── run.php                # 入口:php run.php
    └── src/                   # 示例自身的类(App\ 命名空间)

运行方式 ​

bash
cd examples

# 跑全部示例,输出通过/失败矩阵(退出码 0 表示全绿)
php run-all.php

# 只跑某个模块
php run-all.php --filter=rpc

# 失败时打印完整输出
php run-all.php --verbose

单个示例:

bash
cd examples/rpc/index/quick-start-loopback
php run.php

离线也能跑:每个示例的 bootstrap.php 向上查找 monorepo 的 vendor/autoload.php 并复用它,因此不需要 composer install;联网环境下也可以按各示例的 composer.json 独立安装(path repository 指向 kuiper/<子包>)。

收录原则 ​

收录的是文档里能独立跑通、且演示真实能力的代码块。以下不收录:

  • 方法签名片段、配置数组片段、类型声明片段——它们是文档插图,不是程序;
  • 文档里明确标注为错误写法的反例;
  • docs/api/ 下由 composer docs:api 生成的参考文档;
  • 依赖外部服务(真实数据库、Redis、OTel Collector、远端 RPC 服务)而无法离线运行的片段: 这类改写为自包含版本(用内存/桩替代),并在示例 README 里注明与文档的差异。

维护方式 ​

  • 文档改动了行为 → 同步改对应示例 → php run-all.php 必须仍然全绿;
  • 文档新增了能跑的片段 → 在对应 <module>/<doc>/ 下加一个示例,并在其 README 里写明 对应的文档章节;
  • 示例与文档冲突 → 以代码为准修文档,并在提交信息里说明。

示例清单 ​

共 110 个示例,覆盖 12 个模块、70 篇文档。每个示例都可独立运行(php run.php)。

guide(9) ​

示例对应文档演示能力
guide/best-practices/bean-scope-and-factorydocs/modules/guide/best-practices.mdBean 实例与作用域(单例 vs 工厂)
guide/best-practices/error-handler-attributedocs/modules/guide/best-practices.md自定义错误处理器(#[ErrorHandler])
guide/best-practices/named-bean-injectiondocs/modules/guide/best-practices.mdBean 命名规范(具名 Bean 与按接口注入)
guide/getting-started/hello-controllerdocs/modules/guide/getting-started.mdHello World 应用(属性路由 + 依赖注入)
guide/installation/verify-installdocs/modules/guide/installation.md安装验证:Runtime 构建容器
guide/migration-v0.9/runtime-entry-and-server-modedocs/modules/guide/migration-v0.9.mdv0.9 的运行时入口与 SERVER_MODE 运行模式
guide/project-structure/convention-layoutdocs/modules/guide/project-structure.md按项目结构约定组织的应用
guide/troubleshooting/env-not-effectivedocs/modules/guide/troubleshooting.md环境变量「读不到」的真正原因
guide/troubleshooting/route-not-founddocs/modules/guide/troubleshooting.md路由未找到(404 / 405)排查

concepts(6) ​

示例对应文档演示能力
concepts/application-lifecycle/container-boot-and-request-flowdocs/modules/concepts/application-lifecycle.md应用启动流程与请求处理流程
concepts/attributes/di-attributesdocs/modules/concepts/attributes.mdDI 属性:#[Service] / #[Component] / #[Configuration] / #[Bean]
concepts/attributes/route-and-custom-attributesdocs/modules/concepts/attributes.md路由属性、自定义属性与反射
concepts/configuration/application-config-treedocs/modules/concepts/configuration.md配置系统:application.* 子树、env()、类型化读取与运行时更新
concepts/dependency-injection/constructor-injectiondocs/modules/concepts/dependency-injection.md构造函数注入与依赖解析
concepts/dependency-injection/setter-injection-circular-dependencydocs/modules/concepts/dependency-injection.md循环依赖与解法

di(11) ​

示例对应文档演示能力
di/advanced/bean-aware-injectiondocs/modules/di/advanced.mdBean 感知注入(BeanAwareInjection)
di/advanced/circular-dependencydocs/modules/di/advanced.md循环依赖检测
di/bean-definition/bean-namingdocs/modules/di/bean-definition.mdBean 的命名规则与 Bean 间依赖
di/component-scan/namespace-scandocs/modules/di/component-scan.md命名空间扫描与排除
di/component-scan/scanner-directdocs/modules/di/component-scan.md直接使用 ComponentScanner
di/container-config/definitions-and-configdocs/modules/di/container-config.md容器定义的三种来源 + 容器配置文件
di/container-config/minimal-containerdocs/modules/di/container-config.md最小可运行的 DI 容器
di/index/quick-startdocs/modules/di/index.mdDI 快速开始:组件定义与 Bean 定义
di/injection-modes/injection-modesdocs/modules/di/injection-modes.md构造器注入 / Setter 注入 / 属性注入
di/injection-modes/named-injectiondocs/modules/di/injection-modes.md多实现绑定:按 Bean 名称注入
di/testing/container-and-bean-testsdocs/modules/di/testing.mdDI 集成测试

reflection(8) ​

示例对应文档演示能力
reflection/index/custom-parser-and-filterdocs/modules/reflection/index.md自定义类型解析器与类型过滤器
reflection/index/phpdoc-processingdocs/modules/reflection/index.mdPHPDoc 解析与 LRU 缓存
reflection/index/type-parsing-basicsdocs/modules/reflection/index.md类型解析基础
reflection/phpdoc-processing/method-and-property-docblocksdocs/modules/reflection/phpdoc-processing.md方法与属性的 PHPDoc 解析
reflection/reflection-namespace/namespace-class-discoverydocs/modules/reflection/reflection-namespace.mdReflectionNamespace 命名空间类发现
reflection/testing/type-parsing-testsdocs/modules/reflection/testing.md反射模块类型解析测试
reflection/type-parsing/array-and-map-typesdocs/modules/reflection/type-parsing.md数组、映射与形状类型
reflection/type-parsing/class-and-composite-typesdocs/modules/reflection/type-parsing.md类类型、对象形状与联合类型

web(13) ​

示例对应文档演示能力
web/controller/constructor-injectiondocs/modules/web/controller.md控制器的构造函数注入
web/controller/response-helpersdocs/modules/web/controller.md控制器的响应写法
web/error-handling/custom-handlerdocs/modules/web/error-handling.md自定义错误处理器
web/index/hello-controllerdocs/modules/web/index.md最小可运行的 Web 控制器
web/index/http-methodsdocs/modules/web/index.md同一路径响应多种 HTTP 方法
web/middleware/attribute-prioritydocs/modules/web/middleware.md路由级中间件属性与优先级
web/middleware/global-orderdocs/modules/web/middleware.md全局中间件的注册与执行顺序
web/request-response/query-and-bodydocs/modules/web/request-response.md读取请求:查询参数、请求体、路径参数、请求头
web/routing/named-routesdocs/modules/web/routing.md命名路由、类级别前缀与 URL 反向生成
web/routing/path-parametersdocs/modules/web/routing.md路径参数、可选参数与正则约束
web/testing/end-to-enddocs/modules/web/testing.mdWeb 应用的端到端请求测试
web/views/php-viewdocs/modules/web/views.mdPHP 视图引擎渲染模板
web/views/twig-viewdocs/modules/web/views.mdTwig 视图引擎与模板继承

rpc(22) ​

示例对应文档演示能力
rpc/best-practices/retry-timeout-budgetdocs/modules/rpc/best-practices.md客户端最佳实践:熔断、业务错误与最坏耗时估算
rpc/client/error-handlingdocs/modules/rpc/client.md客户端错误处理
rpc/client/proxy-generator-dtodocs/modules/rpc/client.md声明式客户端:代理生成与 DTO 序列化
rpc/codec/codec-registry-matrixdocs/modules/rpc/codec.md编解码器 supports 矩阵与 CodecRegistry
rpc/codec/jsonrpc-payload-roundtripdocs/modules/rpc/codec.mdJSON-RPC 与 TARS 报文编解码
rpc/configuration/client-config-prioritydocs/modules/rpc/configuration.md客户端配置的层次与优先级
rpc/configuration/server-configdocs/modules/rpc/configuration.md服务端配置:注解注册与配置注册
rpc/connection-pool/pool-config-and-statsdocs/modules/rpc/connection-pool.md连接池配置与统计
rpc/connection-pool/pooled-transporterdocs/modules/rpc/connection-pool.mdPooledTransporter 与 PooledSession
rpc/index/quick-start-loopbackdocs/modules/rpc/index.mdRPC 快速开始(进程内回环)
rpc/middleware/retry-semanticsdocs/modules/rpc/middleware.mdRetry 中间件的重试语义
rpc/middleware/server-error-boundarydocs/modules/rpc/middleware.md服务端中间件的错误边界
rpc/middleware/stack-orderdocs/modules/rpc/middleware.md中间件接口与 MiddlewareStack 执行顺序
rpc/server/error-codesdocs/modules/rpc/server.md服务端错误码与参数校验
rpc/server/in-process-serverdocs/modules/rpc/server.md服务端:服务注册表与进程内 RPC 服务端
rpc/service-discovery/resolver-chain-cachedocs/modules/rpc/service-discovery.mdChainedServiceResolver 与 CachedServiceResolver
rpc/service-discovery/static-resolver-round-robindocs/modules/rpc/service-discovery.mdStaticServiceResolver 与 round_robin 负载均衡
rpc/swoole-integration/coroutine-pooldocs/modules/rpc/swoole-integration.mdSwoole 协程环境与协程连接池
rpc/testing/fake-transporter-assertionsdocs/modules/rpc/testing.mdRPC 测试:可运行的自检脚本
rpc/transporters/connection-failuredocs/modules/rpc/transporters.md传输层异常:连接失败与 HTTP 错误
rpc/transporters/endpoint-parsingdocs/modules/rpc/transporters.mdEndpoint 的构造与解析
rpc/transporters/tcp-loopbackdocs/modules/rpc/transporters.mdTCP 传输器:真实 socket 上的 JSON-RPC 调用

cache(11) ​

示例对应文档演示能力
cache/cache-drivers/array-cachedocs/modules/cache/cache-drivers.mdArrayCache 数组缓存
cache/cache-drivers/ephemeral-driverdocs/modules/cache/cache-drivers.mdEphemeral 内存驱动
cache/cache-drivers/file-driverdocs/modules/cache/cache-drivers.mdFileDriver 文件驱动
cache/configuration/custom-cache-pooldocs/modules/cache/configuration.md自定义缓存池
cache/index/psr16-simple-cachedocs/modules/cache/index.mdPSR-16 简单缓存(SimpleCache)
cache/index/psr6-cache-pooldocs/modules/cache/index.mdPSR-6 缓存池(通过 DI 容器)
cache/multi-level-cache/chained-cachedocs/modules/cache/multi-level-cache.mdChainedCache 链式缓存
cache/multi-level-cache/composite-driverdocs/modules/cache/multi-level-cache.mdComposite 复合驱动(多级缓存)
cache/psr-implementation/psr16-batch-operationsdocs/modules/cache/psr-implementation.mdPSR-16:批量操作与 DateInterval TTL
cache/psr-implementation/psr6-batch-deferreddocs/modules/cache/psr-implementation.mdPSR-6:批量操作、延迟保存与删除
cache/testing/psr6-cache-pool-testdocs/modules/cache/testing.md用 PHPUnit 测试 PSR-6 缓存池

serializer(5) ​

示例对应文档演示能力
serializer/configuration/di-serializerdocs/modules/serializer/configuration.md通过 DI 获取序列化器(内置 normalizer + 严格模式)
serializer/index/basic-usagedocs/modules/serializer/index.md简单对象的序列化与反序列化
serializer/index/complex-typesdocs/modules/serializer/index.md复杂类型支持(DateTime 与枚举)
serializer/normalizer/custom-normalizerdocs/modules/serializer/normalizer.md自定义 Normalizer
serializer/valinor/valinor-adapterdocs/modules/serializer/valinor.mdValinor 序列化器适配器

logger(5) ​

示例对应文档演示能力
logger/configuration/named-loggersdocs/modules/logger/configuration.md多日志器配置与命名空间分配
logger/handlers-formatters/custom-handler-formatterdocs/modules/logger/handlers-formatters.md自定义处理器与格式化器
logger/index/basic-usagedocs/modules/logger/index.md日志器的基本用法
logger/processors/custom-processordocs/modules/logger/processors.md自定义日志处理器(Monolog Processor)
logger/testing/test-handler-capturedocs/modules/logger/testing.md用 Monolog TestHandler 捕获日志做断言

otel(6) ​

示例对应文档演示能力
otel/configuration/auto-instrumentation-mountsdocs/modules/otel/configuration.mdOTel 自动挂载埋点
otel/configuration/env-options-validationdocs/modules/otel/configuration.mdOTel 环境变量与配置校验
otel/configuration/manual-instrumentationdocs/modules/otel/configuration.md业务代码中手动埋点
otel/index/request-lifecycle-flushdocs/modules/otel/index.md请求生命周期事件与自动冲刷
otel/index/telemetry-facadedocs/modules/otel/index.mdOTel 遥测门面(Telemetry)
otel/ip-server-example/business-custom-instrumentationdocs/modules/otel/ip-server-example.mdip-server 业务自定义埋点

resilience(7) ​

示例对应文档演示能力
resilience/circuitbreaker/config-and-builderdocs/modules/resilience/circuitbreaker.md熔断器配置
resilience/circuitbreaker/state-machinedocs/modules/resilience/circuitbreaker.md熔断器状态机
resilience/configuration/container-assemblydocs/modules/resilience/configuration.mdResilienceConfiguration 自动装配
resilience/index/quick-startdocs/modules/resilience/index.md弹性组件快速开始
resilience/middleware/rpc-middleware-chaindocs/modules/resilience/middleware.mdresilience 版 RPC 中间件
resilience/retry/config-and-backoffdocs/modules/resilience/retry.md重试器配置优先级与退避算法
resilience/retry/exception-classificationdocs/modules/resilience/retry.md重试器异常分类与指标

swoole(7) ​

示例对应文档演示能力
swoole/connection-pool/health-checkdocs/modules/swoole/connection-pool.md连接池的健康检查、连接预热与异常处理
swoole/connection-pool/pool-lifecycledocs/modules/swoole/connection-pool.md连接池的创建、取用、释放与统计
swoole/coroutine/channel-and-counterdocs/modules/swoole/coroutine.md协程通道(Channel)与跨协程计数器(Counter)
swoole/coroutine/context-and-deferdocs/modules/swoole/coroutine.md协程上下文与延迟执行(defer)
swoole/index/helper-checkdocs/modules/swoole/index.mdSwoole 环境检测与协程能力
swoole/installation/environment-checkdocs/modules/swoole/installation.mdSwoole 安装后的环境自检
swoole/testing/coroutine-assertionsdocs/modules/swoole/testing.mdSwoole 模块的测试写法

基于 MIT 许可发布