-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistry.php
160 lines (145 loc) · 3.69 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
<?php
namespace Rad\DependencyInjection;
use ArrayAccess;
use Serializable;
use JsonSerializable;
use Rad\Core\SingletonTrait;
/**
* Registry
*
* @package Rad\DependencyInjection
*/
class Registry implements ArrayAccess, Serializable, JsonSerializable
{
protected static $storage = [self::DEFAULT_SCOPE => []];
const DEFAULT_SCOPE = 'default';
/**
* Store key in registry
*
* @param string $key
* @param mixed $value
* @param string $scope
*/
public static function set($key, $value, $scope = self::DEFAULT_SCOPE)
{
self::$storage[$scope][$key] = $value;
}
/**
* Get key from registry
*
* @param string $key
* @param string $scope
*
* @return null
*/
public static function get($key, $scope = self::DEFAULT_SCOPE)
{
if (isset(self::$storage[$scope][$key])) {
return self::$storage[$scope][$key];
}
return null;
}
/**
* Check key exist or not
*
* @param string $key
* @param string $scope
*
* @return bool
*/
public static function has($key, $scope = self::DEFAULT_SCOPE)
{
return isset(self::$storage[$scope][$key]);
}
/**
* @param $key
* @param string $scope
*/
public static function remove($key, $scope = self::DEFAULT_SCOPE)
{
unset(self::$storage[$scope][$key]);
}
/**
* Whether a offset exists
*
* @param mixed $offset An offset to check for.
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @return boolean true on success or false on failure.
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Offset to retrieve
*
* @param mixed $offset The offset to retrieve.
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Offset to set
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @return void
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Offset to unset
*
* @param mixed $offset The offset to unset.
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @return void
*/
public function offsetUnset($offset)
{
$this->remove($offset);
}
/**
* String representation of object
*
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
*/
public function serialize()
{
return serialize(self::$storage);
}
/**
* Constructs the object
*
* @param string $serialized The string representation of the object.
*
* @link http://php.net/manual/en/serializable.unserialize.php
* @return void
*/
public function unserialize($serialized)
{
self::$storage = unserialize($serialized);
}
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by json_encode,
* which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return self::$storage;
}
}