1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Finite\State\StateInterface; |
4
|
|
|
|
5
|
|
|
class StateMachineFactory extends Object { |
6
|
|
|
|
7
|
|
|
private static $handlers = []; |
8
|
|
|
|
9
|
|
|
public function forDNDeployment(DNDeployment $obj) { |
10
|
|
|
$loader = new Finite\Loader\ArrayLoader([ |
11
|
|
|
'class' => 'DNDeployment', |
12
|
|
|
'states' => [ |
13
|
|
|
DNDeployment::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL], |
14
|
|
|
DNDeployment::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL], |
15
|
|
|
DNDeployment::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL], |
16
|
|
|
DNDeployment::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL], |
17
|
|
|
DNDeployment::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL], |
18
|
|
|
DNDeployment::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL], |
19
|
|
|
DNDeployment::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL], |
20
|
|
|
DNDeployment::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL], |
21
|
|
|
], |
22
|
|
|
'transitions' => [ |
23
|
|
|
DNDeployment::TR_SUBMIT => ['from' => [DNDeployment::STATE_NEW], 'to' => DNDeployment::STATE_SUBMITTED], |
24
|
|
|
DNDeployment::TR_QUEUE => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_QUEUED], |
25
|
|
|
DNDeployment::TR_INVALIDATE => [ |
26
|
|
|
'from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_SUBMITTED], |
27
|
|
|
'to' => DNDeployment::STATE_INVALID |
28
|
|
|
], |
29
|
|
|
DNDeployment::TR_DEPLOY => ['from' => [DNDeployment::STATE_QUEUED], 'to' => DNDeployment::STATE_DEPLOYING], |
30
|
|
|
DNDeployment::TR_ABORT => [ |
31
|
|
|
'from' => [ |
32
|
|
|
DNDeployment::STATE_QUEUED, |
33
|
|
|
DNDeployment::STATE_DEPLOYING, |
34
|
|
|
DNDeployment::STATE_ABORTING |
35
|
|
|
], |
36
|
|
|
'to' => DNDeployment::STATE_ABORTING |
37
|
|
|
], |
38
|
|
|
DNDeployment::TR_COMPLETE => ['from' => [DNDeployment::STATE_DEPLOYING], 'to' => DNDeployment::STATE_COMPLETED], |
39
|
|
|
DNDeployment::TR_FAIL => [ |
40
|
|
|
'from' => [ |
41
|
|
|
DNDeployment::STATE_NEW, |
42
|
|
|
DNDeployment::STATE_SUBMITTED, |
43
|
|
|
DNDeployment::STATE_QUEUED, |
44
|
|
|
DNDeployment::STATE_INVALID, |
45
|
|
|
DNDeployment::STATE_DEPLOYING, |
46
|
|
|
DNDeployment::STATE_ABORTING |
47
|
|
|
], |
48
|
|
|
'to' => DNDeployment::STATE_FAILED |
49
|
|
|
], |
50
|
|
|
] |
51
|
|
|
]); |
52
|
|
|
$stateMachine = new Finite\StateMachine\StateMachine($obj); |
53
|
|
|
$loader->load($stateMachine); |
54
|
|
|
$stateMachine->initialize(); |
55
|
|
|
$this->addHandlers($stateMachine); |
56
|
|
|
return $stateMachine; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function addHandlers($machine) { |
60
|
|
|
$class = get_class($machine->getObject()); |
61
|
|
|
if (empty($this->config()->handlers[$class])) return; |
62
|
|
|
|
63
|
|
|
$transitions = $this->config()->handlers[$class]; |
64
|
|
|
foreach ($transitions as $transName => $handlers) { |
65
|
|
|
if (!is_array($handlers)) { |
66
|
|
|
throw new Exception(sprintf('Transition %s must be configured as an assoc array.', $transName)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($handlers as $handlerClass => $handlerMethod) { |
70
|
|
|
$handlerObj = Injector::inst()->get($handlerClass); |
71
|
|
|
if (!is_callable([$handlerObj, $handlerMethod])) { |
72
|
|
|
throw new Exception(sprintf('Handler %s is not callable on %s.', $handlerMethod, $handlerClass)); |
73
|
|
|
} |
74
|
|
|
$machine->getDispatcher()->addListener( |
75
|
|
|
sprintf('finite.post_transition.%s', $transName), |
76
|
|
|
[$handlerObj, $handlerMethod] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|