PHP code example of solution-forest / workflow-engine-core
1. Go to this page and download the library: Download solution-forest/workflow-engine-core 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/ */
solution-forest / workflow-engine-core example snippets
use SolutionForest\WorkflowEngine\Core\WorkflowBuilder;
use SolutionForest\WorkflowEngine\Core\WorkflowEngine;
use SolutionForest\WorkflowEngine\Core\WorkflowContext;
use SolutionForest\WorkflowEngine\Actions\BaseAction;
// Define custom actions
class ValidateOrderAction extends BaseAction
{
public function execute(WorkflowContext $context): ActionResult
{
$orderId = $context->getData('order_id');
// Your validation logic here
if ($this->isValidOrder($orderId)) {
return ActionResult::success(['validated' => true]);
}
return ActionResult::failure('Invalid order');
}
}
// Create a workflow
$workflow = WorkflowBuilder::create('order-processing')
->addStep('validate', ValidateOrderAction::class)
->addStep('payment', ProcessPaymentAction::class)
->addStep('fulfillment', FulfillOrderAction::class)
->addTransition('validate', 'payment')
->addTransition('payment', 'fulfillment')
->build();
// Execute the workflow
$engine = new WorkflowEngine();
$context = new WorkflowContext(
workflowId: 'order-processing',
stepId: 'validate',
data: ['order_id' => 123, 'customer_id' => 456]
);
$instance = $engine->start($workflow, $context);
$result = $engine->executeStep($instance, $context);
use SolutionForest\WorkflowEngine\Attributes\Condition;
class ConditionalAction extends BaseAction
{
#[Condition("data.amount > 1000")]
public function execute(WorkflowContext $context): ActionResult
{
// This action only executes if amount > 1000
return ActionResult::success();
}
}
use SolutionForest\WorkflowEngine\Attributes\Retry;
class ReliableAction extends BaseAction
{
#[Retry(maxAttempts: 3, delay: 1000)]
public function execute(WorkflowContext $context): ActionResult
{
// This action will retry up to 3 times with 1 second delay
return ActionResult::success();
}
}
use SolutionForest\WorkflowEngine\Attributes\Timeout;
class TimedAction extends BaseAction
{
#[Timeout(seconds: 30)]
public function execute(WorkflowContext $context): ActionResult
{
// This action will timeout after 30 seconds
return ActionResult::success();
}
}
use SolutionForest\WorkflowEngine\Contracts\StorageAdapter;
class CustomStorageAdapter implements StorageAdapter
{
public function save(WorkflowInstance $instance): void
{
// Save workflow instance to your storage
}
public function load(string $instanceId): ?WorkflowInstance
{
// Load workflow instance from your storage
}
public function delete(string $instanceId): void
{
// Delete workflow instance from your storage
}
}
use SolutionForest\WorkflowEngine\Contracts\EventDispatcher;
class CustomEventDispatcher implements EventDispatcher
{
public function dispatch(object $event): void
{
// Handle workflow events
match (get_class($event)) {
'SolutionForest\WorkflowEngine\Events\WorkflowStarted' => $this->onWorkflowStarted($event),
'SolutionForest\WorkflowEngine\Events\StepCompletedEvent' => $this->onStepCompleted($event),
'SolutionForest\WorkflowEngine\Events\WorkflowCompletedEvent' => $this->onWorkflowCompleted($event),
default => null,
};
}
}
use SolutionForest\WorkflowEngine\Contracts\Logger;
class CustomLogger implements Logger
{
public function info(string $message, array $context = []): void
{
// Log info messages
}
public function error(string $message, array $context = []): void
{
// Log error messages
}
public function warning(string $message, array $context = []): void
{
// Log warning messages
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.