PHP Class raoul2000\workflow\base\SimpleWorkflowBehavior

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',
        ],
    ];
}
Inheritance: extends yii\base\Behavior
Afficher le fichier Open project: raoul2000/yii2-workflow Class Usage Examples

Méthodes publiques

Свойство Type Description
$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.
$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.
$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.
$propagateErrorsToModel If TRUE, all errors that may be registred on an invalidated 'before' event, are assigned to the status attribute of the owner model.
$source name of the workflow source component to use with the behavior
$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.
$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.
$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.
$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.

Méthodes publiques

Méthode Description
__construct ( array $config = [] ) Instance constructor.
afterDelete ( Event $event ) Fires pending events, once the owner model has been successfully deleted.
afterSaveStatus ( Event $event ) After the owner model has been saved, fire pending events.
attach ( $owner ) Attaches the behavior to the model.
beforeDelete ( Event $event ) Handle the case where the owner model is leaving the workflow.
beforeSaveStatus ( Event $event ) Send owner model into status if needed.
createTransitionItems ( mixed $status, boolean $WithScenarioNames, boolean $withEventSequence ) : array Creates and returns workflow event sequence and/or scenario for the pending transition.
enterWorkflow ( string $workflowId = null ) : boolean Puts the owner model into the workflow $workflowId or into its default workflow if no argument is provided.
events ( ) Install events handlers.
getDefaultWorkflowId ( ) : string Returns the id of the default workflow associated with the owner model.
getEventSequence ( string $status ) : WorkflowEvent[] 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.
getNextStatuses ( $validate = false, $beforeEvents = false ) : array Returns all status that can be reached from the current status.
getScenarioSequence ( string $status ) : string[] 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.
getStatusAccessor ( ) : null | raoul2000\workflow\base\IStatusAccessor Returns the status accessor instance used by this behavior or NULL if no status accessor is used.
getStatusConverter ( ) : null | raoul2000\workflow\base\IStatusIdConverter Returns the status converter instance used by this behavior or NULL if no status converter is used.
getWorkflow ( ) : null | Workflow Returns the current Workflow instance the model is in or NULL if the model is not in a workflow.
getWorkflowSource ( ) : raoul2000\workflow\source\IWorkflowSource Returns the *Workflow Source Component* used by this behavior.
getWorkflowStatus ( ) : null | Status Returns the current Status instance.
hasWorkflowStatus ( ) : boolean Returns a value indicating whether the owner model is currently in a workflow or not.
init ( ) Initialize the behavior.
initStatus ( ) Initialize the internal status value based on the owner model status attribute.
isAttachedTo ( BaseActiveRecord $model ) : boolean Tests that a SimpleWorkflowBehavior behavior is attached to the object passed as argument.
sendToStatus ( Status | string $status ) : boolean Send the owner model into the status passed as argument.
statusEquals ( Status | string $status = null ) : boolean Tests if the current status is equal to the status passed as argument.

Private Methods

Méthode Description
doAutoInsert ( ) NOT IMPLEMENTED
ensureStatusInstance ( mixed $mixed, boolean $strict = false ) : Status Returns a Status instance for the value passed as argument.
firePendingEvents ( ) Send pending events.
getOwnerStatus ( ) : string Returns the value stored in the [[statusAttribute]] attribute of the owner model.
selectDefaultWorkflowId ( ) : string Returns the default workflow ID to use with this model.
sendToStatusInternal ( mixed $status, boolean $delayed ) : boolean Performs status change and event fire.
setStatusInternal ( Status | null $status ) Set the internal status value and the owner model status attribute.

Method Details

__construct() public méthode

Instance constructor.
public __construct ( array $config = [] )
$config array

afterDelete() public méthode

This is the event handler for ActiveRecord::EVENT_AFTER_DELETE
public afterDelete ( Event $event )
$event yii\base\Event

afterSaveStatus() public méthode

This is the event handler for both ActiveRecord::EVENT_AFTER_UPDATE and ActiveRecord::EVENT_AFTER_INSERT events
public afterSaveStatus ( Event $event )
$event yii\base\Event

attach() public méthode

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 also: http://www.yiiframework.com/doc-2.0/yii-base-behavior.html#attach()-detail
See also: initStatus()
public attach ( $owner )

beforeDelete() public méthode

This is the event handler for ActiveRecord::EVENT_BEFORE_DELETE
public beforeDelete ( Event $event )
$event yii\base\Event

beforeSaveStatus() public méthode

This is the event handler for both ActiveRecord::EVENT_BEFORE_INSERT and ActiveRecord::EVENT_BEFORE_UPDATE events
public beforeSaveStatus ( Event $event )
$event yii\base\Event

