Runner 0.3.0 · Library for PHP
Flows that run, wait and resume
Runner runs flows: blocks of steps that share one context. A step can check a condition, call a PHP function, run another block and come back, jump, or pause. A paused flow picks up later, even in another process, right after the step where it stopped. Use it for orders and approvals, data jobs, or code generators.
Free software (GPL 3) · PHP 8.1 or newer · one file, no dependencies
What a flow is
Blocks of steps, written in YAML
A flow is a YAML file. The first block is where the flow starts. Each step does its work through small PHP files that return a function, and every step reads and writes the same context: a PHP array.
A step can have these keys. They run in this order:
- condition If it is false, the rest of the step is skipped.
- context Values merged into the context.
- call Runs another block, then comes back to this step.
- activity A PHP function. What it returns is kept under the step's name.
- jump Goes to another step and does not come back.
- pause Stops the run. The next run continues after this step.
flows/order.yml
id: order description: Compute an order total, pause once for review, then print a receipt blocks: main: steps: total: activity: compute_total review: call: review.check receipt: activity: format_receipt review: steps: check: condition: needs_review context: reviewed: true pause: true
functions/compute_total.php
<?php declare(strict_types=1); // The returned value is stored in $context["total"] (the step id). return function (array &$context): float { $total = 0.0; foreach ($context["items"] as $item) { $total += $item["price"] * $item["qty"]; } return $total; };
functions/needs_review.php
<?php declare(strict_types=1); return fn (array &$context): bool => $context["total"] >= 20 && empty($context["reviewed"]);
functions/format_receipt.php
<?php declare(strict_types=1); return fn (array &$context): string => sprintf("Total: %.2f%s", $context["total"], empty($context["reviewed"]) ? "" : " (reviewed)");
A real run
Watch it pause, then resume in a new process
Runner 0.3.0 ran this flow twice from its command line, with items of 10 × 2 and 5 × 1. The first run paused for review. The second run, a new PHP process with the saved context, finished the flow. Below is what Runner recorded, event by event. Move with Previous and Next.
- run.start Run 1 starts with the context you give it.
- block.enter Enters block main, the first block.
- step.start Step main.total starts.
- activity.end Activity compute_total returns 25.0, kept as total.
- step.start Step main.review starts.
- call call review.check: runs that block, then comes back here.
- block.enter Enters block review, called.
- step.start Step review.check starts.
- condition Condition needs_review is true, so the step goes on.
- context.update The step sets reviewed = true.
- pause pause: the run stops here. Saved: where it is, and where to come back to (main.review).
- run.end Run 1 ends: paused (exit code 3).
- run.start Run 2 starts, in a new PHP process, from the saved context.
- block.enter Resumes in block review, where it paused.
- step.skip Skips review.check: it ran before the pause.
- call.return Block review ends. Back to main.review.
- block.enter Back in block main.
- step.skip Skips main.total: it ran before the pause.
- step.start Continues step main.review, after its call.
- step.start Step main.receipt starts.
- activity.end Activity format_receipt returns "Total: 25.00 (reviewed)", kept as receipt.
- run.end Run 2 ends: done (exit code 0).
Simulation
Try a flow without running its functions
Since 0.3.0, Runner can simulate a flow. A test double you declare answers each condition and activity, so no function file is loaded. The flow itself runs on Runner's normal engine, with the same calls, jumps and pauses.
Here is an invoice flow. The order is large, so it pauses for approval. Then the first charge is declined: the retry step jumps back to charge, and the second charge goes through.
The real functions would write payments.log and outbox.log. After the simulation, neither file exists. The events match the ones Runner checks in its own tests.
- A double can return a value, give a different answer on each call, fail, or pause.
- Limits on steps, transfers and seconds stop a simulation that would never end.
- Values with names like password or token are hidden in the trace.
{ "description": "Large order: pauses for approval, the first charge is declined, the retry is captured", "doubles": { "orders:validate_order": {"returns": true}, "billing:compute_total": {"returns": 1250.0}, "needs_approval": {"returns": true}, "billing:charge_card": {"sequence": [ {"returns": {"status": "declined"}, "context": {"last_charge": "declined"}}, {"returns": {"status": "captured"}, "context": {"last_charge": "captured"}} ]}, "payment_declined": {"sequence": [{"returns": true}, {"returns": false}]}, "send_receipt": {"returns": "receipt sent"} }, "budget": {"steps": 200, "transfers": 50, "seconds": 5} }
- run.start Run 1 starts with the context you give it.
- block.enter Enters block main, the first block.
- step.start Step main.validated starts.
- activity.end Activity orders:validate_order returns true, kept as validated. A test double gave this answer.
- step.start Step main.total starts.
- activity.end Activity billing:compute_total returns 1250.0, kept as total. A test double gave this answer.
- step.start Step main.approval starts.
- call call approval.check: runs that block, then comes back here.
- block.enter Enters block approval, called.
- step.start Step approval.check starts.
- condition Condition needs_approval is true, so the step goes on.
- context.update The step sets approval_requested = true.
- pause pause: the run stops here. Saved: where it is, and where to come back to (main.approval).
- run.end Run 1 ends: paused (exit code 3).
- run.start Run 2 starts, in a new PHP process, from the saved context.
- block.enter Resumes in block approval, where it paused.
- step.skip Skips approval.check: it ran before the pause.
- call.return Block approval ends. Back to main.approval.
- block.enter Back in block main.
- step.skip Skips main.validated: it ran before the pause.
- step.skip Skips main.total: it ran before the pause.
- step.start Continues step main.approval, after its call.
- step.start Step main.payment starts.
- call call payment.charge: runs that block, then comes back here.
- block.enter Enters block payment, called.
- step.start Step payment.charge starts.
- activity.end Activity billing:charge_card returns {"status": "declined"}, kept as charge. A test double gave this answer.
- step.start Step payment.retry starts.
- condition Condition payment_declined is true, so the step goes on.
- jump jump payment.charge: goes there and does not come back.
- block.enter Enters block payment again, by the jump.
- step.start Step payment.charge starts.
- activity.end Activity billing:charge_card returns {"status": "captured"}, kept as charge. A test double gave this answer.
- step.start Step payment.retry starts.
- condition Condition payment_declined is false, so the rest of the step is skipped.
- call.return Block payment ends. Back to main.payment.
- step.start Step main.receipt starts.
- activity.end Activity send_receipt returns "receipt sent", kept as receipt. A test double gave this answer.
- run.end Run 2 ends: done (exit code 0).
What it is for
Any work that happens in steps
Runner runs the steps and keeps them in order. Your application decides where to keep a paused context and when to run it again: Runner is not a queue or a scheduler.
Business processes
Approvals that wait for a person, payments that try again, orders that move through stages.
Long jobs
Work that stops and continues in another request or process, without repeating the steps that already ran.
Generators
Load a model, check it, render it with Div, check the result. divide's example generator is a Runner flow.
Runner in divide
See the flow, edit it, replay a simulation
divide works on the same YAML file. Its Flow Designer draws the blocks as lanes, the steps as cards, and every call and jump as a line. You edit the flow on the board, and each change is an edit of the YAML.
Runner does the work: divide asks Runner to check, run and simulate the flow. divide adds the view and the editing.
| Task | Runner, the engine | divide, the IDE |
|---|---|---|
| The flow | Reads the YAML and compiles it to PHP. Rejects unknown keys. | Draws it in the Flow Designer and edits the YAML. Shows Runner's verdict. |
| Running | Runs it, pauses it, and resumes it from a saved context. | Ctrl+F9 asks Runner to run it, and keeps the context so you can resume. |
| Simulation | Answers functions with test doubles and records every event. | Shift+F9 asks Runner to simulate, then replays the events on the board, forwards and backwards. |
| Functions | Finds each function file, by name and module. | Lists them, makes new ones, and opens them in the PHP editor. |
| One function, line by line | Not part of Runner. | Function Mock Simulation runs one function's real PHP code, with doubles only for what it asks the context for. It is not a sandbox. |
Under the hood
Small on purpose
One file, one class
No runtime dependencies. Use it from PHP, or from the terminal with the command line it comes with.
YAML becomes PHP
A flow compiles to a real PHP function. You can run it or save its source, which comes out the same every time.
Tested
On PHP 8.1 to 8.5, on every change. It follows the Sequential Flow Model.
Your code, trusted
Flows, conditions and activities are PHP code you trust. Runner does not sandbox them.
Install
Your first paused flow in three steps
A complete example. You need PHP 8.1 or newer and Composer.
1. Create a project with Runner
In an empty folder.
mkdir hello-runner cd hello-runner composer require divengine/runner:0.3.0
2. Add four files
flows/hello.yml:
id: hello blocks: main: steps: greeting: activity: greet wait: pause: true shout: activity: shout
functions/greet.php:
<?php return fn (array &$context): string => "Hello " . $context["name"];
functions/shout.php:
<?php return fn (array &$context): string => strtoupper($context["greeting"]) . "!";
input.json:
{"name": "Ada"}
3. Run, then resume
The first run stops at wait and saves the context:
php vendor/divengine/runner/bin/run.php flows/hello.yml --context input.json --root functions --save-context state.json
paused
The second run continues after it:
php vendor/divengine/runner/bin/run.php flows/hello.yml --context state.json --save-context state.json
done
What the last step left in the context:
php -r 'echo json_decode(file_get_contents("state.json"), true)["shout"], PHP_EOL;'
HELLO ADA!
Prefer an IDE? divide runs, draws and simulates Runner flows. Every example on this page, with the commands to run it again with Runner 0.3.0, is in the site's repository.