boson-php/runtime
Lightweight runtime for boson-php that helps bootstrap and manage execution of Boson-based apps. Provides core runtime utilities and integration points for running, configuring, and packaging projects with minimal overhead.
Installation Add the package via Composer:
composer require boson-php/runtime
No additional configuration is required for basic usage.
First Use Case: Runtime Execution The package provides a lightweight runtime environment for executing Boson workflows (a state machine/DSL for business logic). Start with a simple workflow definition:
use Boson\Runtime\Runtime;
$runtime = new Runtime();
$result = $runtime->run('workflow_id', ['input' => 'data']);
Where to Look First
Boson\Runtime\Runtime for execution logic.resources/workflows/example.json) and load them dynamically.Dynamic Workflow Loading Load workflows from files or databases:
$workflow = json_decode(file_get_contents('path/to/workflow.json'), true);
$runtime->loadWorkflow('workflow_id', $workflow);
Input/Output Handling Pass structured input and capture outputs:
$output = $runtime->run('workflow_id', [
'user_id' => 123,
'action' => 'process_order'
]);
Error Handling Wrap runtime calls in try-catch:
try {
$runtime->run('workflow_id', $input);
} catch (\Boson\Runtime\Exception\WorkflowException $e) {
Log::error("Workflow failed: " . $e->getMessage());
}
Service Provider Binding
Bind the runtime as a singleton in AppServiceProvider:
$this->app->singleton(Runtime::class, function ($app) {
return new Runtime();
});
Artisan Commands Create a command to trigger workflows:
use Boson\Runtime\Runtime;
class RunWorkflowCommand extends Command {
protected $signature = 'workflow:run {id} {--input=}';
public function handle(Runtime $runtime) {
$input = json_decode($this->option('input'), true);
$result = $runtime->run($this->argument('id'), $input);
$this->info($result);
}
}
Event Listeners Hook into workflow completion:
$runtime->on('workflow_completed', function ($workflowId, $output) {
event(new WorkflowCompleted($workflowId, $output));
});
Workflow Validation
WorkflowException.$validator = new \Boson\Runtime\Validator();
if (!$validator->validate($workflow)) {
throw new \InvalidArgumentException("Invalid workflow");
}
State Management
Runtime to support stateful workflows:
$runtime->setStateStore(new RedisStateStore());
Performance
Enable Verbose Logging Set the runtime log level:
$runtime->setLogLevel(\Monolog\Logger::DEBUG);
Inspect Workflow Steps
Use Runtime::getDebugInfo() to trace execution:
$debugInfo = $runtime->getDebugInfo();
dd($debugInfo['steps']); // Array of executed steps
Custom Actions Register custom actions (e.g., database queries):
$runtime->registerAction('custom_action', function ($input) {
return Model::where('id', $input['id'])->first();
});
Middleware Add pre/post-processing middleware:
$runtime->pushMiddleware(function ($workflow, $input, $next) {
$input['timestamp'] = now()->toDateTimeString();
return $next($workflow, $input);
});
Plugin System
The package is designed for extension. Fork and modify Runtime to add:
How can I help you explore Laravel packages today?