Registery.php
Gesponsorde koppelingen
PHP script bestanden
Code (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
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
<?php
/**
* The registery class
*
* @author Wouter J <http://wouterj.nl>
* @license Creative Commons 3.0 <http://creativecommons.org/licenses/by-sa/3.0/>
*/
class Registery implements \ArrayAccess
{
protected static $values = array();
// De basis methods
public static function set( $key, $value )
{
if( self::exists($key) )
throw new \OutOfBoundsException(sprintf('The key (%s) has already been set, destroy it first', $key));
self::$values[$key] = $value;
}
public static function get( $key )
{
if( !self::exists($key) )
throw new \OutOfBoundsException(sprintf('The key %s does not exists', $key));
return self::$values[$key];
}
public static function destroy( $key )
{
unset(self::$values[$key]);
}
public static function exists( $key )
{
return array_key_exists($key, self::$values);
}
// De magic shortcut methods
public function __get( $key )
{
return self::get($key);
}
public function __set( $key, $value )
{
return self::set($key, $value);
}
public function __call( $key, array $arguments = array() )
{
if( !is_callable(self::get($key)) )
throw new \InvalidArgumentException(sprintf('The key %s is not a closure', $key));
return call_user_func_array(self::get($key), $arguments);
}
public function __unset( $key )
{
return self::destroy($key);
}
public function __isset( $key )
{
return self::exists($key);
}
// ArrayAccess methods
public function offsetGet( $key )
{
return self::get($key);
}
public function offsetSet( $key, $value )
{
return self::set($key, $value);
}
public function offsetUnset( $key )
{
return self::destroy($key);
}
public function offsetExists( $key )
{
return self::exists($key);
}
}
/**
* The registery class
*
* @author Wouter J <http://wouterj.nl>
* @license Creative Commons 3.0 <http://creativecommons.org/licenses/by-sa/3.0/>
*/
class Registery implements \ArrayAccess
{
protected static $values = array();
// De basis methods
public static function set( $key, $value )
{
if( self::exists($key) )
throw new \OutOfBoundsException(sprintf('The key (%s) has already been set, destroy it first', $key));
self::$values[$key] = $value;
}
public static function get( $key )
{
if( !self::exists($key) )
throw new \OutOfBoundsException(sprintf('The key %s does not exists', $key));
return self::$values[$key];
}
public static function destroy( $key )
{
unset(self::$values[$key]);
}
public static function exists( $key )
{
return array_key_exists($key, self::$values);
}
// De magic shortcut methods
public function __get( $key )
{
return self::get($key);
}
public function __set( $key, $value )
{
return self::set($key, $value);
}
public function __call( $key, array $arguments = array() )
{
if( !is_callable(self::get($key)) )
throw new \InvalidArgumentException(sprintf('The key %s is not a closure', $key));
return call_user_func_array(self::get($key), $arguments);
}
public function __unset( $key )
{
return self::destroy($key);
}
public function __isset( $key )
{
return self::exists($key);
}
// ArrayAccess methods
public function offsetGet( $key )
{
return self::get($key);
}
public function offsetSet( $key, $value )
{
return self::set($key, $value);
}
public function offsetUnset( $key )
{
return self::destroy($key);
}
public function offsetExists( $key )
{
return self::exists($key);
}
}