yii2-workflow Class Reference
  • Namespace
  • Class

Namespaces

  • raoul2000
    • workflow
      • actions
      • base
      • events
      • helpers
      • source
        • file
      • validation

Classes

  • SimpleWorkflowBehavior
  • Status
  • StatusIdConverter
  • Transition
  • Workflow
  • WorkflowBaseObject
  • WorkflowException
  • WorkflowValidationException

Interfaces

  • IStatusAccessor
  • IStatusIdConverter
  • StatusInterface
  • TransitionInterface
  • WorkflowInterface

Class SimpleWorkflowBehavior

SimpleWorkflowBehavior implements the behavior of a model evolving inside a Simple Workflow.

To use SimpleWorkflowBehavior with the default parameters, simply attach it to the model class like you would do for any standard Yii2 behavior.

use raoul2000\workflow\base\SimpleWorkflowBehavior;

public function behaviors()
{
    return [
        'simpleWorkflow' => [
            'class' => SimpleWorkflowBehavior::className()
        ],
    ];
}

To learn more about Yii2 behaviors refer to the Yii2 Definitive Guide

You can customize the SimpleWorkflowBehavior with the following parameters :

  • statusAttribute : name of the attribute that is used by the owner model to hold the status value. The default value is "status".
  • defaultWorkflowId : identifier of the default workflow for the owner model. If no value is provided, the behavior creates a default workflow identifier (see [[getDefaultWorkflowId]]).
  • source : name of the Workflow Source Component that the behavior uses to read the workflow definition. By default the component id "workflowSource" is used. If it is not already available in the current application it is created by the behavior using the default workflow source component class.

Below is an example behavior initialization :

use raoul2000\workflow\base\SimpleWorkflowBehavior;

public function behaviors()
{
    return [
        'simpleWorkflow' => [
            'class' => SimpleWorkflowBehavior::className(),
            'statusAttribute' => 'col_status',
            'defaultWorkflowId' => 'MyWorkflow',
            'source' => 'myWorkflowSource',
        ],
    ];
}
yii\base\Behavior
Extended by raoul2000\workflow\base\SimpleWorkflowBehavior
Namespace: raoul2000\workflow\base
Located at base/SimpleWorkflowBehavior.php

Methods summary

public
# __construct( array $config = [] )

Instance constructor.

Instance constructor.

Parameters

$config
public
# init( )

Initialize the behavior.

Initialize the behavior.

At initialization time, following actions are performed :

  • get a reference to the workflow source component. If it doesn't exist, it is created.
  • get a reference to the event model component or create it if not configured.
public
# attach( $owner )

Attaches the behavior to the model.

Attaches the behavior to the model.

The operation is successful if the owner component has the appropriate attribute or property to store the workflow status value. The name of this attribute or property is set to 'status' by default, but can be configured using the statusAttribute configuration parameter at construction time.

Note that using a property instead of a model attribute to store the status value is not recomended as it is then the developer responsability to ensure that the workflow operations are consistent, in particular regarding persistence.

If previous requirements are met, the internal status value is initialized.

See

http://www.yiiframework.com/doc-2.0/yii-base-behavior.html#attach()-detail
raoul2000\workflow\base\SimpleWorkflowBehavior::initStatus()
public
# events( )

Install events handlers.

Install events handlers.

Following event are used :

  • ActiveRecord::EVENT_AFTER_FIND
  • ActiveRecord::EVENT_BEFORE_INSERT
  • ActiveRecord::EVENT_BEFORE_UPDATE
  • ActiveRecord::EVENT_AFTER_UPDATE
  • ActiveRecord::EVENT_AFTER_INSERT
  • ActiveRecord::EVENT_AFTER_DELETE
public
# initStatus( )

Initialize the internal status value based on the owner model status attribute.

Initialize the internal status value based on the owner model status attribute.

The status attribute belonging to the owner model is retrieved and if not empty, converted into the corresponding Status object instance. This method does not trigger any event, it is only restoring the model into its workflow. It is invoked when the behavior is attached to the model, and on AFTER_FIND event.

Throws

raoul2000\workflow\base\WorkflowException
if the status attribute could not be converted into a Status object
public boolean
# enterWorkflow( string $workflowId = null )

Puts the owner model into the workflow $workflowId or into its default workflow if no argument is provided. If the owner model is already in a workflow, an exception is thrown. If this method ends with no error, the owner model's status is the initial status of the selected workflow.

Puts the owner model into the workflow $workflowId or into its default workflow if no argument is provided. If the owner model is already in a workflow, an exception is thrown. If this method ends with no error, the owner model's status is the initial status of the selected workflow.

Parameters

$workflowId

the ID of the workflow the owner model must be inserted to or NULL to insert into the model's default workflow.

Returns

boolean
TRUE if the operation succeeded, FALSE otherwise

