|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metabor\Statemachine\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Metabor\Statemachine\Statemachine; |
|
6
|
|
|
use MetaborStd\Semaphore\MutexFactoryInterface; |
|
7
|
|
|
use MetaborStd\Statemachine\Factory\FactoryInterface; |
|
8
|
|
|
use MetaborStd\Statemachine\Factory\ProcessDetectorInterface; |
|
9
|
|
|
use MetaborStd\Statemachine\Factory\StateNameDetectorInterface; |
|
10
|
|
|
use MetaborStd\Statemachine\Factory\TransitionSelectorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Oliver Tischlinger |
|
14
|
|
|
*/ |
|
15
|
|
|
class Factory implements FactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ProcessDetectorInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $processDetector; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var StateNameDetectorInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $stateNameDetector; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \SplObjectStorage|\SplObserver[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $statemachineObserver; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TransitionSelectorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $transitonSelector; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var MutexFactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $mutexFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ProcessDetectorInterface $processDetector |
|
44
|
|
|
* @param StateNameDetectorInterface $stateNameDetector |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function __construct(ProcessDetectorInterface $processDetector, StateNameDetectorInterface $stateNameDetector = null) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->processDetector = $processDetector; |
|
49
|
1 |
|
$this->stateNameDetector = $stateNameDetector; |
|
50
|
1 |
|
$this->statemachineObserver = new \SplObjectStorage(); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param MutexFactoryInterface $mutexFactory |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setMutexFactory(MutexFactoryInterface $mutexFactory = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->mutexFactory = $mutexFactory; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param TransitionSelectorInterface $transitionSelector |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setTransitonSelector(TransitionSelectorInterface $transitionSelector) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->transitonSelector = $transitionSelector; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \SplObserver $observer |
|
71
|
|
|
*/ |
|
72
|
|
|
public function attachStatemachineObserver(\SplObserver $observer) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->statemachineObserver->attach($observer); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \SplObserver $observer |
|
79
|
|
|
*/ |
|
80
|
|
|
public function detachStatemachineObserver(\SplObserver $observer) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->statemachineObserver->detach($observer); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \Traversable |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getStatemachineObserver() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->statemachineObserver; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param object $subject |
|
95
|
|
|
* |
|
96
|
|
|
* @return \MetaborStd\Statemachine\StatemachineInterface |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function createStatemachine($subject) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$process = $this->processDetector->detectProcess($subject); |
|
101
|
1 |
|
if ($this->stateNameDetector) { |
|
102
|
|
|
$stateName = $this->stateNameDetector->detectCurrentStateName($subject); |
|
103
|
|
|
} else { |
|
104
|
1 |
|
$stateName = null; |
|
105
|
|
|
} |
|
106
|
1 |
|
if ($this->mutexFactory) { |
|
107
|
1 |
|
$mutex = $this->mutexFactory->createMutex($subject); |
|
108
|
|
|
} else { |
|
109
|
1 |
|
$mutex = null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
$statemachine = new Statemachine($subject, $process, $stateName, $this->transitonSelector, $mutex); |
|
113
|
|
|
|
|
114
|
1 |
|
foreach ($this->statemachineObserver as $observer) { |
|
115
|
|
|
$statemachine->attach($observer); |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return $statemachine; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|