createTransitionItems() public méthode

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 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)
public createTransitionItems ( mixed $status, boolean $WithScenarioNames, boolean $withEventSequence ) : array
$status mixed a status Id or a Status instance considered as the target status to reach
$WithScenarioNames boolean When TRUE scenario names are requested, FALSE otherwise
$withEventSequence boolean When TRUE the event sequence is requested, FALSE otherwise
Résultat array Three elements : Status intance, scenario names, event sequence.

enterWorkflow() public méthode

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.
public enterWorkflow ( string $workflowId = null ) : boolean
$workflowId string the ID of the workflow the owner model must be inserted to or NULL to insert into the model's default workflow.
Résultat boolean TRUE if the operation succeeded, FALSE otherwise

events() public méthode

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 events ( )

getDefaultWorkflowId() public méthode

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**.
public getDefaultWorkflowId ( ) : string
Résultat string id for the workflow the owner model is in.

getEventSequence() public méthode

The event list returned by this method depends on the event sequence component that was configured for this behavior at construction time.
public getEventSequence ( string $status ) : WorkflowEvent[]
$status string the target status
Résultat raoul2000\workflow\events\WorkflowEvent[] The list of events

getNextStatuses() public méthode

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.
public getNextStatuses ( $validate = false, $beforeEvents = false ) : array
Résultat array list of status

getScenarioSequence() public méthode

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.
public getScenarioSequence ( string $status ) : string[]
$status string the target status
Résultat string[] list of scenario names

getStatusAccessor() public méthode

This component is initialized by the [[$statusAccessor]] configuration property.
public getStatusAccessor ( ) : null | raoul2000\workflow\base\IStatusAccessor
Résultat null | raoul2000\workflow\base\IStatusAccessor the status accessor component used bu the behavior or NULL if not configured (default)

getStatusConverter() public méthode

This component is initialized by the [[$statusConverter]] configuration property.
public getStatusConverter ( ) : null | raoul2000\workflow\base\IStatusIdConverter
Résultat null | raoul2000\workflow\base\IStatusIdConverter The status Id Converter used by the behavior or NULL if not configured (default)

getWorkflow() public méthode

Returns the current Workflow instance the model is in or NULL if the model is not in a workflow.
public getWorkflow ( ) : null | Workflow
Résultat null | Workflow the workflow the owner model is currently in, or null if the owner model is not in a workflow

getWorkflowSource() public méthode

This component is initialized by the [[$source]] configuration property. If not configured, the behavior creates and register its own workflow source component.
public getWorkflowSource ( ) : raoul2000\workflow\source\IWorkflowSource
Résultat raoul2000\workflow\source\IWorkflowSource the workflow source component instance used by this behavior

getWorkflowStatus() public méthode

Returns the current Status instance.
See also: hasWorkflowStatus()
public getWorkflowStatus ( ) : null | Status
Résultat null | Status The status instance or NULL if the model is not in a workflow

hasWorkflowStatus() public méthode

Returns a value indicating whether the owner model is currently in a workflow or not.
public hasWorkflowStatus ( ) : boolean
Résultat boolean TRUE if the owner model is in a workflow, FALSE otherwise

init() public méthode

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 init ( )

initStatus() public méthode

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.
public initStatus ( )

isAttachedTo() public static méthode

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.
public static isAttachedTo ( BaseActiveRecord $model ) : boolean
$model yii\db\BaseActiveRecord the model to test.
Résultat boolean TRUE if at least one SimpleWorkflowBehavior behavior is attached to $model, FALSE otherwise

sendToStatus() public méthode

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.
public sendToStatus ( Status | string $status ) : boolean
$status Status | string the destination status to reach. If NULL, then the owner model is going to leave its current workflow.
Résultat boolean TRUE if the transition could be performed, FALSE otherwise

statusEquals() public méthode

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());
public statusEquals ( Status | string $status = null ) : boolean
$status Status | string the status to test
Résultat boolean

Property Details

$autoInsert public_oe property

(**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.
public $autoInsert

$eventSequence public_oe property

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.
public $eventSequence

$fireDefaultEvent public_oe property

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.
public $fireDefaultEvent

$propagateErrorsToModel public_oe property

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

$source public_oe property

name of the workflow source component to use with the behavior
public $source

$statusAccessor public_oe property

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.
public $statusAccessor

$statusAttribute public_oe property

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.
public $statusAttribute

$statusConverter public_oe property

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.
public $statusConverter

$stopOnFirstInvalidEvent public_oe property

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 also: WorkflowEvent::invalidate()
public $stopOnFirstInvalidEvent