/
Registry.php
270 lines (220 loc) · 5.29 KB
/
Registry.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
class Request_1
{
}
class Request_2
{
}
class RegistryСlassic
{
private static $instance;
private $values = [];
private function __construct()
{
}
static function instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
function get($key)
{
if (isset($this->values[$key])) {
return $this->values[$key];
}
return null;
}
function set($key, $value)
{
$this->values[$key] = $value;
}
}
$reg = RegistryСlassic::instance();
$reg->set('request', new Request_1);
$reg = RegistryСlassic::instance();
print_r($reg->get('request'));
// ************************************************** //
// ************************************************** //
// ************************************************** //
abstract class Registry
{
abstract protected function get($key);
abstract protected function set($key, $val);
}
class RequestRegistry extends Registry
{
private $values = [];
private static $instance;
private function __construct()
{
}
static function instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
protected function get($key)
{
if (isset($this->values[$key])) {
return $this->values[$key];
}
return null;
}
protected function set($key, $value)
{
$this->values[$key] = $value;
}
static function getRequest()
{
return self::instance()->get('request');
}
static function setRequest(Request_2 $request)
{
return self::instance()->set('request', $request);
}
}
RequestRegistry::setRequest(new Request_2);
print_r(RequestRegistry::getRequest());
class SessionRegistry extends Registry
{
private static $instance;
private function __construct()
{
//session_start();
}
static function instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
function get($key)
{
if (isset($_SESSION[__CLASS__][$key])) {
return $_SESSION[__CLASS__][$key];
}
return null;
}
function set($key, $value)
{
$_SESSION[__CLASS__][$key] = $value;
}
static function getComplex()
{
return self::instance()->get('complex');
}
static function setComplex(Complex $complex)
{
return self::instance()->set('complex', $complex);
}
}
class Complex
{
}
SessionRegistry::setComplex(new Complex);
print_r(SessionRegistry::getComplex());
class ApplicationRegistry extends Registry
{
private static $instance;
private $freezedir = "data";
private $values = [];
private $mtimes = [];
private function __construct()
{
}
static function instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
function get($key)
{
$path = $this->freezedir . DIRECTORY_SEPARATOR . $key;
if (file_exists($path)) {
clearstatcache();
$mtime = filemtime($path);
if (!isset($this->mtimes[$key])) {
$this->mtimes[$key] = 0;
}
if ($mtime > $this->mtimes[$key]) {
$data = file_get_contents($path);
$this->mtimes[$key] = $mtime;
return ($this->values[$key] = unserialize($data));
}
}
if (isset($this->values[$key])) {
return $this->values[$key];
}
return null;
}
protected function set($key, $value)
{
$this->values[$key] = $value;
$path = $this->freezedir . DIRECTORY_SEPARATOR . $key;
file_put_contents($path, serialize($value));
$this->mtimes[$key] = time();
}
static function getDSN()
{
return self::instance()->get('dsn');
}
static function setDSN($dsn)
{
return self::instance()->set('dsn', $dsn);
}
}
class DSN
{
}
ApplicationRegistry::setDSN(new DSN);
print_r(ApplicationRegistry::getDSN());
class MemApplicationRegistry extends Registry
{
private static $instance;
private $values = [];
private $id;
const DSN = 1;
private function __construct()
{
$this->id = @shm_attach(55, 10000, 0600);
if (!$this->id) {
throw new Exception("Нет доступа к общей памяти");
}
}
static function instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
protected function get($key)
{
return shm_get_var($this->id, $key);
}
protected function set($key, $value)
{
return shm_put_var($this->id, $key, $value);
}
static function getDSN()
{
return self::instance()->get(self::DSN);
}
static function setDSN($dsn)
{
return self::instance()->set(self::DSN, $dsn);
}
}
try {
MemApplicationRegistry::setDSN(new DSN);
print_r(MemApplicationRegistry::getDSN());
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "\n";
}