Skip to content
Mostafa Barmshory edited this page Feb 22, 2017 · 3 revisions

The Workflow component provides tools for managing a workflow or finite state machine.

Installation

You can install the component in 2 different ways:

Then, require the vendor/autoload.php file to enable the autoloading mechanism provided by Composer. Otherwise, your application won't be able to find the classes of this Pluf component.

Creating a Workflow

The workflow component gives you an object oriented way to define a process or a life cycle that your Pluf object goes through. Each step or stage in the process is called a place. You do also define transitions that describe the action to get from one place to another.

state machine

A set of places and transitions creates a definition. A workflow needs a Definition and a way to write the states to the objects (i.e. an instance of a Pluf_Model).

Consider the following example for a blog post. A post can have one of a number of predefined statuses (draft, review, rejected, published). In a workflow, these statuses are called states. You can define the workflow like this:

$defenition = array(
        'draft' => array(....),
        'review' => array(....),
        'rejected' => array(....),
        'published' => array(....)
);

where each states are list of transaction. For example

array(
        'rejectPost' => array(
                'next' => 'check',
                'preconditions' => array(
                        'Pluf_Condition_Class::checkMethod'
                ),
                'action' => array(
                        'Pluf_View_Class',
                        'method'
                )
        ),
        'plublishPost' => array( ... )
)

The Workflow can now help you to decide what actions are allowed on a blog post depending on what place it is in. This will keep your domain logic in one place and not spread all over your application.

When you define multiple workflows you should consider using a Registry, which is an object that stores and provides access to different workflows. A registry will also help you to decide if a workflow supports the object you are trying to use it with.

Usage

When you have configured a Registry with your workflows, you may use it as follows:

    $definition = array(...);
    $machine = new Workflow_Machine();
    $machine
        ->setStates($definition)
        ->setProperty('state')
        ->transact($request, $object, 'rejectPost');

Learn more