forked from dszulist/mkphp-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.php
More file actions
119 lines (108 loc) · 2.24 KB
/
Registry.php
File metadata and controls
119 lines (108 loc) · 2.24 KB
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
<?php
/**
* MK_Registry
*
* Klasa oparta na wzoru Rejestr
*
* @category MK
* @package MK_Registry
* @author lwinnicki
*/
class MK_Registry extends ArrayObject
{
/**
* @var mixed
*/
private static $registry = null;
/**
*
* @param array $array
* @param int $flags
*
* @internal param $ #P#C\ArrayObject.ARRAY_AS_PROPS|\type|? $flags
*/
public function __construct(array $array = array(), $flags = parent::ARRAY_AS_PROPS)
{
parent::__construct($array, $flags);
}
/**
* Zwraca instancję reestru
*
* @return MK_Registry
*/
public static function getInstance()
{
if (self::$registry === null) {
self::setInstance(new MK_Registry());
}
return self::$registry;
}
/**
* Tworzy instancję Rejestru
*
* @param MK_Registry $registry
*
* @throws MK_Exception
*/
public static function setInstance(MK_Registry $registry)
{
if (self::$registry !== null) {
throw new MK_Exception('Rejestr już jest utworzony');
}
self::$registry = $registry;
}
/**
* Zwraca wartość, o podanej nazwie, zapisaną w rejestrze.
* Jeżeli nie istnieje zwraca wyjątek
*
* @param String $index
*
* @throws MK_Exception
* @return Mixed
*/
public static function get($index)
{
$instance = self::getInstance();
if (!$instance->offsetExists($index)) {
throw new MK_Exception('Nie istnieje wartość dla klucza ' . $index);
}
return $instance->offsetGet($index);
}
/**
* Wstawia podaną wartość do rejestru o podanym kluczu
*
* @param String $index
* @param Mixed $value
*/
public static function set($index, $value)
{
$instance = self::getInstance();
$instance->offsetSet($index, $value);
}
/**
* Sprawdza czy istnieje wartość o podanym indeksie
*
* @param String $index
*
* @return Boolean
*/
public static function isRegistered($index)
{
if (self::$registry === null) {
return false;
}
return self::$registry->offsetExists($index);
}
/**
* (non-PHPdoc)
* @see ArrayObject::offsetExists()
*
* @param $index
*
* @return bool
*/
public function offsetExists($index)
{
return array_key_exists($index, $this);
}
}