Master backend architectures, REST API design, microservices, and server-side development across multiple languages. Use this as a central index to access specialized sub-skills.
中文简介正在准备中。当前显示原文说明。
Generates State pattern for PHP 8.5. Creates state machines with context, state interface, and concrete states for behavior changes. Includes unit tests.
中文简介正在准备中。当前显示原文说明。
查看安装方法安装前可以查看交给 AI 代理的指令内容。
Creates State pattern infrastructure for objects that change behavior based on internal state.
| Scenario | Example |
|---|---|
| Object behavior varies by state | Order (pending, paid, shipped) |
| Many conditionals based on state | Document workflow |
| State-specific transitions | Subscription lifecycle |
| Finite state machines | Payment processing |
Determine:
Path: src/Domain/{BoundedContext}/State/
{Name}StateInterface.php — State contract with all actionsAbstract{Name}State.php — Base class with default (invalid) implementations{StateName}State.php — Concrete state for each state (PendingState, PaidState, etc.){Name}StateFactory.php — Factory for reconstitution from storagePath: src/Domain/{BoundedContext}/Exception/
InvalidStateTransitionException.php — Exception for invalid transitionsPath: src/Domain/{BoundedContext}/Entity/
Update entity to use state pattern with delegation methods.
Path: tests/Unit/Domain/{BoundedContext}/State/
{StateName}StateTest.php — Test each concrete state{Entity}StateTransitionTest.php — Test full state machine| Component | Path |
|---|---|
| State Interface | src/Domain/{BoundedContext}/State/ |
| Concrete States | src/Domain/{BoundedContext}/State/ |
| State Factory | src/Domain/{BoundedContext}/State/ |
| Entity with State | src/Domain/{BoundedContext}/Entity/ |
| Exception | src/Domain/{BoundedContext}/Exception/ |
| Unit Tests | tests/Unit/Domain/{BoundedContext}/State/ |
| Component | Pattern | Example |
|---|---|---|
| Interface | {Name}StateInterface | OrderStateInterface |
| Abstract State | Abstract{Name}State | AbstractOrderState |
| Concrete State | {StateName}State | PendingState, PaidState |
| Factory | {Name}StateFactory | OrderStateFactory |
| Exception | InvalidStateTransitionException | InvalidStateTransitionException |
| Test | {ClassName}Test | PendingStateTest |
interface {Name}StateInterface
{
public function getName(): string;
public function {action1}({Context} $context): self;
public function {action2}({Context} $context): self;
public function canTransitionTo(self $state): bool;
/** @return array<string> */
public function allowedTransitions(): array;
}
abstract readonly class Abstract{Name}State implements {Name}StateInterface
{
public function {action1}({Context} $context): {Name}StateInterface
{
throw InvalidStateTransitionException::actionNotAllowed('{action1}', $this->getName());
}
public function canTransitionTo({Name}StateInterface $state): bool
{
return in_array($state->getName(), $this->allowedTransitions(), true);
}
}
final readonly class {StateName}State extends Abstract{Name}State
{
public function getName(): string
{
return '{state_name}';
}
public function {action1}({Context} $context): {Name}StateInterface
{
$context->recordEvent(new {Event}($context->id()));
return new {NextState}State();
}
public function allowedTransitions(): array
{
return ['{next_state_1}', '{next_state_2}'];
}
}
final class {Name}StateFactory
{
public static function fromName(string $name): {Name}StateInterface
{
return match ($name) {
'pending' => new PendingState(),
'confirmed' => new ConfirmedState(),
default => throw new \InvalidArgumentException("Unknown state: $name"),
};
}
}
// Entity with state
$order = new Order($id, $customerId, $items);
$order->confirm(); // Pending -> Confirmed
$order->pay(); // Confirmed -> Paid
$order->ship(); // Paid -> Shipped
$order->deliver(); // Shipped -> Delivered
// Check state
if ($order->isInState('delivered')) {
// ...
}
// Reconstitute from storage
$state = OrderStateFactory::fromName($row['state']);
$order = new Order($id, $customerId, $items, $state);
┌──────────┐ action1 ┌──────────┐ action2 ┌──────────┐
│ State1 │───────────▶│ State2 │───────────▶│ State3 │
└────┬─────┘ └────┬─────┘ └──────────┘
│ │
│ cancel │ cancel
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│Cancelled │ │Cancelled │
└──────────┘ └──────────┘
| Anti-pattern | Problem | Solution |
|---|---|---|
| Mutable States | Shared state pollution | Make states readonly |
| God State | One state handles all | Split into specific states |
| Missing Transitions | Silent failures | Throw on invalid action |
| State in Entity | Mixed concerns | Extract to State classes |
| No Factory | Hard to reconstitute | Add StateFactory |
For complete PHP templates and examples, see:
references/templates.md — State interface, abstract, concrete templatesreferences/examples.md — Order state machine example and tests还没有评价。欢迎分享你的使用感受。
无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。
还没有评论。来写第一条吧。
Master backend architectures, REST API design, microservices, and server-side development across multiple languages. Use this as a central index to access specialized sub-skills.
中文简介正在准备中。当前显示原文说明。
在每次回复中使用固定前缀并列出本次实际参考的指令/文件,便于校验是否遵循上下文与规则。
中文简介正在准备中。当前显示原文说明。
Automate 21risk tasks via Rube MCP (Composio). Always search tools first for current schemas.
中文简介正在准备中。当前显示原文说明。
Automate 2chat tasks via Rube MCP (Composio). Always search tools first for current schemas.
中文简介正在准备中。当前显示原文说明。
Arquitecto de Soluciones Principal y Consultor Tecnológico de Andru.ia. Diagnostica y traza la hoja de ruta óptima para proyectos de IA en español.
中文简介正在准备中。当前显示原文说明。
[00] META. Orchestrate skills into dynamic chains for complex tasks. Analyzes the task, discovers available skills, builds an optimal chain, explains why each skill is needed, and executes step-by-step with user confirmation. Use for any complex task requiring multiple thinking/research/analysis steps. Triggers on "plan this", "how to approach", "what's the strategy", "build a plan", or any multi-step problem requiring skill orchestration.
中文简介正在准备中。当前显示原文说明。