-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRegistry.php
118 lines (106 loc) · 2.47 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
<?php
/**
* @author Paul Dragoonis <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT
* @package Core
* @link www.ppiframework.com
*/
class PPI_Registry {
/**
* Registry object provides storage for shared objects.
* @var object $_instance
*/
private static $_instance = null;
/**
* The registrys internal data
*
* @var array
*/
protected static $_vars = array();
/**
* @param array $array Initial data for the registry
*/
public function __construct() {}
/**
* Retrieves the default instance of the registry, if it doesn't exist then we create it.
*
* @return PPI_Registry
*/
public static function getInstance() {
if (self::$_instance === null) {
self::legacyInit();
}
return self::$_instance;
}
/**
* Set the default registry instance to a specified instance.
*
* @param object $registry An object instance of type PPI_Registry
* @return void
* @throws PPI_Exception if registry is already initialized.
*/
public static function setInstance($registry) {
if (self::$_instance !== null) {
throw new PPI_Exception('Registry is already initialized');
}
self::$_instance = $registry;
}
/**
* Initialize the default registry instance.
*
* @return void
*/
protected static function legacyInit() {
self::setInstance(new PPI_Registry_Legacy());
}
/**
* Initialisation function, currently used to set mock data
*
* @static
* @param array $options
* @return void
*/
public static function init(array $options = array()) {
if(isset($options['data']) && !empty($options['data'])) {
self::$_vars = $options['data'];
}
}
/**
* Get a value from the registey
*
* @param string $index
* @return mixed
* @throws PPI_Exception if no entry is registerd for $index.
*/
public static function get($key, $default = null) {
return ifset(self::$_vars[$key], $default);
}
/**
* Set a value in the registry
*
* @param string $index
* @param mixed $value The object to store in the ArrayObject.
* @return void
*/
public static function set($index, $value) {
self::$_vars[$index] = $value;
}
/**
* Removes an offset from the registry
*
* @param string $index
* @return void
*/
public static function remove($index) {
unset(self::$_vars[$index]);
}
/**
* Checks if an offset exists in the registry
*
* @param string $index
* @return mixed
*/
public static function exists($index) {
return array_key_exists($index, self::$_vars);
}
}