PHP code example of devespresso / system-life-cycle
1. Go to this page and download the library: Download devespresso/system-life-cycle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
devespresso / system-life-cycle example snippets
// config/systemLifeCycle.php
return [
// Days to keep execution logs
'log_retention_days' => 90,
// Days to keep completed lifecycle model records
'completed_model_retention_days' => 30,
// Column type for model_id in polymorphic relations
// Must be set BEFORE running migrations
// Supported: 'string', 'integer', 'ulid', 'uuid'
'model_id_type' => 'string',
// Total number of execution attempts before a record is marked as failed
'max_attempts' => 3,
// Set to true and populate 'relation_mapping' to use custom morph aliases
'custom_relation_mapping' => false,
'relation_mapping' => [],
// Schedule configuration — set 'enabled' to false to manage commands yourself
'schedule' => [
'enabled' => true,
'run' => [
'frequency' => 'hourly',
'window_in_minutes' => 60, // executes_at lookup window
'stale_after_minutes' => 120, // reset executes_at after this
],
'logs_clean_up' => ['frequency' => 'weekly'],
'completed_models_clean_up' => ['frequency' => 'weekly'],
],
];
use Devespresso\SystemLifeCycle\Traits\EnableSystemLifeCycles;
class User extends Model
{
use EnableSystemLifeCycles;
}
// Attach a lifecycle to the model
// Idempotent — returns the existing record if already enrolled,
// regardless of which stage the model is currently on
$user->addLifeCycleByCode('onboarding');
// Re-enroll from the beginning (resets stage, status, attempts, payload)
// Creates a fresh record if the model was never enrolled
$user->reEnrollLifeCycle('onboarding');
// Get the raw SystemLifeCycleModel record for a lifecycle
$record = $user->getLifeCycleByCode('onboarding');
// $record->status, $record->attempts, $record->payload ...
// Get the stage service instance for the model's current stage
$stage = $user->getLifeCycleStageByCode('onboarding'); // returns ?LifeCycleStageContract
// Manually advance to the next stage (bypasses logging and payload propagation)
$user->setNextLifeCycleStage('onboarding');
// Remove the lifecycle from the model
$user->removeLifeCycle('onboarding');
// Query all lifecycle model records for this model
$user->lifeCycles()->get();
use Devespresso\SystemLifeCycle\SystemLifeCycleService;
class SendWelcomeEmailStage extends SystemLifeCycleService
{
public function handle(): void
{
// $this->model — the Eloquent model being processed
// $this->params — the payload array (read/write between stages)
// $this->systemLifeCycleModel — the raw lifecycle model record
Mail::to($this->model->email)->send(new WelcomeEmail($this->model));
$this->setParam('welcome_sent_at', now()->toDateTimeString());
}
public function shouldContinueToNextStage(): bool
{
// Return false to reschedule this stage for later
return true;
}
}
// When a user registers
$user->addLifeCycleByCode('onboarding');
// Run every 5 minutes instead of hourly
'schedule' => [
'enabled' => true,
'run' => [
'frequency' => 'everyFiveMinutes',
'window_in_minutes' => 5,
'stale_after_minutes' => 10,
],
],
// Disable auto-scheduling to manage commands yourself.
// You must still set window_in_minutes and stale_after_minutes
// to match whatever frequency you schedule the run command at,
// as the query scope and stale reset rely on these values.
'schedule' => [
'enabled' => false,
'run' => [
'window_in_minutes' => 5, // match your custom frequency
'stale_after_minutes' => 10,
],
],
public function shouldContinueToNextStage(): bool
{
return $this->model->payment_verified_at !== null;
}
public function setExecutesAt(): ?Carbon
{
// Check again in 30 minutes
return now()->addMinutes(30);
}