phil / story-graph
Write branching lessons and gamebooks as checked graphs: choices gated by rules, variables, once-only and fallback choices, readings to store, Twee 3.
Requires
- php: >=8.5
- phil/rules: ^0.1@dev
Requires (Dev)
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 02:53:07 UTC
README
Write branching lessons and gamebooks as checked graphs: choices gated by rules, variables, once-only and fallback choices, readings to store, Twee 3.
phil/story-graph is a plain PHP library. A story is a graph of nodes — passages left by their choices, and endings — that a reader moves through one choice at a time. A branching lesson in a course, a scenario that grades a decision, a gamebook: each is a story, and the library keeps where each reader stands, which choices are open to them, and whether the graph holds a dead end before anyone reads it.
- Choices gated by rules: a choice may hold a condition, a rule of
phil/rulesread against the reading — its variables, how many times each node was entered — and against facts the application adds, such as a quiz score. A locked choice is hidden, or shown with what remains to be done. - Variables and effects: a story declares integer, boolean and text variables; a choice or a node sets them or adds to them when it is taken or entered. Integers saturate at the bound of a JSON number.
- Once-only and fallback choices: a choice may be taken once per reading; a fallback is open only when no other choice is, so a passage whose choices are all locked or used still leads somewhere.
- Graded endings: an ending may name an outcome, such as
passedorfailed, that a course reads. - A checker before publishing: dead ends, nodes never reached, traps with no way to an ending, passages whose choices may all be closed, conditions reading undeclared variables, and loops, in time linear in the graph.
- Readings as values: a reading is immutable and holds no identifier of a person; it encodes to versioned JSON to store between two requests, and a reading begun on an earlier revision of the story is adopted, not misread.
- Formats: the story's own versioned JSON form, lossless and fingerprinted; Twee 3, the text form of Twine, read and written for text, links and tags; JSON Canvas 1.0, to look at the map of a story in a canvas editor.
- Safe on untrusted input: every reader is strict and bounded by
Limits, no condition runs code, the macros of story formats are kept as text and never run, and the services keep no state between calls.
Status
| Part | State |
|---|---|
| Stories, nodes, choices, effects and their checks | Implemented and tested |
| The navigator: start, open choices, choose, adopt a reading | Implemented and tested |
| The graph checker | Implemented and tested |
| The JSON form of stories and readings | Implemented, tested and fuzzed |
| Twee 3, read and written | Implemented, tested and fuzzed |
| The JSON Canvas map | Implemented and tested |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements, installing, a first story. |
| Stories and readings | Nodes, choices, conditions, variables, effects, the navigator and revisions. |
| Checking a story | What the graph checker finds, and how severe each finding is. |
| Formats | The JSON form, stored readings, Twee 3 and JSON Canvas. |
| Extension points | Condition types, the application's facts, the limits, what is stable. |
| Testing | The tests, the fuzz targets and the benchmark. |
| Reference | Every public type, with its role. |
Quick start
composer require phil/story-graph
use Phil\Rules\Fact\Facts; use Phil\Rules\Type\ConditionTypes; use Phil\StoryGraph\Json\ReadingCodec; use Phil\StoryGraph\Json\StoryReader; use Phil\StoryGraph\Navigator; $story = new StoryReader(ConditionTypes::builtIn())->fromJson($storedStory); $navigator = new Navigator(); $codec = new ReadingCodec(); $reading = $navigator->start($story); $facts = Facts::of(['quiz.score' => $score]); // what the application knows besides the reading foreach ($navigator->choices($story, $reading, $facts) as $option) { // $option->choice->label, $option->open; a locked choice: $option->result->explanation() } $reading = $navigator->choose($story, $reading, 'read', $facts); $navigator->current($story, $reading)->body; // the author's text, escaped by the page that shows it $stored = $codec->encode($reading); // kept beside the learner's identifier
Requirements
PHP 8.5 or later, and phil/rules.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.