Download the PHP package helvetitec/flowengine without Composer
On this page you can find all versions of the php package helvetitec/flowengine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download helvetitec/flowengine
More information about helvetitec/flowengine
Files in helvetitec/flowengine
Package flowengine
Short Description Statemachine bots for conversations in Laravel
License MIT
Informations about the package flowengine
Helvetitec FlowEngine
FlowEngine is a small Laravel package for building stateful, resumable flows such as conversations, onboarding journeys, and application workflows.
A flow receives an input, handles the subject's current state, updates its state or context, and persists when execution stops. Each subject can have multiple independent FlowRun records.
Requirements
- PHP with support for union types (PHP 8.0 or newer)
- A Laravel application
- A database connection for the
flow_runstable
Laravel discovers the package service provider through Composer package discovery.
Installation
Install the package with Composer:
If your application uses Laravel Boost, its boost:install and boost:update
commands automatically include FlowEngine's package-specific AI guidelines.
Publish the migration and run it:
The migration creates flow_runs, including a polymorphic subject, the flow class, current state, JSON context, cooldown, and active flag.
Quick start
Add HasFlowRuns to an Eloquent model that will own flows:
Create a flow by extending FlowEngine. The doRun method is called with the current input. Use the subject and helper methods to implement state transitions:
Start and run the flow from a controller, job, event listener, or webhook:
startFlow creates the run without executing it. This is useful when context must be prepared first:
Pass force: true to run a flow even when its run is inactive or has a future cooldown:
When to use a custom FlowEngine
FlowRun should normally be preferred because it is the default Eloquent implementation and works well for flows owned by a model, such as a Chat. It is not the right fit when a flow has no model owner at all, for example a system-wide import or a temporary session that is not linked to a Chat or another Eloquent model.
In those cases, a custom FlowEngine can run against any application-owned class that implements FlowSubject. The subject below deliberately returns null from getOwner() and persists its state outside flow_runs:
The custom subject owns its lifecycle and persistence; FlowEngine still handles cooldown checks, exceptions, stopping, and the flow helpers. Use this approach only when FlowRun cannot represent the flow's ownership or persistence model. Otherwise, use HasFlowRuns and FlowRun.
Core concepts
Flow states
Every new run starts in the start state. transition changes the state for the next run. transitionAndStop changes the state and persists immediately.
Context
Context is a nullable associative array stored as JSON on the flow_runs row:
Use broadcastContext on a model to merge the same values into every flow run:
Cooldowns and lifecycle
A future cooldown prevents a normal run. The cooldown is cleared automatically once it has expired:
Use stop(persist: false) only when the changes made during the current execution should not be saved. stop() persists by default. Exceptions raised by a flow are wrapped in FlowEngineException with flow and subject context.
API summary
FlowEngine
subject()returns the currentFlowSubject.transition(string $state)changes the next state.set,get,increment,decrement,pull, anddeletemanage context.cooldown(Carbon|CarbonImmutable|null $until)sets the next cooldown.stop(bool $persist = true)ends execution.transitionAndStop(string $state)transitions and persists.pause(Carbon|CarbonImmutable $until)applies a cooldown and persists.deactivate()disables the run and persists.reset(Carbon|CarbonImmutable|null $until = null, bool $deleteContext = false)returns tostart.
HasFlowRuns
flowRuns()returns the model's morph-many relationship.startFlow(string $flowClass)creates or retrieves a run.runFlow(string $flowClass, mixed $input = null, bool $force = false)starts and runs a flow.broadcastContext(array $data)merges context into all runs for the model.
FlowRun
FlowRun is the default Eloquent implementation of FlowSubject. It provides resolveFlow, runFlow, mergeContext, and cleanup helpers:
Rules of thumb
- Implement flow logic in
doRun; do not call it directly. - Stop after handling an input so state and context are persisted.
- Keep flow state in the run's state key and durable, small values in context.
- Use
force: truedeliberately; it bypasses active and cooldown checks. - Keep side effects (messages, notifications, external calls) in the flow's state handlers.
Development
Install dependencies and run the available static analysis command:
License
This package is open-sourced software licensed under the MIT license.