Original file: src/Fsm/Data/TransitionAction.php
The TransitionAction class is part of a Finite State Machine (FSM) system implemented in PHP. This class represents an action that occurs during a transition between states within the FSM framework. By allowing for both callable logic defined as strings or Closures, the class enables a flexible mechanism for defining behavior that can be triggered in various states of the system.
The following constants are defined for the TransitionAction class. These constants help enforce strict typing and improve static analysis.
| Constant | Value | Description |
|---|---|---|
TIMING_BEFORE |
'before' |
Action to be executed before the transition occurs. |
TIMING_AFTER |
'after' |
Action to be executed after the transition occurs. |
TIMING_ON_SUCCESS |
'on_success' |
Action to be executed when the transition succeeds. |
TIMING_ON_FAILURE |
'on_failure' |
Action to be executed when the transition fails. |
TYPE_VERB |
'verb' |
Indicates that the action is a class string or verb. |
TYPE_CLOSURE |
'closure' |
Indicates that the action is a Closure. |
TYPE_CALLABLE |
'callable' |
Indicates that the action is a callable array. |
TYPE_SERVICE |
'service' |
Indicates that the action is a service. |
PRIORITY_CRITICAL |
100 |
Highest action priority. |
PRIORITY_HIGH |
75 |
High action priority. |
PRIORITY_NORMAL |
50 |
Normal action priority. |
PRIORITY_LOW |
25 |
Lowest action priority. |
The class defines the following properties:
| Property | Type | Default | Description |
|---|---|---|---|
callable |
`string | Closure | array` |
parameters |
array<string, mixed> |
[] |
Static parameters to pass to the callable action. |
runAfterTransition |
bool |
true |
Flag to indicate if the action should run after the model transition is completed. |
timing |
string |
self::TIMING_AFTER |
Timing of when the action should be executed relative to the transition. |
priority |
int |
self::PRIORITY_NORMAL |
Execution priority determining the order of action execution. |
name |
`string | null` | null |
queued |
bool |
false |
Whether the action is queued for execution. |
public function __construct(
string|Closure|array $callable,
array $parameters = [],
bool $runAfterTransition = true,
string $timing = self::TIMING_AFTER,
int $priority = self::PRIORITY_NORMAL,
?string $name = null,
bool $queued = false,
)
callable: The action logic, which can be a string, a Closure, or an array indicating a callable.parameters: An array of static parameters to be passed to the callable.runAfterTransition: A boolean flag indicating if the action should run after the model is saved.timing: A string indicating when the action will be executed relative to the transition.priority: An integer representing the action's execution priority. Higher values are executed first.name: An optional string for the action's name, useful for debugging or logging.queued: A boolean indicating if the action is queued.The constructor initializes the action with the provided parameters. It features improved logic for constructing the object from an array if the first argument is an array, checking against expected keys.
public function getType(): string
Determines the type of action based on the callable property.
TYPE_VERB, TYPE_CLOSURE, TYPE_CALLABLE, or TYPE_SERVICE.This method assesses the callable property's value and utilizes a match expression to determine its type based on the following classifications:
callable is a string and represents an existing class, it returns TYPE_VERB.callable is an instance of Closure, it returns TYPE_CLOSURE.callable is an array, it returns TYPE_CALLABLE.TYPE_SERVICE.public function shouldExecuteAt(string $timing): bool
Determines if the action should be executed at the provided timing.
timing: A string representing the timing to check against, such as TIMING_BEFORE, TIMING_AFTER, TIMING_ON_SUCCESS, or TIMING_ON_FAILURE.This method compares the timing property with the provided timing argument and returns true if they match, enabling condition checks for action execution based on the transition lifecycle.
public function getDisplayName(): string
Retrieves a human-readable name for the action.
The method checks if a name has been provided. If not, it derives a name based on the type of action:
TYPE_VERB, it returns the class basename if it is a string.TYPE_CLOSURE, it returns 'Closure'.TYPE_CALLABLE, it returns a combination of the class name and method name, if applicable.This aids in debugging and logging by providing meaningful information about the action being executed.
This detailed documentation serves as a resource for developers working with the TransitionAction class in the FSM system, providing
How can I help you explore Laravel packages today?