EZComponents – ezworkflow Example

Ezcomponents are silently growing PHP Components Library. It contains many libraries with it. I’m gonna use one of the components called WorkFlow.

Workflow components are useful for creating business logics and manage business process easily. Here I’m gonna show you how to use this component step by step.

1. Firstly, Download the entire Components Library from here : http://ezcomponents.org/download.

2. Then, All you need to do is Unzip and upload it in your Server or Localhost!.

3. After you upload all the files on the server, Find a folder called “WorkflowDatabaseTiein”.

4. Inside the folder, Go to “design” and open the file called “schema_mysql.sql”.

5. Now open your PhpMyAdmin and create a database and name it as “ezflow”.

6. After creating the Database, import the “schema_mysql.sql” in the database “ezflow”.

7. You will see there 6 tables created in the database, they are

i.      execution
ii.    execution_state
iii.   node
iv.   node_connection
v .  variable_handler
vi.   workflow

Here the piece of code going to do the magic of WorkFlow, create this file in the root folder of ezcomponents.


<?php

// include and register the autoloader
require_once 'Base/src/base.php';
function __autoload($className)
{
ezcBase::autoload($className);
}

$workflow = new ezcWorkflow('AGR');
$getName = new ezcWorkflowNodeInput(array(
'name' => new ezcWorkflowConditionIsString()
));

$sayHello = new ezcWorkflowNodeAction(array('class' => 'myServiceObject'));

$workflow->startNode->addoutNode($getName);
$getName->addOutNode($sayHello);
$sayHello->addoutNode($workflow->endNode);

// Set up database connection.
$db = ezcDbFactory::create( 'mysql://root@localhost/ezflow' );
// Set up workflow definition storage (database).
$definition = new ezcWorkflowDatabaseDefinitionStorage( $db );
// Save workflow definition to database.
$definition->save( $workflow );

$execution = new ezcWorkflowDatabaseExecution($db);
$execution->workflow = $workflow;
$execution->start();

if ($execution->hasEnded())
echo "Execution has ended.n";
else
echo "Execution has NOT ended?n";

?>

Line no. 22 : You need to give your username, password and the hostname to run it properly

$db = ezcDbFactory::create( ‘mysql://username:password@hostname/database’ );

for instance,  $db = ezcDbFactory::create( ‘mysql://root:root123@localhost/ezflow’ );


Run the above Program in a browser. This will create a workflow and store it in the database and also one execution will be created in the database. Check the Tables, Workflow, execution, execution_state to know what is happening.

Actually, the above program started an execution at the moment, and it is waiting for the input to end the execution. This is really useful to do the post programming.

This execution is stored in the database, so whenever we need to resume the execution may be after 1 week or 1 month we can do it by resuming it. Let see the below code to do the magic.


<?php
require_once 'Base/src/base.php';
function __autoload($className)
{
ezcBase::autoload($className);
}

$db = ezcDbFactory::create( 'mysql://root@localhost/ezflow' );
$execution = new ezcWorkflowDatabaseExecution( $db, 1);

class myServiceObject implements ezcWorkflowServiceObject
{
public function execute(ezcWorkflowExecution $execution)
{
$name = $execution->getVariable('name');
echo "Hello {$name}!n";
}

public function __toString()
{
return __CLASS__;
}
}

$execution->resume(array('name' => 'Superman'));

if ($execution->hasEnded())
echo "Execution has ended.n";
else
echo "Execution has NOT ended?n";

?>

The above code will resume the progress. All you need to do is give the “execution_id” at the line no. 9

$execution = new ezcWorkflowDatabaseExecution( $db, 1);

Find the execution id in the table execution and give it in the above line (here it is ‘1’).

If you run this program. This will give the result “Hello Superman” and Execution Ended.

This is how the workflow is happening. Try and tell me what else is missing in this post!

8 Comments

Add a Comment

Your email address will not be published. Required fields are marked *