MslsRegistry::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 = [];
22
23
	/**
24
	 * Instance
25
	 *
26
	 * @var MslsRegistry
27
	 */
28
	private static $instance;
29
30
	/**
31
	 * Get an object by key
32
	 *
33
	 * @param string $key
34
	 *
35
	 * @return mixed
36
	 */
37
	private function get( $key ) {
38
		return isset( self::$arr[ $key ] ) ? self::$arr[ $key ] : null;
39
	}
40
41
	/**
42
	 * Set an object
43
	 *
44
	 * @param string $key
45
	 * @param mixed $instance
46
	 */
47
	private function set( $key, $instance ) {
48
		self::$arr[ $key ] = $instance;
49
	}
50
51
	/**
52
	 * Registry is a singleton
53
	 *
54
	 * @return MslsRegistry
55
	 */
56
	public static function instance() {
57
		if ( ! isset( self::$instance ) ) {
58
			self::$instance = new self();
59
		}
60
61
		return self::$instance;
62
	}
63
64
	/**
65
	 * Static get_object calls get
66
	 *
67
	 * @param string $key
68
	 *
69
	 * @return mixed
70
	 */
71
	public static function get_object( $key ) {
72
		return self::instance()->get( $key );
73
	}
74
75
	/**
76
	 * Static set_object calls set
77
	 *
78
	 * @param string $key
79
	 * @param mixed $instance
80
	 */
81
	public static function set_object( $key, $instance ) {
82
		self::instance()->set( $key, $instance );
83
	}
84
85
}
86