izzum\statemachine\StateMachine::run PHP 메소드

run() 공개 메소드

The first possible transition is based on the configuration of the guard logic and the current state of the statemachine. This method is the main way to have the statemachine do useful work. TRICKY: Be careful when using this function, since all guard logic must be mutually exclusive! If not, you might end up performing the state transition with priority n when you really want to perform transition n+1. An alternative is to use the 'transition' method to target 1 transition specifically: $statemachine->transition('a_to_b'); So you are always sure that you are actually doing the intented transition instead of relying on the configuration and guard logic (which *might* not be correctly implemented, leading to transitions that would normally not be executed).
public run ( string $message = null ) : boolean
$message string optional message. this can be used by the persistence adapter to be part of the transition history to provide extra information about the transition.
리턴 boolean true if a transition was applied.
    public function run($message = null)
    {
        try {
            $transitions = $this->getCurrentState()->getTransitions();
            foreach ($transitions as $transition) {
                $transitioned = $this->performTransition($transition, $message);
                if ($transitioned) {
                    return true;
                }
            }
        } catch (Exception $e) {
            // will be rethrown
            $this->handlePossibleNonStatemachineException($e, Exception::SM_RUN_FAILED);
        }
        // no transition done
        return false;
    }

Usage Example

$violet = new State('violet');
// create the machine with the correct session adapter to store the state accross page refreshes
$adapter = new Session();
$machine = new StateMachine(new Context(new Identifier('session-example', 'rainbow-machine'), null, $adapter));
//add the transitions, going from one color to the next and back to the first
$machine->addTransition(new Transition($new, $red));
$machine->addTransition(new Transition($red, $orange));
$machine->addTransition(new Transition($orange, $yellow));
$machine->addTransition(new Transition($yellow, $green));
$machine->addTransition(new Transition($green, $blue));
$machine->addTransition(new Transition($blue, $indigo));
$machine->addTransition(new Transition($indigo, $violet));
$machine->addTransition(new Transition($violet, $red));
//initialize the first time to 'red' and then cycle through the
//colors for each page refresh
$machine->run();
//get some data to put in the output
$current = $machine->getCurrentState();
$next_transitions = implode(',', $current->getTransitions());
$next = $current->getTransitions()[0]->getStateTo();
//generate the ouput
$output = <<<EOT
<html>
    <header>
        <title>rainbows all over the place: gimme some more izzum jo!</title>
        <style>
        body {
            background-color: {$current}; 
            color: black;
            padding:10px;
            margin:10px;