PHP Класс 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',
        ],
    ];
}
Наследование: extends yii\base\Behavior
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
__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.

Приватные методы

Метод Описание
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.

Описание методов

__construct() публичный метод

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

afterDelete() публичный метод

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

afterSaveStatus() публичный метод

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() публичный метод

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.
См. также: http://www.yiiframework.com/doc-2.0/yii-base-behavior.html#attach()-detail
См. также: initStatus()
public attach ( $owner )

beforeDelete() публичный метод

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

beforeSaveStatus() публичный метод

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() публичный метод

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
Результат array Three elements : Status intance, scenario names, event sequence.

enterWorkflow() публичный метод

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.
Результат boolean TRUE if the operation succeeded, FALSE otherwise

events() публичный метод

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() публичный метод

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
Результат string id for the workflow the owner model is in.

getEventSequence() публичный метод

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
Результат raoul2000\workflow\events\WorkflowEvent[] The list of events

getNextStatuses() публичный метод

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
Результат array list of status

getScenarioSequence() публичный метод

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
Результат string[] list of scenario names

getStatusAccessor() публичный метод

This component is initialized by the [[$statusAccessor]] configuration property.
public getStatusAccessor ( ) : null | raoul2000\workflow\base\IStatusAccessor
Результат null | raoul2000\workflow\base\IStatusAccessor the status accessor component used bu the behavior or NULL if not configured (default)

getStatusConverter() публичный метод

This component is initialized by the [[$statusConverter]] configuration property.
public getStatusConverter ( ) : null | raoul2000\workflow\base\IStatusIdConverter
Результат null | raoul2000\workflow\base\IStatusIdConverter The status Id Converter used by the behavior or NULL if not configured (default)

getWorkflow() публичный метод

Returns the current Workflow instance the model is in or NULL if the model is not in a workflow.
public getWorkflow ( ) : null | Workflow
Результат null | Workflow the workflow the owner model is currently in, or null if the owner model is not in a workflow

getWorkflowSource() публичный метод

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
Результат raoul2000\workflow\source\IWorkflowSource the workflow source component instance used by this behavior

getWorkflowStatus() публичный метод

Returns the current Status instance.
См. также: hasWorkflowStatus()
public getWorkflowStatus ( ) : null | Status
Результат null | Status The status instance or NULL if the model is not in a workflow

hasWorkflowStatus() публичный метод

Returns a value indicating whether the owner model is currently in a workflow or not.
public hasWorkflowStatus ( ) : boolean
Результат boolean TRUE if the owner model is in a workflow, FALSE otherwise

init() публичный метод

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() публичный метод

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() публичный статический метод

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.
Результат boolean TRUE if at least one SimpleWorkflowBehavior behavior is attached to $model, FALSE otherwise

sendToStatus() публичный метод

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.
Результат boolean TRUE if the transition could be performed, FALSE otherwise

statusEquals() публичный метод

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
Результат boolean

Описание свойств

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

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

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

$propagateErrorsToModel публичное свойство

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 публичное свойство

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

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

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

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

$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.
См. также: WorkflowEvent::invalidate()
public $stopOnFirstInvalidEvent