Throws

raoul2000\workflow\base\WorkflowException
public
# afterSaveStatus( yii\base\Event $event )

After the owner model has been saved, fire pending events. This is the event handler for both ActiveRecord::EVENT_AFTER_UPDATE and ActiveRecord::EVENT_AFTER_INSERT events

After the owner model has been saved, fire pending events. This is the event handler for both ActiveRecord::EVENT_AFTER_UPDATE and ActiveRecord::EVENT_AFTER_INSERT events

Parameters

$event
public
# beforeSaveStatus( yii\base\Event $event )

Send owner model into status if needed. This is the event handler for both ActiveRecord::EVENT_BEFORE_INSERT and ActiveRecord::EVENT_BEFORE_UPDATE events

Send owner model into status if needed. This is the event handler for both ActiveRecord::EVENT_BEFORE_INSERT and ActiveRecord::EVENT_BEFORE_UPDATE events

Parameters

$event
public
# beforeDelete( yii\base\Event $event )

Handle the case where the owner model is leaving the workflow. This is the event handler for ActiveRecord::EVENT_BEFORE_DELETE

Handle the case where the owner model is leaving the workflow. This is the event handler for ActiveRecord::EVENT_BEFORE_DELETE

Parameters

$event
public
# afterDelete( yii\base\Event $event )

Fires pending events, once the owner model has been successfully deleted. This is the event handler for ActiveRecord::EVENT_AFTER_DELETE

Fires pending events, once the owner model has been successfully deleted. This is the event handler for ActiveRecord::EVENT_AFTER_DELETE

Parameters

$event
public boolean
# sendToStatus( raoul2000\workflow\base\Status|string $status )

Send the owner model into the status passed as argument.

Send the owner model into the status passed as argument.

If the transition between the current status and $status can be performed, the status attribute in the owner model is updated with the value of the new status, otherwise it is not changed. This method can be invoked directly but you should keep in mind that it does not handle status persistance.

Parameters

$status

the destination status to reach. If NULL, then the owner model is going to leave its current workflow.

Returns

boolean
TRUE if the transition could be performed, FALSE otherwise
public raoul2000\workflow\events\WorkflowEvent[]
# getEventSequence( string $status )

Creates and returns the list of events that will be fire when the owner model is sent from its current status to the one passed as argument.

Creates and returns the list of events that will be fire when the owner model is sent from its current status to the one passed as argument.

The event list returned by this method depends on the event sequence component that was configured for this behavior at construction time.

Parameters

$status
the target status

Returns

raoul2000\workflow\events\WorkflowEvent[]
The list of events
public string[]
# getScenarioSequence( string $status )

Creates and returns the list of scenario names that will be used to validate the owner model when it is sent from its current status to the one passed as argument.

Creates and returns the list of scenario names that will be used to validate the owner model when it is sent from its current status to the one passed as argument.

Parameters

$status
the target status

Returns

string[]
list of scenario names
public array
# createTransitionItems( mixed $status, boolean $WithScenarioNames, boolean $withEventSequence )

Creates and returns workflow event sequence and/or scenario for the pending transition.

Creates and returns workflow event sequence and/or scenario for the pending transition.

Being given the current status and the status value passed as argument this method returns the corresponding event sequence and the scenario names.

The returned array contains up to 3 elements : - index = 0 : the Status instance corresponding to the $status passed as argument - index = 1 : an array of [[\raoul2000\workflow\events\WorkflowEvent]] instances (the event sequence) that may contain no element if no event sequence component is configured or if event sequence are not requested ($withEventSequence = false) - index = 2 : an array of scenario names (string) that may be empty if scenario names are not requested ($WithScenarioNames= false)

Parameters

$status
a status Id or a Status instance considered as the target status to reach
$WithScenarioNames
When TRUE scenario names are requested, FALSE otherwise
$withEventSequence
When TRUE the event sequence is requested, FALSE otherwise

Returns

array
Three elements : Status intance, scenario names, event sequence.

Throws

raoul2000\workflow\base\WorkflowException
public array
# getNextStatuses( $validate = false, $beforeEvents = false )

Returns all status that can be reached from the current status.

Returns all status that can be reached from the current status.

The list of reachable statuses is returned as an array where keys are status ids and value is an associative array that contains at least the status instance. By default, no validation is performed and no event is fired by this method, however you may use $validate and $beforeEvents argument to enable them.

When $validate is true, the model is validated for each scenario and for each possible transition. When $beforeEvents is true, all "before" events are fired and if a handler is attached it is executed.

Each entry of the returned array has the following structure :

