|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\statemachine\persistence; |
|
3
|
|
|
use izzum\statemachine\Identifier; |
|
4
|
|
|
use izzum\statemachine\State; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* In memory storage adapter that stores statemachine data, best used in a |
|
8
|
|
|
* runtime environment. |
|
9
|
|
|
* This is the default persistence adapter for a Context. |
|
10
|
|
|
* |
|
11
|
|
|
* TRICKY: This memory adapter only functions during the execution of |
|
12
|
|
|
* one (1) php process. Therefore, it is best used in a runtime environment such |
|
13
|
|
|
* as a php daemon program or an interactive command line php script. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Rolf Vreijdenberger |
|
16
|
|
|
*/ |
|
17
|
|
|
class Memory extends Adapter { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* hashmap. |
|
21
|
|
|
* the key is Identifier->getId() |
|
22
|
|
|
* |
|
23
|
|
|
* @var StorageData[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $registry = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritDoc} |
|
29
|
|
|
*/ |
|
30
|
19 |
|
public function processGetState(Identifier $identifier) |
|
31
|
|
|
{ |
|
32
|
19 |
|
return $this->getStateFromRegistry($identifier); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
17 |
|
protected function insertState(Identifier $identifier, $state, $message = null) |
|
39
|
|
|
{ |
|
40
|
17 |
|
$this->setStateInRegistry($identifier, $state, $message); |
|
41
|
17 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
*/ |
|
46
|
12 |
|
protected function updateState(Identifier $identifier, $state, $message = null) |
|
47
|
|
|
{ |
|
48
|
12 |
|
$this->setStateInRegistry($identifier, $state, $message); |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* @param Identifier $identifier |
|
54
|
|
|
* @param string $state |
|
55
|
|
|
*/ |
|
56
|
17 |
|
protected function setStateInRegistry(Identifier $identifier, $state, $message = null) |
|
57
|
|
|
{ |
|
58
|
17 |
|
$data = new StorageData($identifier, $state, $message); |
|
59
|
17 |
|
$this->writeRegistry($identifier->getId(), $data); |
|
60
|
17 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
*/ |
|
65
|
19 |
|
public function isPersisted(Identifier $identifier) |
|
66
|
|
|
{ |
|
67
|
19 |
|
$persisted = false; |
|
68
|
19 |
|
$storage = $this->getStorageFromRegistry($identifier); |
|
69
|
19 |
|
if ($storage != null) { |
|
70
|
12 |
|
$persisted = true; |
|
71
|
12 |
|
} |
|
72
|
19 |
|
return $persisted; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function getEntityIds($machine, $state = null) |
|
79
|
|
|
{ |
|
80
|
4 |
|
$ids = array(); |
|
81
|
4 |
|
foreach ($this->getRegistry() as $key => $storage) { |
|
82
|
3 |
|
if (strstr($key, $machine)) { |
|
83
|
3 |
|
if ($state) { |
|
84
|
1 |
|
if ($storage->state === $state) { |
|
85
|
1 |
|
$ids [] = $storage->id; |
|
86
|
1 |
|
} |
|
87
|
1 |
|
} else { |
|
88
|
3 |
|
$ids [] = $storage->id; |
|
89
|
|
|
} |
|
90
|
3 |
|
} |
|
91
|
4 |
|
} |
|
92
|
4 |
|
return $ids; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
19 |
|
protected function getStateFromRegistry(Identifier $identifier) |
|
96
|
|
|
{ |
|
97
|
19 |
|
$storage = $this->getStorageFromRegistry($identifier); |
|
98
|
19 |
|
if (!$storage) { |
|
99
|
18 |
|
$state = State::STATE_UNKNOWN; |
|
100
|
18 |
|
} else { |
|
101
|
5 |
|
$state = $storage->state; |
|
102
|
|
|
} |
|
103
|
19 |
|
return $state; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
19 |
|
protected function addHistory(Identifier $identifier, $state, $message = null, $is_exception = false) |
|
110
|
|
|
{ |
|
111
|
|
|
//don't store history in memory, this is a simple adapter and we don't want a memory increase |
|
112
|
|
|
//for a long running process |
|
113
|
19 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key |
|
118
|
|
|
* @param StorageData $value |
|
119
|
|
|
*/ |
|
120
|
17 |
|
protected function writeRegistry($key, $value) |
|
121
|
|
|
{ |
|
122
|
17 |
|
self::$registry [$key] = $value; |
|
123
|
17 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* |
|
127
|
|
|
* @return StorageData[] |
|
128
|
|
|
*/ |
|
129
|
21 |
|
protected function getRegistry() |
|
130
|
|
|
{ |
|
131
|
21 |
|
return self::$registry; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
21 |
|
public function getStorageFromRegistry(Identifier $identifier) |
|
135
|
|
|
{ |
|
136
|
21 |
|
$registry = $this->getRegistry(); |
|
137
|
21 |
|
if (!isset($registry [$identifier->getId()])) { |
|
138
|
21 |
|
$storage = null; |
|
139
|
21 |
|
} else { |
|
140
|
13 |
|
$storage = $registry [$identifier->getId()]; |
|
141
|
|
|
} |
|
142
|
21 |
|
return $storage; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* clears the storage facility. |
|
147
|
|
|
* Not a method we want to have on the Adapter interface. |
|
148
|
|
|
* this method is useful for testing. |
|
149
|
|
|
*/ |
|
150
|
29 |
|
public static function clear() |
|
151
|
|
|
{ |
|
152
|
29 |
|
self::$registry = array(); |
|
153
|
29 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
public static function get() |
|
156
|
|
|
{ |
|
157
|
1 |
|
return self::$registry; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |