1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\persistence; |
3
|
|
|
use izzum\statemachine\Identifier; |
4
|
|
|
use izzum\statemachine\State; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* the Session persistence adapter uses php sessions for persistence. |
8
|
|
|
* |
9
|
|
|
* Keep in mind that: |
10
|
|
|
* - php sessions have a limited lifetime. |
11
|
|
|
* - php sessions itself can have a different backend adapter, configurable. |
12
|
|
|
* - php sessions are only valid for one user's session (unless a session id is |
13
|
|
|
* forced) |
14
|
|
|
* |
15
|
|
|
* A simple adapter for a proof of concept and an example. |
16
|
|
|
* it's possible to use this adapter for for instance gui wizards. |
17
|
|
|
* |
18
|
|
|
* see also the /examples/session for how to use this adapter. |
19
|
|
|
* |
20
|
|
|
* TRICKY: make sure output is not already sent when instantiating this adapter. |
21
|
|
|
* |
22
|
|
|
* @author Rolf Vreijdenberger |
23
|
|
|
*/ |
24
|
|
|
class Session extends Adapter { |
25
|
|
|
/** |
26
|
|
|
* the namespace of the session |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $namespace; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @param string $namespace |
35
|
|
|
* optional, defaults to izzum |
36
|
|
|
* @param string $session_id |
37
|
|
|
* optional force a session id, used for testing purposes |
38
|
|
|
*/ |
39
|
|
|
public function __construct($namespace = 'izzum', $session_id = null) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
if (session_status() === PHP_SESSION_NONE) { |
42
|
|
|
if ($session_id !== null) { |
43
|
|
|
session_id($session_id); |
44
|
|
|
} |
45
|
|
|
session_start(); |
46
|
|
|
} |
47
|
|
|
$this->namespace = $namespace; |
48
|
|
|
if (!isset($_SESSION)) { |
49
|
|
|
$_SESSION = array(); |
50
|
|
|
} |
51
|
|
|
if (!isset($_SESSION [$this->namespace])) { |
52
|
|
|
$_SESSION [$this->namespace] = array(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function processGetState(Identifier $identifier) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$key = $identifier->getId(); |
63
|
|
|
if (isset($_SESSION [$this->namespace] [$key])) { |
64
|
|
|
$state = $_SESSION [$this->namespace] [$key]->state; |
65
|
|
|
} else { |
66
|
|
|
$state = State::STATE_UNKNOWN; |
67
|
|
|
} |
68
|
|
|
return $state; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
protected function insertState(Identifier $identifier, $state, $message = null) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
// set object on the session |
78
|
|
|
$data = new StorageData($identifier, $state, $message); |
79
|
|
|
$key = $identifier->getId(); |
80
|
|
|
$_SESSION [$this->namespace] [$key] = $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
protected function updateState(Identifier $identifier, $state, $message = null) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
// set object on the session |
89
|
|
|
$data = new StorageData($identifier, $state, $message); |
90
|
|
|
$key = $identifier->getId(); |
91
|
|
|
$_SESSION [$this->namespace] [$key] = $data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function isPersisted(Identifier $identifier) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$key = $identifier->getId(); |
100
|
|
|
if (!isset($_SESSION [$this->namespace] [$key])) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function getEntityIds($machine, $state = null) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$ids = array(); |
112
|
|
|
foreach ($_SESSION [$this->namespace] as $key => $storage) { |
113
|
|
|
if (strstr($key, $machine)) { |
114
|
|
|
if ($state) { |
115
|
|
|
if ($storage->state === $state) { |
116
|
|
|
$ids [] = $storage->id; |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
$ids [] = $storage->id; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $ids; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: