-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMreg.php
More file actions
82 lines (76 loc) · 1.73 KB
/
Mreg.php
File metadata and controls
82 lines (76 loc) · 1.73 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
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage Mreg
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* This is a registry class
* @see registry design pattern
*
*/
class Mreg
{
static private $_registry = array ();
/**
* Enregistre un objet dans le registre
*
* @param string $key
* @param objet $obj
*/
static private function forceSet ($key, $obj)
{
self::$_registry[$key] = $obj;
}
static public function set ($key, $obj)
{
// Verifie les doublons de clefs
if (array_key_exists ($key, self::$_registry)) {
throw new Exception ("{$key} alerady exists");
}
// Verifie s’il s’agit bien d’un objet
if (!is_object ($obj) && !is_array($obj)) {
throw new Exception ("Only objects or arrays can be registered");
}
// pas de probleme, on enregistre
self::$_registry[$key] = $obj;
}
/**
* Restaure un objet enregistré
*
* @param string $key
* @return objet
*/
static public function &get ($key)
{
if (!array_key_exists ($key, self::$_registry)) {
throw new Exception ("no {$key} object");
}
return self::$_registry[$key];
}
static public function append($key,$array)
{
if(!is_array($array)) {
throw new Exception ("Mreg can only append arrays (tried to append a non-array to {$key})");
}
try {
$res = self::get($key);
} catch(Exception $e) {
$res = array();
}
if(!is_array($res)) {
throw new Exception ("registered element {$key} is not an array. Cannot append");
}
$res = array_merge($res,$array);
try {
self::forceSet($key,$res);
} catch(Exception $e) {
// do nothing more
}
}
}