1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Metabor\Statemachine; |
4
|
|
|
|
5
|
|
|
use Metabor\Named; |
6
|
|
|
use Metabor\Statemachine\Util\SetupHelper; |
7
|
|
|
use Metabor\Statemachine\Util\StateCollectionMerger; |
8
|
|
|
use MetaborStd\MergeableInterface; |
9
|
|
|
use MetaborStd\Statemachine\ProcessInterface; |
10
|
|
|
use MetaborStd\Statemachine\StateInterface; |
11
|
|
|
use MetaborStd\Statemachine\TransitionInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Oliver Tischlinger |
15
|
|
|
*/ |
16
|
|
|
class Process extends Named implements ProcessInterface, MergeableInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var StateCollection |
20
|
|
|
*/ |
21
|
|
|
private $states; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var StateInterface |
25
|
|
|
*/ |
26
|
|
|
private $initialState; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StateCollectionMerger |
30
|
|
|
*/ |
31
|
|
|
private $stateCollectionMerger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param StateInterface $initialState |
36
|
|
|
*/ |
37
|
15 |
|
public function __construct($name, StateInterface $initialState) |
38
|
|
|
{ |
39
|
15 |
|
parent::__construct($name); |
40
|
15 |
|
$this->initialState = $initialState; |
41
|
15 |
|
$this->createCollection(); |
42
|
15 |
|
$this->initProcess(new SetupHelper($this->states)); |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param StateInterface $state |
47
|
|
|
*/ |
48
|
15 |
|
protected function addState(StateInterface $state) |
49
|
|
|
{ |
50
|
15 |
|
$name = $state->getName(); |
51
|
15 |
|
if ($this->states->hasState($name)) { |
52
|
|
|
if ($this->states->getState($name) !== $state) { |
53
|
|
|
throw new \Exception('There is already a different state with name "' . $name . '"'); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
15 |
|
$this->states->addState($state); |
57
|
|
|
/* @var $transition TransitionInterface */ |
58
|
15 |
|
foreach ($state->getTransitions() as $transition) { |
59
|
6 |
|
$targetState = $transition->getTargetState(); |
60
|
6 |
|
$this->addState($targetState); |
61
|
15 |
|
} |
62
|
|
|
} |
63
|
15 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SetupHelper $setupHelper |
67
|
|
|
*/ |
68
|
15 |
|
protected function initProcess(SetupHelper $setupHelper) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// overwrite to use SetupHelper |
71
|
15 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
*/ |
75
|
15 |
|
protected function createCollection() |
76
|
|
|
{ |
77
|
15 |
|
$this->states = new StateCollection(); |
78
|
15 |
|
$this->addState($this->initialState); |
79
|
15 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see MetaborStd\Statemachine.ProcessInterface::getInitialState() |
83
|
|
|
*/ |
84
|
10 |
|
public function getInitialState() |
85
|
|
|
{ |
86
|
10 |
|
return $this->initialState; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see MetaborStd\Statemachine.ProcessInterface::getStates() |
91
|
|
|
*/ |
92
|
1 |
|
public function getStates() |
93
|
|
|
{ |
94
|
1 |
|
return $this->states->getStates(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
*/ |
100
|
2 |
|
public function getState($name) |
101
|
|
|
{ |
102
|
2 |
|
return $this->states->getState($name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
|
public function hasState($name) |
111
|
|
|
{ |
112
|
1 |
|
return $this->states->hasState($name); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return StateCollectionMerger |
117
|
|
|
*/ |
118
|
1 |
|
public function getStateCollectionMerger() |
119
|
|
|
{ |
120
|
1 |
|
if (!$this->stateCollectionMerger) { |
121
|
1 |
|
$this->stateCollectionMerger = new StateCollectionMerger($this->states); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
return $this->stateCollectionMerger; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @see \MetaborStd\MergeableInterface::merge() |
129
|
|
|
*/ |
130
|
1 |
|
public function merge($source) |
131
|
|
|
{ |
132
|
1 |
|
$merger = $this->getStateCollectionMerger(); |
133
|
1 |
|
$merger->merge($source); |
134
|
1 |
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.