Registry   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 54.17%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 116
ccs 13
cts 24
cp 0.5417
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 2
A set() 0 4 1
A get() 0 8 2
A isRegistered() 0 7 2
A init() 0 3 1
A __construct() 0 3 1
A setInstance() 0 6 2
1
<?php
2
/**
3
 * Registry class
4
 */
5
namespace Phile\Core;
6
7
/**
8
 * the Registry class for implementing a registry
9
 *
10
 * @author  Frank Nägler
11
 * @link    https://philecms.github.io
12
 * @license http://opensource.org/licenses/MIT
13
 * @package Phile\Core
14
 */
15
class Registry extends \ArrayObject
16
{
17
    /**
18
     * Registry object provides storage for shared objects.
19
     *
20
     * @var Registry
21
     */
22
    private static $registry = null;
23
24
    /**
25
     * Retrieves the default registry instance.
26
     *
27
     * @return Registry
28
     */
29 57
    public static function getInstance()
30
    {
31 57
        if (self::$registry === null) {
32
            self::init();
33
        }
34
35 57
        return self::$registry;
36
    }
37
38
    /**
39
     * Set the default registry instance to a specified instance.
40
     *
41
     * @param Registry $registry An object instance of type Registry,
42
     *                           or a subclass.
43
     *
44
     * @throws \Exception
45
     */
46
    public static function setInstance(Registry $registry)
47
    {
48
        if (self::$registry !== null) {
49
            throw new \Exception('Registry is already initialized', 1398536572);
50
        }
51
        self::$registry = $registry;
52
    }
53
54
    /**
55
     * Initialize the default registry instance.
56
     *
57
     * @return void
58
     */
59
    protected static function init()
60
    {
61
        self::setInstance(new Registry());
62
    }
63
64
    /**
65
     * getter method, basically same as offsetGet().
66
     *
67
     * This method can be called from an object of type Registry, or it
68
     * can be called statically.  In the latter case, it uses the default
69
     * static instance stored in the class.
70
     *
71
     * @param string $index - get the value associated with $index
72
     *
73
     * @return mixed
74
     * @throws \Exception if no entry is registerd for $index.
75
     */
76 54
    public static function get($index)
77
    {
78 54
        $instance = self::getInstance();
79 54
        if (!$instance->offsetExists($index)) {
80
            throw new \Exception("No entry is registered for key '$index'", 1398536594);
81
        }
82
83 54
        return $instance->offsetGet($index);
84
    }
85
86
    /**
87
     * setter method, basically same as offsetSet().
88
     *
89
     * This method can be called from an object of type Registry, or it
90
     * can be called statically.  In the latter case, it uses the default
91
     * static instance stored in the class.
92
     *
93
     * @param string $index The location in the ArrayObject in which to store
94
     *                      the value.
95
     * @param mixed  $value The object to store in the ArrayObject.
96
     *
97
     * @return void
98
     */
99 52
    public static function set($index, $value)
100
    {
101 52
        $instance = self::getInstance();
102 52
        $instance->offsetSet($index, $value);
103
    }
104
105
    /**
106
     * Returns TRUE if the $index is a named value in the registry,
107
     * or FALSE if $index was not found in the registry.
108
     *
109
     * @param string $index
110
     *
111
     * @return boolean
112
     */
113 2
    public static function isRegistered($index)
114
    {
115 2
        if (self::$registry === null) {
116
            return false;
117
        }
118
119 2
        return self::$registry->offsetExists($index);
120
    }
121
122
    /**
123
     * the constructor
124
     *
125
     * @param array   $array data array
126
     * @param integer $flags ArrayObject flags
127
     */
128
    public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
129
    {
130
        parent::__construct($array, $flags);
131
    }
132
}
133