|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\statemachine; |
|
3
|
|
|
use izzum\statemachine\loader\Loader; |
|
4
|
|
|
use izzum\statemachine\persistence\Adapter; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* This class (or it's subclasses) should be the preferred way to get a |
|
8
|
|
|
* statemachine. |
|
9
|
|
|
* It's based on the AbstractFactory pattern. |
|
10
|
|
|
* |
|
11
|
|
|
* To fully use polymorphism for all your statemachines, you should instantiate |
|
12
|
|
|
* all your machines via factories. |
|
13
|
|
|
* Full polymorphism allows you to build tooling that is the same for every |
|
14
|
|
|
* statemachine you use in your program. The only thing you would need to do is |
|
15
|
|
|
* instantiate the different factories to produce the different machines. |
|
16
|
|
|
* |
|
17
|
|
|
* The different sql backends provided store a fully qualified factory class |
|
18
|
|
|
* name for a machine so you can instantiate factories dynamically. |
|
19
|
|
|
* |
|
20
|
|
|
* |
|
21
|
|
|
* implement the abstract methods in a factory subclass specific to your problem |
|
22
|
|
|
* domain. |
|
23
|
|
|
* |
|
24
|
|
|
* $factory = new MySpecificFactory($dependencies_injected_here); |
|
25
|
|
|
* $machine = $factory->getStateMachine($order->getId()); |
|
26
|
|
|
* $factory->add($machine->getInitialState()); |
|
27
|
|
|
* $machine->run(); |
|
28
|
|
|
* |
|
29
|
|
|
* @author Rolf Vreijdenberger |
|
30
|
|
|
* |
|
31
|
|
|
* @link https://en.wikipedia.org/wiki/Abstract_factory_pattern |
|
32
|
|
|
* @link https://en.wikipedia.org/wiki/Template_method_pattern |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class AbstractFactory { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets the concrete Loader. |
|
38
|
|
|
* |
|
39
|
|
|
* A simple implementation might use the LoaderArray |
|
40
|
|
|
* |
|
41
|
|
|
* @return Loader An implementation of a Loader class (might be implemented |
|
42
|
|
|
* on the persistence adapter) |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract protected function createLoader(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns an implementation of an Adapter class for the persistence layer |
|
48
|
|
|
* |
|
49
|
|
|
* @return Adapter |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract protected function createAdapter(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get a builder to build your domain objects |
|
55
|
|
|
* |
|
56
|
|
|
* @return EntityBuilder |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract protected function createBuilder(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* get the machine name for the machines that are produced by this factory. |
|
62
|
|
|
* will be used by the Identifier and Context |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
abstract protected function getMachineName(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Factory method to get a correctly configured statemachine without |
|
70
|
|
|
* creating all the default objects in application code. |
|
71
|
|
|
* |
|
72
|
|
|
* TRICKY: When using this method it could lead to unoptimized creation |
|
73
|
|
|
* of different builders/loaders/persistence objects. |
|
74
|
|
|
* For example: a Loader can be reused, a databaseloader will only have |
|
75
|
|
|
* to access a database once for a specific machine to get all the |
|
76
|
|
|
* transitions. |
|
77
|
|
|
* |
|
78
|
|
|
* |
|
79
|
|
|
* When using this method when inside a loop of some sorts where multiple |
|
80
|
|
|
* statemachines for different entities are needed, it would be wise to |
|
81
|
|
|
* cache/reuse the different loaders, builders and persistence adapters. |
|
82
|
|
|
* |
|
83
|
|
|
* php's spl_object_hash() method would be a good way to cache a fully |
|
84
|
|
|
* loaded statemachine. |
|
85
|
|
|
* |
|
86
|
|
|
* Furthermore, once a Loader, ReferenceBuilder and Adapter for persistence |
|
87
|
|
|
* have been instantiated, they can be cached in a field of this class since |
|
88
|
|
|
* they can safely be reused and shared. Or just change the context on a |
|
89
|
|
|
* machine to have access to all the same transitions, builders etc. of the |
|
90
|
|
|
* machine. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $id |
|
93
|
|
|
* the entity id for the Identifier |
|
94
|
|
|
* @return StateMachine a statemachine ready to go |
|
95
|
|
|
* @throws Exception |
|
96
|
|
|
* @link https://en.wikipedia.org/wiki/Abstract_factory_pattern |
|
97
|
|
|
* @link https://en.wikipedia.org/wiki/Template_method_pattern |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getStateMachine($id) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$context = $this->createContext(new Identifier($id, $this->getMachineName())); |
|
102
|
1 |
|
$machine = $this->createMachine($context); |
|
103
|
1 |
|
$loader = $this->createLoader(); |
|
104
|
1 |
|
$loader->load($machine); |
|
105
|
1 |
|
return $machine; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* create a statemachine |
|
110
|
|
|
* |
|
111
|
|
|
* @param Context $context |
|
112
|
|
|
* @return StateMachine |
|
113
|
|
|
*/ |
|
114
|
1 |
|
protected function createMachine(Context $context) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return new StateMachine($context); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Factory method to get a configured Context with the default Builder |
|
121
|
|
|
* and persistence adapter for a concrete statemachine type. |
|
122
|
|
|
* |
|
123
|
|
|
* |
|
124
|
|
|
* @param Identifier $identifier |
|
125
|
|
|
* @return Context |
|
126
|
|
|
* @throws Exception |
|
127
|
|
|
* @link https://en.wikipedia.org/wiki/Abstract_factory_pattern |
|
128
|
|
|
* @link https://en.wikipedia.org/wiki/Template_method_pattern |
|
129
|
|
|
*/ |
|
130
|
1 |
|
protected function createContext(Identifier $identifier) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$context = new Context($identifier, $this->createBuilder(), $this->createAdapter()); |
|
133
|
1 |
|
return $context; |
|
134
|
|
|
} |
|
135
|
|
|
} |