[
    targetStatusId => [
        'status' => the status instance
        // the 'validation' key is present only if $validate is true
        'validation' => [
            0 => [
                 'scenario' => scenario name
                'success' => true (validation success) | false (validation failure) | null (no validation for this scenario)
            ],
            1 => [ ... ]
         ],
         // the 'event' key is present only if $beforeEvent is TRUE
         'event' => [
            0 => [
                'name' => event name
                'success' => true (event handler success) | false (event handler failed : the event has been invalidated) | null (no event handler)
            ]
            1 => [...]
        ],
        // if $validate is true or if $beforeEvent is TRUE
       'isValid' => true   (being given the verifications that were done, the target status can be reached)
          | false (being given the verifications that were done, the target status cannot be reached)
    ],
     anotherStatusID => [ ...]
]

If the owner model is not currently in a workflow, this method returns the initial status of its default workflow for the model.

Returns

array
list of status

Throws

raoul2000\workflow\base\WorkflowException
public string
# getDefaultWorkflowId( )

Returns the id of the default workflow associated with the owner model.

Returns the id of the default workflow associated with the owner model.

If no default workflow id has been configured, it is created by using the shortname of the owner model class (i.e. the class name without the namespace part), suffixed with 'Workflow'.

For instance, class app\model\Post has a default workflow id equals to PostWorkflow.

Returns

string
id for the workflow the owner model is in.
public raoul2000\workflow\source\IWorkflowSource
# getWorkflowSource( )

Returns the Workflow Source Component used by this behavior. This component is initialized by the [[$source]] configuration property. If not configured, the behavior creates and register its own workflow source component.

Returns the Workflow Source Component used by this behavior. This component is initialized by the [[$source]] configuration property. If not configured, the behavior creates and register its own workflow source component.

Returns

raoul2000\workflow\source\IWorkflowSource
the workflow source component instance used by this behavior
public null|raoul2000\workflow\base\IStatusAccessor
# getStatusAccessor( )

Returns the status accessor instance used by this behavior or NULL if no status accessor is used. This component is initialized by the [[$statusAccessor]] configuration property.

Returns the status accessor instance used by this behavior or NULL if no status accessor is used. This component is initialized by the [[$statusAccessor]] configuration property.

Returns

null|raoul2000\workflow\base\IStatusAccessor

the status accessor component used bu the behavior or NULL if not configured (default)

Throws

yii\base\InvalidConfigException
public null|raoul2000\workflow\base\IStatusIdConverter
# getStatusConverter( )

Returns the status converter instance used by this behavior or NULL if no status converter is used. This component is initialized by the [[$statusConverter]] configuration property.

Returns the status converter instance used by this behavior or NULL if no status converter is used. This component is initialized by the [[$statusConverter]] configuration property.

Returns

null|raoul2000\workflow\base\IStatusIdConverter

The status Id Converter used by the behavior or NULL if not configured (default)

Throws

yii\base\InvalidConfigException
public null|raoul2000\workflow\base\Status
# getWorkflowStatus( )

Returns the current Status instance.

Returns the current Status instance.

Returns

null|raoul2000\workflow\base\Status
The status instance or NULL if the model is not in a workflow

See

raoul2000\workflow\base\SimpleWorkflowBehavior::hasWorkflowStatus()
public null|raoul2000\workflow\base\Workflow
# getWorkflow( )

Returns the current Workflow instance the model is in or NULL if the model is not in a workflow.

Returns the current Workflow instance the model is in or NULL if the model is not in a workflow.

Returns

null|raoul2000\workflow\base\Workflow

the workflow the owner model is currently in, or null if the owner model is not in a workflow

public boolean
# hasWorkflowStatus( )

Returns a value indicating whether the owner model is currently in a workflow or not.

Returns a value indicating whether the owner model is currently in a workflow or not.

Returns

boolean
TRUE if the owner model is in a workflow, FALSE otherwise
public boolean
# statusEquals( raoul2000\workflow\base\Status|string $status = null )

Tests if the current status is equal to the status passed as argument.

Tests if the current status is equal to the status passed as argument.

TRUE is returned when :

  • $status is empty and the owner model has no current status
  • $status is not empty and refers to the same status as the current one

All other condition return FALSE.

This method can be invoked passing a string or a [[Status]] instance argument.

Example :

$post->statusEquals('draft');
       $post->statusEquals($otherPost->getWorkflowStatus());

Parameters

$status
the status to test

Returns

boolean
public static boolean
# isAttachedTo( yii\db\BaseActiveRecord $model )

Tests that a SimpleWorkflowBehavior behavior is attached to the object passed as argument.

Tests that a SimpleWorkflowBehavior behavior is attached to the object passed as argument.

This method returns FALSE if $model is not an instance of yii\base\Component or if none of its attached behaviors is a or inherit from SimpleWorkflowBehavior.

Parameters

$model
the model to test.

Returns

boolean
TRUE if at least one SimpleWorkflowBehavior behavior is attached to $model, FALSE otherwise

Constants summary

