|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\statemachine; |
|
3
|
|
|
use izzum\statemachine\persistence\Adapter; |
|
4
|
|
|
use izzum\statemachine\persistence\Memory; |
|
5
|
|
|
use izzum\statemachine\Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Context is an object that holds all the contextual information for the |
|
9
|
|
|
* statemachine to do it's work with the help of the relevant dependencies. |
|
10
|
|
|
* A Context is created by your application to provide the right dependencies |
|
11
|
|
|
* ('context') for the statemachine to work with. |
|
12
|
|
|
* |
|
13
|
|
|
* It seperates the concerns for the statemachine of how you are reading/writing |
|
14
|
|
|
* state data and of how you access your domain models. |
|
15
|
|
|
* |
|
16
|
|
|
* Important are: |
|
17
|
|
|
* - the entity id, which references an application domain specific object |
|
18
|
|
|
* like 'Order' or 'Customer' that goes through some finite states in it's |
|
19
|
|
|
* lifecycle. |
|
20
|
|
|
* - the machine name, which is the type identifier for the machine and related |
|
21
|
|
|
* to the entity (eg: 'order-machine') |
|
22
|
|
|
* - persistence adapter, which reads/writes to/from a storage facility |
|
23
|
|
|
* - entity_builder, which constructs the stateful entity. |
|
24
|
|
|
* |
|
25
|
|
|
* The entity is the object that will be acted upon by the |
|
26
|
|
|
* Statemachine. This stateful object will be uniquely identified by it's id, |
|
27
|
|
|
* which will mostly be some sort of primary key for that object that is defined |
|
28
|
|
|
* by the application specific implementation. |
|
29
|
|
|
* |
|
30
|
|
|
* A reference to the stateful object can be obtained via the factory method |
|
31
|
|
|
* getEntity(). |
|
32
|
|
|
* |
|
33
|
|
|
* This class delegates reading and writing states to specific implementations |
|
34
|
|
|
* of the Adapter classes. this is useful for |
|
35
|
|
|
* testing and creating specific behaviour for statemachines that need extra |
|
36
|
|
|
* functionality to get and set the correct states. |
|
37
|
|
|
* |
|
38
|
|
|
* |
|
39
|
|
|
* @author Rolf Vreijdenberger |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
class Context { |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* the Identifier that uniquely identifies the statemachine |
|
46
|
|
|
* |
|
47
|
|
|
* @var Identifier |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $identifier; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* the builder to get the reference to the entity. |
|
53
|
|
|
* |
|
54
|
|
|
* @var EntityBuilder |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $entity_builder; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* the instance for getting to the persistence layer |
|
60
|
|
|
* |
|
61
|
|
|
* @var Adapter |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $persistence_adapter; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* an associated statemachine, if one is set. |
|
67
|
|
|
* Only a statemachine that uses this Context should set itself on the |
|
68
|
|
|
* Context, providing a bidirectional association. |
|
69
|
|
|
* |
|
70
|
|
|
* @var StateMachine |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $statemachine; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Constructor |
|
76
|
|
|
* |
|
77
|
|
|
* @param Identifier $identifier |
|
78
|
|
|
* the identifier for the statemachine |
|
79
|
|
|
* @param EntityBuilder $entity_builder |
|
80
|
|
|
* optional: A specific builder class to create a reference to |
|
81
|
|
|
* the entity we wish to manipulate/have access to. |
|
82
|
|
|
* @param Adapter $persistence_adapter |
|
83
|
|
|
* optional: A specific reader/writer class can be used to |
|
84
|
|
|
* generate different 'read/write' behaviour |
|
85
|
|
|
*/ |
|
86
|
88 |
|
public function __construct(Identifier $identifier, $entity_builder = null, $persistence_adapter = null) |
|
87
|
|
|
{ |
|
88
|
88 |
|
$this->identifier = $identifier; |
|
89
|
88 |
|
$this->entity_builder = $entity_builder; |
|
90
|
88 |
|
$this->persistence_adapter = $persistence_adapter; |
|
91
|
88 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Provides a bidirectional association with the statemachine. |
|
95
|
|
|
* This method should be called only by the StateMachine itself. |
|
96
|
|
|
* |
|
97
|
|
|
* @param StateMachine $statemachine |
|
98
|
|
|
*/ |
|
99
|
58 |
|
public function setStateMachine(StateMachine $statemachine) |
|
100
|
|
|
{ |
|
101
|
58 |
|
$this->statemachine = $statemachine; |
|
102
|
58 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* gets the associated statemachine (if a statemachine is associated) |
|
106
|
|
|
* |
|
107
|
|
|
* @return StateMachine |
|
108
|
|
|
*/ |
|
109
|
18 |
|
public function getStateMachine() |
|
110
|
|
|
{ |
|
111
|
18 |
|
return $this->statemachine; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Gets a (cached) reference to the application domain specific model, |
|
116
|
|
|
* for example an 'Order' or 'Customer' that transitions through states in |
|
117
|
|
|
* it's lifecycle. |
|
118
|
|
|
* |
|
119
|
|
|
* |
|
120
|
|
|
* @param boolean $create_fresh_entity |
|
121
|
|
|
* optional |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
42 |
|
public function getEntity($create_fresh_entity = false) |
|
125
|
|
|
{ |
|
126
|
|
|
// use a specialized builder object to create the (cached) reference. |
|
127
|
42 |
|
return $this->getBuilder()->getEntity($this->getIdentifier(), $create_fresh_entity); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* gets the state. |
|
132
|
|
|
* first try the backend storage facility. If not found, then try the |
|
133
|
|
|
* configured statemachine itself for the initial state. |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
21 |
|
public function getState() |
|
138
|
|
|
{ |
|
139
|
|
|
// get the state by delegating to a specific reader |
|
140
|
21 |
|
$state = $this->getPersistenceAdapter()->getState($this->getIdentifier()); |
|
141
|
|
|
// if found |
|
142
|
21 |
|
if ($state !== State::STATE_UNKNOWN) { |
|
143
|
7 |
|
return $state; |
|
144
|
|
|
} |
|
145
|
|
|
// not found, try to get it from the states loaded on the statemachine |
|
146
|
17 |
|
if (!$this->getStateMachine()) { |
|
147
|
|
|
// reference to statemachine does not exist (possible standalone context object) |
|
148
|
3 |
|
return $state; |
|
149
|
|
|
} |
|
150
|
|
|
// reference to machine exists, just try to get the initial state |
|
151
|
14 |
|
$state = $this->getStateMachine()->getInitialState(true); |
|
152
|
14 |
|
if ($state === null) { |
|
153
|
2 |
|
return State::STATE_UNKNOWN; |
|
154
|
|
|
} |
|
155
|
|
|
// we have a State instance, get the name |
|
156
|
13 |
|
$state = $state->getName(); |
|
157
|
13 |
|
return $state; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Sets the state |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $state |
|
164
|
|
|
* @param string $message optional message. this can be used by the persistence adapter |
|
165
|
|
|
* to be part of the transition history to provide extra information about the transition. |
|
166
|
|
|
* @return boolan true if there was never any state persisted for this |
|
167
|
|
|
* machine before (just added for the |
|
168
|
|
|
* first time), false otherwise |
|
169
|
|
|
*/ |
|
170
|
18 |
|
public function setState($state, $message = null) |
|
171
|
|
|
{ |
|
172
|
|
|
// set the state by delegating to a specific writer |
|
173
|
18 |
|
return $this->getPersistenceAdapter()->setState($this->getIdentifier(), $state, $message); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* adds the state data to the persistence layer if it is not there. |
|
178
|
|
|
* Used to mark the initial construction of a statemachine at a certain |
|
179
|
|
|
* point in time. subsequent calls to 'add' will not have any effect if it |
|
180
|
|
|
* has already been persisted. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $state |
|
183
|
|
|
* @param string $message optional message. this can be used by the persistence adapter |
|
184
|
|
|
* to be part of the transition history to provide extra information about the transition. |
|
185
|
|
|
* @return boolean true if it was added, false if it was already there |
|
186
|
|
|
*/ |
|
187
|
7 |
|
public function add($state, $message = null) |
|
188
|
|
|
{ |
|
189
|
7 |
|
return $this->getPersistenceAdapter()->add($this->getIdentifier(), $state, $message); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* returns the builder used to get the application domain specific model. |
|
194
|
|
|
* |
|
195
|
|
|
* @return EntityBuilder |
|
196
|
|
|
*/ |
|
197
|
43 |
|
public function getBuilder() |
|
198
|
|
|
{ |
|
199
|
43 |
|
if ($this->entity_builder === null || !is_a($this->entity_builder, 'izzum\statemachine\EntityBuilder')) { |
|
200
|
|
|
// the default builder returns the Identifier as the entity |
|
201
|
37 |
|
$this->entity_builder = new EntityBuilder(); |
|
202
|
37 |
|
} |
|
203
|
43 |
|
return $this->entity_builder; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* gets the Context state reader/writer. |
|
208
|
|
|
* |
|
209
|
|
|
* @return Adapter a concrete persistence adapter |
|
210
|
|
|
*/ |
|
211
|
22 |
|
public function getPersistenceAdapter() |
|
212
|
|
|
{ |
|
213
|
22 |
|
if ($this->persistence_adapter === null || !is_a($this->persistence_adapter, 'izzum\statemachine\persistence\Adapter')) { |
|
214
|
|
|
// the default |
|
215
|
15 |
|
$this->persistence_adapter = new Memory(); |
|
216
|
15 |
|
} |
|
217
|
22 |
|
return $this->persistence_adapter; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* gets the entity id that represents the unique identifier for the |
|
222
|
|
|
* application domain specific model. |
|
223
|
|
|
* |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
9 |
|
public function getEntityId() |
|
227
|
|
|
{ |
|
228
|
9 |
|
return $this->getIdentifier()->getEntityId(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* get the Identifier |
|
233
|
|
|
* |
|
234
|
|
|
* @return Identifier |
|
235
|
|
|
*/ |
|
236
|
64 |
|
public function getIdentifier() |
|
237
|
|
|
{ |
|
238
|
64 |
|
return $this->identifier; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* gets the statemachine name that handles the entity |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
26 |
|
public function getMachine() |
|
247
|
|
|
{ |
|
248
|
26 |
|
return $this->getIdentifier()->getMachine(); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* get the toString representation |
|
253
|
|
|
* |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
10 |
|
public function toString() |
|
257
|
|
|
{ |
|
258
|
10 |
|
return get_class($this) . "(" . $this->getId(true) . ")"; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* get the unique identifier for an Context, which consists of the machine |
|
263
|
|
|
* name and the entity_id in parseable form, with an optional state |
|
264
|
|
|
* |
|
265
|
|
|
* @param boolean $readable |
|
266
|
|
|
* human readable or not. defaults to false |
|
267
|
|
|
* @param boolean $with_state |
|
268
|
|
|
* append current state. defaults to false |
|
269
|
|
|
* @return string |
|
270
|
|
|
*/ |
|
271
|
15 |
|
public function getId($readable = false, $with_state = false) |
|
272
|
|
|
{ |
|
273
|
15 |
|
$output = $this->getIdentifier()->getId($readable); |
|
274
|
15 |
|
if ($readable) { |
|
275
|
15 |
|
if ($with_state) { |
|
276
|
1 |
|
$output .= ", state: '" . $this->getState() . "'"; |
|
277
|
1 |
|
} |
|
278
|
15 |
|
} else { |
|
279
|
2 |
|
if ($with_state) { |
|
280
|
1 |
|
$output .= "_" . $this->getState(); |
|
281
|
1 |
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
15 |
|
return $output; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
3 |
|
public function __toString() |
|
288
|
|
|
{ |
|
289
|
3 |
|
return $this->toString(); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* stores a failed transition, called by the statemachine |
|
294
|
|
|
* This is a transition that has failed since it: |
|
295
|
|
|
* - was not allowed |
|
296
|
|
|
* - where an exception was thrown from a rule or command |
|
297
|
|
|
* - etc. any general transition failure |
|
298
|
|
|
* |
|
299
|
|
|
* @param Transition $transition |
|
300
|
|
|
* @param Exception $e |
|
301
|
|
|
*/ |
|
302
|
2 |
|
public function setFailedTransition(Transition $transition, Exception $e) |
|
303
|
|
|
{ |
|
304
|
2 |
|
$this->getPersistenceAdapter()->setFailedTransition($this->getIdentifier(), $transition, $e); |
|
305
|
|
|
} |
|
306
|
|
|
} |