Constructor
public __construct ( |
||
$context | a fully configured context providing all the relevant parameters/dependencies to be able to run this statemachine for an entity. |
public function __construct($name, $alias) { $this->name = $name; $this->alias = $alias; //create machine with unique superhero name. //associate the domain object (that will be used on callables) with $this $context = new Context(new Identifier($name . ":" . $alias, "superhero-machine"), new ModelBuilder($this)); //call parent constructor parent::__construct($context); //define the states and the state types, and some entry states $start = new State('start', State::TYPE_INITIAL); $callback_dress_normal_for_entering_state = array($this, 'changeIntoNormalClothes'); $normal = new State('normal', State::TYPE_NORMAL, null, null, $callback_dress_normal_for_entering_state); $callback_entering_superhero_state = array($this, 'changeIntoCostume'); $super = new State('superhero', State::TYPE_NORMAL, null, null, $callback_entering_superhero_state); $posing = new State('posing'); $fighting = new State('fighting'); $resqueing = new State('resqueing'); $done = new State('done', State::TYPE_FINAL); //add transitions to this class (a subclass of statmachine), with event names $this->addTransition(new Transition($start, $normal, 'wakeup')); $this->addTransition(new Transition($normal, $done, 'done')); $this->addTransition(new Transition($super, $fighting, 'fight')); $this->addTransition(new Transition($super, $posing, 'pose')); $this->addTransition(new Transition($super, $resqueing, 'rescue')); //allow to go from super to every other state $this->addTransition(new Transition($super, new State('regex:/.*/'))); //allow to go from every state to super $this->addTransition(new Transition(new State('regex:/.*/'), $super, 'beSuper')); //allow to go from every state to normal $this->addTransition(new Transition(new State('regex:/.*/'), $normal, 'standDown')); //allow to pose, rescue of fight from every state except start and normal $this->addTransition(new Transition(new State('not-regex:/start|normal/'), $posing, 'pose')); $this->addTransition(new Transition(new State('not-regex:/start|normal/'), $resqueing, 'resque')); $this->addTransition(new Transition(new State('not-regex:/start|normal/'), $fighting, 'fight')); }