Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsRegistry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A __clone() 0 2 1
A get() 0 3 2
A set() 0 3 1
A instance() 0 7 2
A get_object() 0 3 1
A set_object() 0 3 1
1
<?php
2
/**
3
 * MslsRegistry
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Registry instead of singletons
12
 * @package Msls
13
 */
14
class MslsRegistry {
15
16
	/**
17
	 * Generic container
18
	 *
19
	 * @var array
20
	 */
21
	private static $arr = array();
22
23
	/**
24
	 * Instance
25
	 *
26
	 * @var MslsRegistry
27
	 */
28
	private static $instance;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * Don't call me directly!
34
	 *
35
	 * @codeCoverageIgnore
36
	 */
37
	final private function __construct() {
38
	}
39
40
	/**
41
	 * Clone
42
	 *
43
	 * Don't call me directly!
44
	 *
45
	 * @codeCoverageIgnore
46
	 */
47
	final private function __clone() {
48
	}
49
50
	/**
51
	 * Get an object by key
52
	 *
53
	 * @param string $key
54
	 *
55
	 * @return mixed
56
	 */
57
	private function get( $key ) {
58
		return ( isset( self::$arr[ $key ] ) ? self::$arr[ $key ] : null );
59
	}
60
61
	/**
62
	 * Set an object
63
	 *
64
	 * @param string $key
65
	 * @param mixed $instance
66
	 */
67
	private function set( $key, $instance ) {
68
		self::$arr[ $key ] = $instance;
69
	}
70
71
	/**
72
	 * Registry is a singleton
73
	 *
74
	 * @return MslsRegistry
75
	 */
76
	public static function instance() {
77
		if ( ! isset( self::$instance ) ) {
78
			self::$instance = new self();
79
		}
80
81
		return self::$instance;
82
	}
83
84
	/**
85
	 * Static get_object calls get
86
	 *
87
	 * @param string $key
88
	 *
89
	 * @return mixed
90
	 */
91
	public static function get_object( $key ) {
92
		return self::instance()->get( $key );
93
	}
94
95
	/**
96
	 * Static set_object calls set
97
	 *
98
	 * @param string $key
99
	 * @param mixed $instance
100
	 */
101
	public static function set_object( $key, $instance ) {
102
		self::instance()->set( $key, $instance );
103
	}
104
105
}
106