1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\utils; |
3
|
|
|
use izzum\statemachine\persistence\Adapter; |
4
|
|
|
use izzum\statemachine\loader\Loader; |
5
|
|
|
use izzum\statemachine\Identifier; |
6
|
|
|
use izzum\statemachine\Exception; |
7
|
|
|
use izzum\statemachine\State; |
8
|
|
|
use izzum\statemachine\Transition; |
9
|
|
|
use izzum\statemachine\StateMachine; |
10
|
|
|
use izzum\statemachine\utils\Utils; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* mix and match a loader (reader) and a persistance adapter (writer) by wrapping |
14
|
|
|
* both of them and use them to delegate the handling logic to. |
15
|
|
|
* |
16
|
|
|
* This allows us to load the statemachine configuration from a specific source and use |
17
|
|
|
* a different sink to write the current state and history information to. |
18
|
|
|
* |
19
|
|
|
* You might want to do this to read the relatively static content for the configuration from |
20
|
|
|
* the filesystem and use a fast backend system to write the state and transition history data to. |
21
|
|
|
* |
22
|
|
|
* for example: |
23
|
|
|
* - use an xml file with the PDO (sql) adapter: |
24
|
|
|
* izzum\statemachine\loader\XML & izzum\statemachine\persistence\PDO |
25
|
|
|
* - use a json file with the Redis adapter: |
26
|
|
|
* izzum\statemachine\loader\JSON & izzum\statemachine\persistence\Redis classes |
27
|
|
|
* - use php code to configure the machine with the Session adapter: |
28
|
|
|
* izzum\statemachine\loader\LoaderArray & izzum\statemachine\persistence\Session classes) |
29
|
|
|
* |
30
|
|
|
* |
31
|
|
|
* @author Rolf Vreijdenberger |
32
|
|
|
* @link https://en.wikipedia.org/wiki/Delegation_pattern |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
class ReaderWriterDelegator extends Adapter implements Loader { |
36
|
|
|
/** |
37
|
|
|
* an instance of a Loader, which reads data |
38
|
|
|
* @var Loader |
39
|
|
|
*/ |
40
|
|
|
private $reader; |
41
|
|
|
/** |
42
|
|
|
* an instance of an Adapter, which writes data |
43
|
|
|
* @var Adapter |
44
|
|
|
*/ |
45
|
|
|
private $writer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Loader $reader the Loader instance to decorate |
49
|
|
|
* @param Adapter $writer the Adapter instance to decorate |
50
|
|
|
*/ |
51
|
2 |
|
public function __construct(Loader $reader, Adapter $writer) |
52
|
|
|
{ |
53
|
2 |
|
$this->reader = $reader; |
54
|
2 |
|
$this->writer = $writer; |
55
|
2 |
|
} |
56
|
|
|
/** |
57
|
|
|
* gets the reader/Loader |
58
|
|
|
* @return Loader |
59
|
|
|
*/ |
60
|
1 |
|
public function getReader() { |
61
|
1 |
|
return $this->reader; |
62
|
|
|
} |
63
|
|
|
/** |
64
|
|
|
* gets the writer/Adapter |
65
|
|
|
* @return Adapter |
66
|
|
|
*/ |
67
|
1 |
|
public function getWriter() { |
68
|
1 |
|
return $this->writer; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function load(StateMachine $stateMachine) |
72
|
|
|
{ |
73
|
1 |
|
return $this->reader->load($stateMachine); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function getEntityIds($machine, $state = null) |
77
|
|
|
{ |
78
|
1 |
|
return $this->writer->getEntityIds($machine, $state); |
79
|
|
|
} |
80
|
1 |
|
public function isPersisted(Identifier $identifier) |
81
|
|
|
{ |
82
|
1 |
|
return $this->writer->isPersisted($identifier); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function processSetState(Identifier $identifier, $state, $message = null) |
86
|
|
|
{ |
87
|
1 |
|
return $this->writer->processSetState($identifier, $state, $message); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function processGetState(Identifier $identifier) |
91
|
|
|
{ |
92
|
1 |
|
return $this->writer->processGetState($identifier); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function add(Identifier $identifier, $state, $message = null) |
96
|
|
|
{ |
97
|
1 |
|
return $this->writer->add($identifier, $state, $message); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function setFailedTransition(Identifier $identifier, Transition $transition, \Exception $e) |
101
|
|
|
{ |
102
|
1 |
|
$this->writer->setFailedTransition($identifier, $transition, $e); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
public function toString() |
106
|
|
|
{ |
107
|
1 |
|
return parent::toString() . " [reader] " . $this->reader->toString() . " [writer] " . $this->writer->toString(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function __toString() |
111
|
|
|
{ |
112
|
1 |
|
return $this->toString(); |
113
|
|
|
} |
114
|
|
|
} |