PHP code example of hibernator / hibernator-phplib
1. Go to this page and download the library: Download hibernator/hibernator-phplib 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/ */
hibernator / hibernator-phplib example snippets
// The entire user onboarding flow in one function
public function run(string $email) {
// Step 1: Send welcome
yield WorkflowContext::execute(new SendEmail($email, 'Welcome!'));
// Step 2: Sleep for 3 days (This actually stops CPU usage!)
// The state is saved to the DB. The process exits.
yield WorkflowContext::wait('3 days');
// Step 3: Wake up automatically and continue
// Even if you redeployed your server 50 times in between.
yield WorkflowContext::execute(new SendEmail($email, 'How is it going?'));
// Step 4: Check billing
$isPaid = yield WorkflowContext::execute(new CheckBilling($email));
if ($isPaid) {
yield WorkflowContext::execute(new GrantPremiumAccess($email));
}
}
namespace App\Activity;
use Hibernator\Activity\ActivityInterface;
class SendWelcomeEmail implements ActivityInterface {
public function __construct(public string $email, public string $name) {}
public function handle(): mixed {
// Your logic here (e.g., Mail::send(...))
echo "Sending email to {$this->email}...\n";
return "sent_success";
}
}
namespace App\Workflow;
use Hibernator\Workflow\WorkflowContext;
use App\Activity\SendWelcomeEmail;
use App\Activity\CheckPaymentStatus;
class OnboardingWorkflow {
public function __construct(public string $userId, public string $email) {}
public function run() {
// Step 1: Send Welcome Email
yield WorkflowContext::execute(new SendWelcomeEmail($this->email, 'User'));
// Step 2: Wait for 3 days
yield WorkflowContext::wait('3 days');
// Step 3: Check if they paid
$status = yield WorkflowContext::execute(new CheckPaymentStatus($this->userId));
if ($status === 'paid') {
return "Onboarding Complete";
} else {
return "User Churned";
}
}
}
use Hibernator\Persistence\MySQLWorkflowStore;
use Hibernator\Workflow\Orchestrator;
use App\Workflow\OnboardingWorkflow;
// 1. Setup the Store
$pdo = new PDO('mysql:host=127.0.0.1;dbname=your_db', 'user', 'pass');
$store = new MySQLWorkflowStore($pdo);
// 2. Setup the Orchestrator
$orchestrator = new Orchestrator($store);
// 3. Run the Workflow
$workflowId = 'onboarding-' . uniqid();
$orchestrator->run($workflowId, OnboardingWorkflow::class, [
'userId' => 'user_123',
'email' => '[email protected]'
]);
echo "Workflow started: $workflowId\n";
// worker.php
use Hibernator\Worker\WorkflowWorker;
// ... setup $store and $orchestrator as above ...
$worker = new WorkflowWorker($store, $orchestrator);
echo "Worker started. Press Ctrl+C to stop.\n";
$worker->start(intervalSeconds: 5);