Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

state-machine

/state-machine

Modeluje workflow i statusy zamówień lub dokumentów w bibliotece state-machine dla PHP 8.5, zastępując rozproszone sprawdzenia statusu.

Repozytorium
statemachine ↗
Licencja
MIT
Ostatnia zmiana repozytorium
05.11.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 05.11.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Modeluje workflow i statusy zamówień lub dokumentów w bibliotece state-machine dla PHP 8.5, zastępując rozproszone sprawdzenia statusu.

Oryginalny opis (EN)

Model a workflow, status lifecycle or approval flow with the nandordudas/state-machine library (PHP 8.5+). Use when the user wants states/transitions, guards, hooks, order/payment/document statuses, or to replace scattered `if ($status === ...)` checks.

Jak działa

  1. 1. Model first
  2. 2. Build
  3. 3. Bind state to the entity
  4. 4. Drive it
  5. Rules of thumb
Sekcje z nagłówków SKILL.md, w oryginale · 60 linii, 415 słów
Podgląd SKILL.md
---
name: state-machine
description: Model a workflow, status lifecycle or approval flow with the nandordudas/state-machine library (PHP 8.5+). Use when the user wants states/transitions, guards, hooks, order/payment/document statuses, or to replace scattered `if ($status === ...)` checks.
---

# Modelling workflows with state-machine

## 1. Model first

List **states** (nouns: `Draft`, `Review`), **events** (verbs: `Submit`, `Approve`), and for each event its allowed
source states and target. Decide which rules are *guards* (may this happen now? pure) and which are *hooks* (do
something because it happened). Draw it: `$definition->toMermaid()`.

Use string-backed enums for states and events (`enum DocState: string`), one file each. Pass the enums
everywhere; `->value` is never needed: read the state back with `$machine->stateAs(DocState::class)`, give
`machine(onEnter:/onExit:)` `[state, hook]` pairs, and seed an `InMemoryStore` with an enum.

## 2. Build

```php
$definition = Definition::builder()
    ->initial(DocState::Draft)
    ->states(...DocState::cases())
    ->onEnter(DocState::Published, $notify)          // after the state is stored
    ->onExit(DocState::Review, $clearReviewer)       // before leaving; throwing aborts
    ->transition(
        Transition::on(DocEvent::Submit, DocState::Draft, DocState::Review),
        Transition::on(DocEvent::Approve, DocState::Review, DocState::Published)->withGuard(new ApproverGuard()),
    )
    ->build();
```

Shorthand: `machine(initial:, states: Enum::class, transitions: [on(...)], onEnter: [...])`; guard-less:
`Definition::fromTable()`. Several transitions with the same event and source = conditional branching; **first
permitted wins**, so put the guarded one first.

## 3. Bind state to the entity

`machineFor($definition, $entity, 'status')` (string or enum-typed property, public or not) or
`new CallbackStore($get, $set)`. For shared rows make the write conditional: `machineFor(..., compareAndSet: fn ($old,
$new) => $repo->update(...) === 1)` or `AtomicCallbackStore` (`UPDATE ... WHERE status = :expected`, `afterWrite:` to
update the entity). When guards read other columns too, wrap the store in a `LockingStore` (`SELECT ... FOR UPDATE`).

## 4. Drive it

`send()` throws; `tryTransition()` returns refusals as a result (`->ok()`, `->failure`) and throws only for hook and
concurrency failures. Catch `StateMachineException` for all library errors. Pass data via the payload array, read in
guards/hooks through `TransitionContext` (`$context->fromAs(DocState::class)` and `toAs()` give the states as
enums). Prefer `tryTransition()` over `can()` + `send()` (racy); `sendAll()` sends several events in order.

## Rules of thumb

- Guards: pure and cheap. Combine with `allOf`, `anyOf`, `not`, `payloadIs`, `payloadsEqual`, or a named `Guard` class
  when the rule is reused (see `examples/Document/ApproverGuard.php`).
- When users need to know *why* they can't do something, explain the guards: `because($guard, 'message', code: '...')`
  or an `ExplainingGuard` class, then show `$machine->blockers($event, $payload)`. Put labels in transition `meta`.
- Side effects go in hooks. `before`/`onExit` can veto; `onEnter`/`after` run after the write and failures are wrapped
  in `AfterHookFailedException` (no rollback).
- Never call `send()` inside guards or `before`/`onExit` (`ConcurrentTransitionException`); from `onEnter`/`after` it is
  queued and applied once the current transition has finished (cycles stop at `maxChainedEvents`).
- Side effects that must not happen when the transition fails belong in `onEnter`/`after`, not in `before`/`onExit`.
- Check structure with `$definition->unreachableStates()`.
- Worked examples: `examples/Payment`, `examples/Document`, `examples/OrderStatus`.

Wersje

RepozytoriumWynikLicencja
Ta strona · nandordudas/statemachine100MIT
zenobi-us/dotfiles100MIT
jame581/godotprompter100MIT
guohaonan-shy/excalidrawer100MIT
leex852/skills100MIT
Owl-Listener/designer-skills90MIT

Migawka bazy z 13.11.2026 · wersja paczki 66aff2ff8dc7 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100model-the-state-machine
    Modeluje cykl życia, workflow, fazy i pola statusu (zamówienie, zadanie, sesja, dokument, wdrożenie) jako jawne stany, przejścia i warunki zamiast rozproszonych flag logicznych.
  • 100spring-statemachine
    Wspiera budowę automatów skończonych w Spring Statemachine: stany, przejścia, guardy, akcje, persystencja i stany hierarchiczne.
  • 100eng-state-machine
    Modeluje cykl życia encji jako wyraźną maszynę stanów z czystą funkcją decyzji i jedną chronioną ścieżką zapisu, gdy kilka miejsc zapisuje ten sam status.
  • 100stateless
    Wspiera pracę z biblioteką Stateless dla .NET: maszyny stanów z guardami i akcjami, silniki workflow, przetwarzanie zamówień, cykl życia urządzeń i procesy zatwierdzania.
  • 100pattern-state-machine
    Pomaga projektować lub przeglądać procesy o wielu stanach i przejściach, takie jak cykl życia, protokół albo politykę ponawiania.
  • 100state-machine-modeling
    Modeluje stany cyklu życia, przejścia, strażników, zdarzenia, skutki uboczne, stany niepoprawne, ponowienia i niezmienniki dla workflowów lub obiektów domenowych.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.