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