-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateManager.class.php
125 lines (102 loc) · 2.64 KB
/
StateManager.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
//ALL STATE CLASS NAMES NEED TO BE LINKED: $ACTION_CLASSES
require_once("State.class.php");
require_once("UnboundState.class.php");
StateManager::getInstance()->initialize();
class StateManager {
protected static $instance = null;
protected $STATE_CLASSES = ["State","UnboundState"];
private $STATE_LOCATION = "./states/";
private $STATE_MIME_TYPE = "state";
private $states = [];
public static function getInstance(){
if(self::$instance === null){
self::$instance = new self;
}
return self::$instance;
}
public static function get(){
return self::getInstance();
}
protected function __construct(){}
protected function __clone(){}
public function initialize() {
$files = glob($this->STATE_LOCATION."*.{".$this->STATE_MIME_TYPE."}", GLOB_BRACE);
foreach($files as $file){
$state = unserialize(file_get_contents($file));
$this->states[$state->getId()] = $state;
}
}
public function copy($state) {
if(is_numeric($state)) {
$state = $this->getStateById($state);
}
if ($state !== null) {
$copy = clone $state;
$copy->setId( null );
return $copy;
}
return null;
}
public function save($stateOrId) {
$state = $stateOrId;
if(is_numeric($stateOrId)) {
$state = $this->getStateById($stateOrId);
}
if ($state !== null) {
if ($state->getId() === null || $state->getId()<0) {
$newId = $this->getNextId();
$state->setId( $newId );
}
$bytes = serialize($state);
$filepath = $this->STATE_LOCATION.$state->getId().".".$this->STATE_MIME_TYPE;
$successBytes = file_put_contents($filepath, $bytes);
if ($successBytes !== false && $successBytes > 0){
$this->states[$state->getId()] = $state;
return $state;
}
}
return null;
}
public function delete($stateOrId) {
if(is_numeric($stateOrId)) {
$id = $stateOrId;
} else {
$id = $stateOrId->getId();
}
$deletionSuccess = unlink($this->STATE_LOCATION.$id.".".$this->STATE_MIME_TYPE);
if ($deletionSuccess) {
unset($this->states[$id]);
}
}
public function getStateById($id = null) {
if ($id !== null && array_key_exists($id, $this->states)) {
return $this->states[$id];
}
return null;
}
public function getStates() {
return $this->states;
}
public function getStateClassNames() {
return $this->STATE_CLASSES;
}
public function getListOfAllStateIdsAndNames() {
$result = [];
foreach ($this->states as $state) {
$result[$state->getId()] = $state->getName();
}
asort($result);
return $result;
}
private function getNextId(){
$nextId = 0;
foreach ($this->states as $state) {
if ($state->getId() >= $nextId) {
$nextId = $state->getId()+1;
}
}
return $nextId;
}
}
?>