示例工程
本文档配套的可运行示例工程,把发布文档里的代码块落成能直接 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-factory | docs/modules/guide/best-practices.md | Bean 实例与作用域(单例 vs 工厂) |
guide/best-practices/error-handler-attribute | docs/modules/guide/best-practices.md | 自定义错误处理器(#[ErrorHandler]) |
guide/best-practices/named-bean-injection | docs/modules/guide/best-practices.md | Bean 命名规范(具名 Bean 与按接口注入) |
guide/getting-started/hello-controller | docs/modules/guide/getting-started.md | Hello World 应用(属性路由 + 依赖注入) |
guide/installation/verify-install | docs/modules/guide/installation.md | 安装验证:Runtime 构建容器 |
guide/migration-v0.9/runtime-entry-and-server-mode | docs/modules/guide/migration-v0.9.md | v0.9 的运行时入口与 SERVER_MODE 运行模式 |
guide/project-structure/convention-layout | docs/modules/guide/project-structure.md | 按项目结构约定组织的应用 |
guide/troubleshooting/env-not-effective | docs/modules/guide/troubleshooting.md | 环境变量「读不到」的真正原因 |
guide/troubleshooting/route-not-found | docs/modules/guide/troubleshooting.md | 路由未找到(404 / 405)排查 |
concepts(6)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
concepts/application-lifecycle/container-boot-and-request-flow | docs/modules/concepts/application-lifecycle.md | 应用启动流程与请求处理流程 |
concepts/attributes/di-attributes | docs/modules/concepts/attributes.md | DI 属性:#[Service] / #[Component] / #[Configuration] / #[Bean] |
concepts/attributes/route-and-custom-attributes | docs/modules/concepts/attributes.md | 路由属性、自定义属性与反射 |
concepts/configuration/application-config-tree | docs/modules/concepts/configuration.md | 配置系统:application.* 子树、env()、类型化读取与运行时更新 |
concepts/dependency-injection/constructor-injection | docs/modules/concepts/dependency-injection.md | 构造函数注入与依赖解析 |
concepts/dependency-injection/setter-injection-circular-dependency | docs/modules/concepts/dependency-injection.md | 循环依赖与解法 |
di(11)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
di/advanced/bean-aware-injection | docs/modules/di/advanced.md | Bean 感知注入(BeanAwareInjection) |
di/advanced/circular-dependency | docs/modules/di/advanced.md | 循环依赖检测 |
di/bean-definition/bean-naming | docs/modules/di/bean-definition.md | Bean 的命名规则与 Bean 间依赖 |
di/component-scan/namespace-scan | docs/modules/di/component-scan.md | 命名空间扫描与排除 |
di/component-scan/scanner-direct | docs/modules/di/component-scan.md | 直接使用 ComponentScanner |
di/container-config/definitions-and-config | docs/modules/di/container-config.md | 容器定义的三种来源 + 容器配置文件 |
di/container-config/minimal-container | docs/modules/di/container-config.md | 最小可运行的 DI 容器 |
di/index/quick-start | docs/modules/di/index.md | DI 快速开始:组件定义与 Bean 定义 |
di/injection-modes/injection-modes | docs/modules/di/injection-modes.md | 构造器注入 / Setter 注入 / 属性注入 |
di/injection-modes/named-injection | docs/modules/di/injection-modes.md | 多实现绑定:按 Bean 名称注入 |
di/testing/container-and-bean-tests | docs/modules/di/testing.md | DI 集成测试 |
reflection(8)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
reflection/index/custom-parser-and-filter | docs/modules/reflection/index.md | 自定义类型解析器与类型过滤器 |
reflection/index/phpdoc-processing | docs/modules/reflection/index.md | PHPDoc 解析与 LRU 缓存 |
reflection/index/type-parsing-basics | docs/modules/reflection/index.md | 类型解析基础 |
reflection/phpdoc-processing/method-and-property-docblocks | docs/modules/reflection/phpdoc-processing.md | 方法与属性的 PHPDoc 解析 |
reflection/reflection-namespace/namespace-class-discovery | docs/modules/reflection/reflection-namespace.md | ReflectionNamespace 命名空间类发现 |
reflection/testing/type-parsing-tests | docs/modules/reflection/testing.md | 反射模块类型解析测试 |
reflection/type-parsing/array-and-map-types | docs/modules/reflection/type-parsing.md | 数组、映射与形状类型 |
reflection/type-parsing/class-and-composite-types | docs/modules/reflection/type-parsing.md | 类类型、对象形状与联合类型 |
web(13)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
web/controller/constructor-injection | docs/modules/web/controller.md | 控制器的构造函数注入 |
web/controller/response-helpers | docs/modules/web/controller.md | 控制器的响应写法 |
web/error-handling/custom-handler | docs/modules/web/error-handling.md | 自定义错误处理器 |
web/index/hello-controller | docs/modules/web/index.md | 最小可运行的 Web 控制器 |
web/index/http-methods | docs/modules/web/index.md | 同一路径响应多种 HTTP 方法 |
web/middleware/attribute-priority | docs/modules/web/middleware.md | 路由级中间件属性与优先级 |
web/middleware/global-order | docs/modules/web/middleware.md | 全局中间件的注册与执行顺序 |
web/request-response/query-and-body | docs/modules/web/request-response.md | 读取请求:查询参数、请求体、路径参数、请求头 |
web/routing/named-routes | docs/modules/web/routing.md | 命名路由、类级别前缀与 URL 反向生成 |
web/routing/path-parameters | docs/modules/web/routing.md | 路径参数、可选参数与正则约束 |
web/testing/end-to-end | docs/modules/web/testing.md | Web 应用的端到端请求测试 |
web/views/php-view | docs/modules/web/views.md | PHP 视图引擎渲染模板 |
web/views/twig-view | docs/modules/web/views.md | Twig 视图引擎与模板继承 |
rpc(22)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
rpc/best-practices/retry-timeout-budget | docs/modules/rpc/best-practices.md | 客户端最佳实践:熔断、业务错误与最坏耗时估算 |
rpc/client/error-handling | docs/modules/rpc/client.md | 客户端错误处理 |
rpc/client/proxy-generator-dto | docs/modules/rpc/client.md | 声明式客户端:代理生成与 DTO 序列化 |
rpc/codec/codec-registry-matrix | docs/modules/rpc/codec.md | 编解码器 supports 矩阵与 CodecRegistry |
rpc/codec/jsonrpc-payload-roundtrip | docs/modules/rpc/codec.md | JSON-RPC 与 TARS 报文编解码 |
rpc/configuration/client-config-priority | docs/modules/rpc/configuration.md | 客户端配置的层次与优先级 |
rpc/configuration/server-config | docs/modules/rpc/configuration.md | 服务端配置:注解注册与配置注册 |
rpc/connection-pool/pool-config-and-stats | docs/modules/rpc/connection-pool.md | 连接池配置与统计 |
rpc/connection-pool/pooled-transporter | docs/modules/rpc/connection-pool.md | PooledTransporter 与 PooledSession |
rpc/index/quick-start-loopback | docs/modules/rpc/index.md | RPC 快速开始(进程内回环) |
rpc/middleware/retry-semantics | docs/modules/rpc/middleware.md | Retry 中间件的重试语义 |
rpc/middleware/server-error-boundary | docs/modules/rpc/middleware.md | 服务端中间件的错误边界 |
rpc/middleware/stack-order | docs/modules/rpc/middleware.md | 中间件接口与 MiddlewareStack 执行顺序 |
rpc/server/error-codes | docs/modules/rpc/server.md | 服务端错误码与参数校验 |
rpc/server/in-process-server | docs/modules/rpc/server.md | 服务端:服务注册表与进程内 RPC 服务端 |
rpc/service-discovery/resolver-chain-cache | docs/modules/rpc/service-discovery.md | ChainedServiceResolver 与 CachedServiceResolver |
rpc/service-discovery/static-resolver-round-robin | docs/modules/rpc/service-discovery.md | StaticServiceResolver 与 round_robin 负载均衡 |
rpc/swoole-integration/coroutine-pool | docs/modules/rpc/swoole-integration.md | Swoole 协程环境与协程连接池 |
rpc/testing/fake-transporter-assertions | docs/modules/rpc/testing.md | RPC 测试:可运行的自检脚本 |
rpc/transporters/connection-failure | docs/modules/rpc/transporters.md | 传输层异常:连接失败与 HTTP 错误 |
rpc/transporters/endpoint-parsing | docs/modules/rpc/transporters.md | Endpoint 的构造与解析 |
rpc/transporters/tcp-loopback | docs/modules/rpc/transporters.md | TCP 传输器:真实 socket 上的 JSON-RPC 调用 |
cache(11)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
cache/cache-drivers/array-cache | docs/modules/cache/cache-drivers.md | ArrayCache 数组缓存 |
cache/cache-drivers/ephemeral-driver | docs/modules/cache/cache-drivers.md | Ephemeral 内存驱动 |
cache/cache-drivers/file-driver | docs/modules/cache/cache-drivers.md | FileDriver 文件驱动 |
cache/configuration/custom-cache-pool | docs/modules/cache/configuration.md | 自定义缓存池 |
cache/index/psr16-simple-cache | docs/modules/cache/index.md | PSR-16 简单缓存(SimpleCache) |
cache/index/psr6-cache-pool | docs/modules/cache/index.md | PSR-6 缓存池(通过 DI 容器) |
cache/multi-level-cache/chained-cache | docs/modules/cache/multi-level-cache.md | ChainedCache 链式缓存 |
cache/multi-level-cache/composite-driver | docs/modules/cache/multi-level-cache.md | Composite 复合驱动(多级缓存) |
cache/psr-implementation/psr16-batch-operations | docs/modules/cache/psr-implementation.md | PSR-16:批量操作与 DateInterval TTL |
cache/psr-implementation/psr6-batch-deferred | docs/modules/cache/psr-implementation.md | PSR-6:批量操作、延迟保存与删除 |
cache/testing/psr6-cache-pool-test | docs/modules/cache/testing.md | 用 PHPUnit 测试 PSR-6 缓存池 |
serializer(5)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
serializer/configuration/di-serializer | docs/modules/serializer/configuration.md | 通过 DI 获取序列化器(内置 normalizer + 严格模式) |
serializer/index/basic-usage | docs/modules/serializer/index.md | 简单对象的序列化与反序列化 |
serializer/index/complex-types | docs/modules/serializer/index.md | 复杂类型支持(DateTime 与枚举) |
serializer/normalizer/custom-normalizer | docs/modules/serializer/normalizer.md | 自定义 Normalizer |
serializer/valinor/valinor-adapter | docs/modules/serializer/valinor.md | Valinor 序列化器适配器 |
logger(5)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
logger/configuration/named-loggers | docs/modules/logger/configuration.md | 多日志器配置与命名空间分配 |
logger/handlers-formatters/custom-handler-formatter | docs/modules/logger/handlers-formatters.md | 自定义处理器与格式化器 |
logger/index/basic-usage | docs/modules/logger/index.md | 日志器的基本用法 |
logger/processors/custom-processor | docs/modules/logger/processors.md | 自定义日志处理器(Monolog Processor) |
logger/testing/test-handler-capture | docs/modules/logger/testing.md | 用 Monolog TestHandler 捕获日志做断言 |
otel(6)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
otel/configuration/auto-instrumentation-mounts | docs/modules/otel/configuration.md | OTel 自动挂载埋点 |
otel/configuration/env-options-validation | docs/modules/otel/configuration.md | OTel 环境变量与配置校验 |
otel/configuration/manual-instrumentation | docs/modules/otel/configuration.md | 业务代码中手动埋点 |
otel/index/request-lifecycle-flush | docs/modules/otel/index.md | 请求生命周期事件与自动冲刷 |
otel/index/telemetry-facade | docs/modules/otel/index.md | OTel 遥测门面(Telemetry) |
otel/ip-server-example/business-custom-instrumentation | docs/modules/otel/ip-server-example.md | ip-server 业务自定义埋点 |
resilience(7)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
resilience/circuitbreaker/config-and-builder | docs/modules/resilience/circuitbreaker.md | 熔断器配置 |
resilience/circuitbreaker/state-machine | docs/modules/resilience/circuitbreaker.md | 熔断器状态机 |
resilience/configuration/container-assembly | docs/modules/resilience/configuration.md | ResilienceConfiguration 自动装配 |
resilience/index/quick-start | docs/modules/resilience/index.md | 弹性组件快速开始 |
resilience/middleware/rpc-middleware-chain | docs/modules/resilience/middleware.md | resilience 版 RPC 中间件 |
resilience/retry/config-and-backoff | docs/modules/resilience/retry.md | 重试器配置优先级与退避算法 |
resilience/retry/exception-classification | docs/modules/resilience/retry.md | 重试器异常分类与指标 |
swoole(7)
| 示例 | 对应文档 | 演示能力 |
|---|---|---|
swoole/connection-pool/health-check | docs/modules/swoole/connection-pool.md | 连接池的健康检查、连接预热与异常处理 |
swoole/connection-pool/pool-lifecycle | docs/modules/swoole/connection-pool.md | 连接池的创建、取用、释放与统计 |
swoole/coroutine/channel-and-counter | docs/modules/swoole/coroutine.md | 协程通道(Channel)与跨协程计数器(Counter) |
swoole/coroutine/context-and-defer | docs/modules/swoole/coroutine.md | 协程上下文与延迟执行(defer) |
swoole/index/helper-check | docs/modules/swoole/index.md | Swoole 环境检测与协程能力 |
swoole/installation/environment-check | docs/modules/swoole/installation.md | Swoole 安装后的环境自检 |
swoole/testing/coroutine-assertions | docs/modules/swoole/testing.md | Swoole 模块的测试写法 |