Workflow

From struts4php

Jump to: navigation, search

codelc4 lialchilic The workflow engine provides a powerful framework for implementing XML based workflows in your PHP application.

Contents

The WorkflowExecutor

Overview

The WorkflowExecutor is the controller for the workflow. With the initialize() method of the WorkflowExecutor you can add as many WorkflowAction and WorkflowActivity objects as necessary for your workflow. It is possible to stop the workflow in each WorkflowAction and WorkflowActivity.

Implementing your WorkflowExecutor

Example for the implementation of a WorkflowExecutor:

class LoginWorkflowExecutor extends AbstractWorkflowExecutor {
 
    public function __construct() {
        AbstractWorkflowExecutor::__construct();
    }
 
    public function __destruct() {
        AbstractWorkflowExecutor::__destruct();
    }    
 
    public function initialize() {
        self::addAction(new LoginWorkflowAction());
    }
}

The WorkflowData

Overview

The WorkflowData is the container for storing data while running the workflow.

Implementing your WorkflowData

Example for the implementation of a WorkflowData:

class LoginWorkflowData implements WorkflowData {
 
    private $valid = false;
 
    public function __construct($username, $password) {
        $this->username = $username;
        $this->password = $password;
    }
 
    public function getUsername() {
        return $this->username;
    }
 
    public function getPassword() {
        return $this->password;
    }
 
    public function setValid() {
        $this->valid = true;
    }
 
    public function isValid() {
        return $valid;
    }
}

WorkflowAction or WorkflowActivity

A WorkflowAction has only one execute() method, that is invoked by the WorkflowExecutor 's own execute() method. While invoking the execute() method, the WorkflowData is passed to the WorkflowAction as parameter.

The WorkflowActivity has an additional evaluate() method, that is also automatically invoked by the WorkflowExecutor. The evaluate() method is always invoked before the execute() method. Depending on it's return value the WorkflowExecutor invoke's the execute() method or not.

Implementation of a WorkflowAction

Example for the implementation of a WorkflowAction:

class LoginWorkflowAction implements WorkflowAction {
 
    public function execute(WorkflowData $data) {
        $username = $data->getUsername();
        $password = $data->getPassword();
        if(empty($username) && empty($password)) {
            $data->setValid();
        }
    }
}

Implementation of a WorkflowActivity

Example for the implementation of a WorkflowActivity:

class LoginWorkflowActivity implements WorkflowActivity {
 
    const USERNAME = "foo";
    const PASSWORD = "bar";
 
    private $username = null;
    private $password = null;
 
    public function evaluate(WorkflowData $data) {
        $this->username = $data->getUsername();
        $this->password = $data->getPassword();
        if(!empty($this->username) && 
           !empty($this->password)) {
            return true;
        }
        return false;
    }
 
    public function execute(WorkflowData $data) {
        if($this->username == self::USERNAME &&
           $this->password == self::PASSWORD) {
            $data->setValid();
        }
    }
}

Usage of the workflow engine

Example for the usage of the workflow engine:

$data = new LoginWorkflowData($_REQUEST["username"], $_REQUEST["password"]);
$executor = new LoginWorkflowExecutor();
$executor->execute($data);
 
if($data->isValid()) {
    echo "Login succeeded";
} else {
    echo "Credentials invalid";
}
Personal tools