rokke/console
Composer 安装命令:
composer require rokke/console
包简介
Console transport adapter for the Rokke Runtime — routes CLI commands to compiled operations
README 文档
README
Console transport adapter for the Rokke Runtime — routes CLI commands to compiled operations exactly as rokke/http routes HTTP requests.
What this is
rokke/console is the CLI transport layer for the Rokke Framework. It scans a directory for classes annotated with #[Command('name')], compiles them into a flat CommandRegistry at build time, and at runtime parses argv tokens into an OperationContext that flows through the same ExecutionEngine as HTTP operations.
There is no Swoole dependency — ConsoleHost::run() uses plain PHP I/O. The build pipeline (ConsoleKernel) mirrors HttpKernel precisely: capability → model builder pass → descriptor → compiler → artifact → host.
Installation
composer require rokke/console
Requires: PHP ≥ 8.4, rokke/runtime ^0.15.0
Usage
1. Create a command handler
use Rokke\Console\Attribute\Command; use Rokke\Console\Attribute\Option; #[Command('users:create')] final class CreateUserCommand { public function __invoke( #[Option] string $name, #[Option] int $age = 0, ): string { return "Created user {$name} (age {$age})"; } }
2. Boot the kernel and run
use Rokke\Console\ConsoleKernel; use Rokke\Console\ConsoleModule; (new ConsoleKernel()) ->register(new ConsoleModule( directory: __DIR__ . '/Commands', namespace: 'App\\Commands', )) ->build() ->run(); // reads $_SERVER['argv'] automatically
3. Invoke from the terminal
php app users:create --name=Fernando --age=28
# Created user Fernando (age 28)
API reference
Attributes
| Attribute | Target | Purpose |
|---|---|---|
#[Command('name')] |
class | Marks an invokable class as a CLI command |
#[Option] |
parameter | Resolves a handler parameter from --key=value or --flag |
#[Option(short: 'n')] |
parameter | Also accepts -n value short form |
Key classes
| Class | Purpose |
|---|---|
ConsoleKernel |
Build entry point — discovers, compiles, produces ConsoleHost |
ConsoleModule |
Module that wires ConsoleDirectoryDiscoveryProvider into the graph |
ConsoleHost |
Runtime entry point — parses argv, dispatches via ExecutionEngine |
CommandRegistry |
Compiled artifact — flat map of name → operationId |
ConsoleContextFactory |
Parses raw argv tokens into OperationContext |
When to use
Use rokke/console when you need to route CLI commands through the same compiled operation pipeline as HTTP handlers — sharing middleware, validation, and result plans between HTTP and CLI surfaces.
For one-off scripts with no need for the operation pipeline, use plain PHP directly.
Stability
0.x — API may evolve as the framework matures. No breaking changes within a minor version.
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-07