string DEFAULT_SOURCE_CLASS

Name of the class used to instantiate the default workflow source component if not configured.

Name of the class used to instantiate the default workflow source component if not configured.

# 'raoul2000\workflow\source\file\WorkflowFileSource'
string DEFAULT_EVENT_SEQUENCE_CLASS

Name of the class used to instantiate the default event sequence if not configured.

Name of the class used to instantiate the default event sequence if not configured.

# 'raoul2000\workflow\events\BasicEventSequence'
string EVENT_BEFORE_CHANGE_STATUS

Name of the default workflow event fired before the owner model change status.

Name of the default workflow event fired before the owner model change status.

# 'EVENT_BEFORE_CHANGE_STATUS'
string EVENT_AFTER_CHANGE_STATUS

Name of the default workflow event fired after the owner model change status.

Name of the default workflow event fired after the owner model change status.

# 'EVENT_AFTER_CHANGE_STATUS'

Properties summary

public string $statusAttribute

name of the owner model attribute used to store the current status value. It is also possible to use a model property but in this case you must provide a suitable status accessor component that will handle status persistence.

name of the owner model attribute used to store the current status value. It is also possible to use a model property but in this case you must provide a suitable status accessor component that will handle status persistence.

# 'status'
public string $source

name of the workflow source component to use with the behavior

name of the workflow source component to use with the behavior

# 'workflowSource'
public null|string|array|object $statusConverter

The status converter component definition or NULL (default) if no status converter is used by this behavior.
When not null, the value of this attribute can be specified in one of the following forms :

  • string : name of an existing status converter component registered in the current Yii::$app.
  • object : the instance of the status converter

Note that the status converter configured here must implement the raoul2000\workflow\base\IStatusIdConverter interface.

The status converter component definition or NULL (default) if no status converter is used by this behavior.
When not null, the value of this attribute can be specified in one of the following forms :

  • string : name of an existing status converter component registered in the current Yii::$app.
  • object : the instance of the status converter

Note that the status converter configured here must implement the raoul2000\workflow\base\IStatusIdConverter interface.

# null
public null|string|array|object $statusAccessor

The status accessor component definition or NULL (default) if no status accessor is used by this behavior.
When not null, the value of this attribute can be specified in one of the following forms :

  • string : name of an existing status accessor component registered in the current Yii::$app.
  • object : the instance of the status converter

Note that the status accessor configured here must implement the raoul2000\workflow\base\IStatusAccessor interface.

The status accessor component definition or NULL (default) if no status accessor is used by this behavior.
When not null, the value of this attribute can be specified in one of the following forms :

  • string : name of an existing status accessor component registered in the current Yii::$app.
  • object : the instance of the status converter

Note that the status accessor configured here must implement the raoul2000\workflow\base\IStatusAccessor interface.

# null
public string $eventSequence

name of the event sequence provider component. If the component does not exist it is created by this behavior using the default event sequence class. Set this attribute to NULL if you are not going to use any Workflow Event.

name of the event sequence provider component. If the component does not exist it is created by this behavior using the default event sequence class. Set this attribute to NULL if you are not going to use any Workflow Event.

# 'eventSequence'
public boolean|string $autoInsert

(not enabled) if TRUE, the model is automatically inserted into the default workflow. If $autoInsert contains a string, it is assumed to be an initial status Id that will be used to set the status. If FALSE (default) the status is not modified.

(not enabled) if TRUE, the model is automatically inserted into the default workflow. If $autoInsert contains a string, it is assumed to be an initial status Id that will be used to set the status. If FALSE (default) the status is not modified.

# false
public boolean $propagateErrorsToModel

If TRUE, all errors that may be registred on an invalidated 'before' event, are assigned to the status attribute of the owner model.

If TRUE, all errors that may be registred on an invalidated 'before' event, are assigned to the status attribute of the owner model.

# false
public boolean $stopOnFirstInvalidEvent

if TRUE, all "before" events are fired even if one of them is invalidated by an attached handler. When FALSE, the first invalidated event interrupts the event sequence. Note that if an event is attached to several handlers, they will all be invoked unless the event is invalidated and marked as handled.

if TRUE, all "before" events are fired even if one of them is invalidated by an attached handler. When FALSE, the first invalidated event interrupts the event sequence. Note that if an event is attached to several handlers, they will all be invoked unless the event is invalidated and marked as handled.

See

WorkflowEvent::invalidate()
# true
public boolean $fireDefaultEvent

When TRUE, a default event is fired on each status change, including when the model enters or leaves the workflow and even if no event sequence is configured. When FALSE the default event is not fired.

When TRUE, a default event is fired on each status change, including when the model enters or leaves the workflow and even if no event sequence is configured. When FALSE the default event is not fired.

# true
yii2-workflow Class Reference API documentation generated by ApiGen