1: | <?php |
2: | // Author: Trabis |
3: | // URL: https://xoops.org |
4: | // E-Mail: lusopoemas@gmail.com |
5: | |
6: | defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
7: | |
8: | /** |
9: | * Class ProtectorRegistry |
10: | */ |
11: | class ProtectorRegistry |
12: | { |
13: | public $_entries; |
14: | public $_locks; |
15: | |
16: | /** |
17: | * ProtectorRegistry constructor. |
18: | */ |
19: | protected function __construct() |
20: | { |
21: | $this->_entries = array(); |
22: | $this->_locks = array(); |
23: | } |
24: | |
25: | /** |
26: | * @return ProtectorRegistry |
27: | */ |
28: | public static function getInstance() |
29: | { |
30: | static $instance = false; |
31: | if (!$instance) { |
32: | $instance = new ProtectorRegistry(); |
33: | } |
34: | |
35: | return $instance; |
36: | } |
37: | |
38: | /** |
39: | * @param $key |
40: | * @param $item |
41: | * |
42: | * @return bool |
43: | */ |
44: | public function setEntry($key, $item) |
45: | { |
46: | if ($this->isLocked($key) == true) { |
47: | trigger_error('Unable to set entry `' . $key . '`. Entry is locked.', E_USER_WARNING); |
48: | |
49: | return false; |
50: | } |
51: | |
52: | $this->_entries[$key] = $item; |
53: | |
54: | return true; |
55: | } |
56: | |
57: | /** |
58: | * @param $key |
59: | */ |
60: | public function unsetEntry($key) |
61: | { |
62: | unset($this->_entries[$key]); |
63: | } |
64: | |
65: | /** |
66: | * @param $key |
67: | * |
68: | * @return null |
69: | */ |
70: | public function getEntry($key) |
71: | { |
72: | if (isset($this->_entries[$key]) == false) { |
73: | return null; |
74: | } |
75: | |
76: | return $this->_entries[$key]; |
77: | } |
78: | |
79: | /** |
80: | * @param $key |
81: | * |
82: | * @return bool |
83: | */ |
84: | public function isEntry($key) |
85: | { |
86: | return ($this->getEntry($key) !== null); |
87: | } |
88: | |
89: | /** |
90: | * @param $key |
91: | * |
92: | * @return bool |
93: | */ |
94: | public function lockEntry($key) |
95: | { |
96: | $this->_locks[$key] = true; |
97: | |
98: | return true; |
99: | } |
100: | |
101: | /** |
102: | * @param $key |
103: | */ |
104: | public function unlockEntry($key) |
105: | { |
106: | unset($this->_locks[$key]); |
107: | } |
108: | |
109: | /** |
110: | * @param $key |
111: | * |
112: | * @return bool |
113: | */ |
114: | public function isLocked($key) |
115: | { |
116: | return (isset($this->_locks[$key]) == true); |
117: | } |
118: | |
119: | public function unsetAll() |
120: | { |
121: | $this->_entries = array(); |
122: | $this->_locks = array(); |
123: | } |
124: | } |
125: |