2017 © Pedro Peláez
 

yii2-extension yii2-workflow

A simple workflow engine for Yii2

image

raoul2000/yii2-workflow

A simple workflow engine for Yii2

  • Friday, June 8, 2018
  • by raoul2000
  • Repository
  • 18 Watchers
  • 126 Stars
  • 11,397 Installations
  • PHP
  • 19 Dependents
  • 0 Suggesters
  • 30 Forks
  • 1 Open issues
  • 15 Versions
  • 12 % Grown

The README.md

yii2-workflow

Build Latest Stable Version Total Downloads License, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist raoul2000/yii2-workflow "*"

or add, (*4)

"raoul2000/yii2-workflow": "*"

to the require section of your composer.json file., (*5)

Quick Start

Configuration

For this "Quick start Guide" we will be using default configuration settings, but remember that yii2-workflow is designed to be highly flexible so to adapt to a lot of execution contexts... well at least that was my goal., (*6)

Create A Workflow

A workflow is defined as a PHP class that implements the \raoul2000\workflow\source\file\IWorkflowDefinitionProvider interface. which declares the getDefinition() method. This method must return an array representing the workflow definition., (*7)

Let's define a very simple workflow that will be used to manage posts in a basic blog system., (*8)

, (*9)

Here is the PHP class that implements the definition for our workflow :, (*10)

@app/models/PostWorkflow.php, (*11)

namespace app\models;

class PostWorkflow implements \raoul2000\workflow\source\file\IWorkflowDefinitionProvider
{
    public function getDefinition() {
        return [
            'initialStatusId' => 'draft',
            'status' => [
                'draft' => [
                    'transition' => ['publish','deleted']
                ],
                'publish' => [
                    'transition' => ['draft','deleted']
                ],
                'deleted' => [
                    'transition' => ['draft']
                ]
            ]
        ];
    }
}

Attach To The Model

Now let's have a look to our Post model: we store the status of a post in a column named status of type STRING(40)., (*12)

The last step is to associate the workflow definition with posts models. To do so we must declare the SimpleWorkflowBehavior behavior in the Post model class and let the default configuration settings do the rest., (*13)

@app/models/Post.php, (*14)

namespace app\models;
/**
 * @property integer $id
 * @property string $title
 * @property string $body
 * @property string $status column used to store the status of the post
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            \raoul2000\workflow\base\SimpleWorkflowBehavior::className()
        ];
    }
    // ...

That's it ! We are ready to play with SimpleWorkflowBehavior., (*15)

Use It !

Now that we are all setup, we can use the SimpleWorkflowBehavior methods to set/get the status of our posts : the SimpleWorkflowBehavior will take care that the post doesn't reach a status where it is not supposed to go to, depending on the workflow definition that we have provided., (*16)

$post = new Post();
$post->status = 'draft';
$post->save();
echo 'post status is : '. $post->workflowStatus->label;

This will print the following message :, (*17)

post status is : Draft

If you do the same thing but instead of draft set the status to publish and try to save it, the following exception is thrown :, (*18)

Not an initial status : PostWorkflow/publish ("PostWorkflow/draft" expected)

That's because in your workflow definition the initial status is set to draft and not publish., (*19)

Ok, one more example for the fun ! This time we are not going to perform the transition when the Post is saved (like we did in the previous example), but immediately, by invoking the sendToStatus method. Our Post is going to try to reach status publish passing through deleted which is strictly forbidden by the workflow. Will it be successful in this risky attempt to break workflow rules ?, (*20)

$post = new Post();
$post->sendToStatus('draft');
$post->sendToStatus('deleted');
$post->sendToStatus('publish'); // danger zone !

Game Over ! There is no transition between deleted and publish, and that's what SimpleWorkflow tries to explain to our fearless post object., (*21)

Workflow Exception – raoul2000\workflow\base\WorkflowException
No transition found between status PostWorkflow/deleted and PostWorkflow/publish

Yes, that's severe, but there was many ways to avoid this exception like for instance by first validating that the transition was possible., (*22)

What's Next ?

This is just one way of using the SimpleWorkflowBehavior but there's much more and hopefully enough to assist you in workflow management inside your Yii2 web app., (*23)

You will find additional information there :, (*24)

You may also be interested in the following projects developed around yii2-workflow :, (*25)

License

yii2-workflow is released under the BSD 3-Clause License. See the bundled LICENSE.md for details., (*26)

Yii2, (*27)

The Versions

04/03 2018
03/10 2015

0.0.20

0.0.20.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

23/08 2015

0.0.19

0.0.19.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

22/08 2015

0.0.18

0.0.18.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

12/07 2015

0.0.17

0.0.17.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

03/07 2015

0.0.14

0.0.14.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

06/06 2015

0.0.12

0.0.12.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

31/05 2015

0.0.11

0.0.11.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow

20/04 2015

0.0.8

0.0.8.0 http://www.yiiframework.com/

A simple workflow engine for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 workflow