Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

Registry::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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.com
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 37
    public static function getInstance()
30
    {
31 37
        if (self::$registry === null) {
32
            self::init();
33
        }
34
35 37
        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
     * @param Registry $registry
45
     *
46
     * @throws \Exception
47
     */
48
    public static function setInstance(Registry $registry)
49
    {
50
        if (self::$registry !== null) {
51
            throw new \Exception('Registry is already initialized', 1398536572);
52
        }
53
        self::$registry = $registry;
54
    }
55
56
    /**
57
     * Initialize the default registry instance.
58
     *
59
     * @return void
60
     */
61
    protected static function init()
62
    {
63
        self::setInstance(new Registry());
64
    }
65
66
    /**
67
     * getter method, basically same as offsetGet().
68
     *
69
     * This method can be called from an object of type Registry, or it
70
     * can be called statically.  In the latter case, it uses the default
71
     * static instance stored in the class.
72
     *
73
     * @param string $index - get the value associated with $index
74
     *
75
     * @return mixed
76
     * @throws \Exception if no entry is registerd for $index.
77
     */
78 34
    public static function get($index)
79
    {
80 34
        $instance = self::getInstance();
81 34
        if (!$instance->offsetExists($index)) {
82
            throw new \Exception("No entry is registered for key '$index'", 1398536594);
83
        }
84
85 34
        return $instance->offsetGet($index);
86
    }
87
88
    /**
89
     * setter method, basically same as offsetSet().
90
     *
91
     * This method can be called from an object of type Registry, or it
92
     * can be called statically.  In the latter case, it uses the default
93
     * static instance stored in the class.
94
     *
95
     * @param string $index The location in the ArrayObject in which to store
96
     *                      the value.
97
     * @param mixed  $value The object to store in the ArrayObject.
98
     *
99
     * @return void
100
     */
101 21
    public static function set($index, $value)
102
    {
103 21
        $instance = self::getInstance();
104 21
        $instance->offsetSet($index, $value);
105 21
    }
106
107
    /**
108
     * Returns TRUE if the $index is a named value in the registry,
109
     * or FALSE if $index was not found in the registry.
110
     *
111
     * @param string $index
112
     *
113
     * @return boolean
114
     */
115 14
    public static function isRegistered($index)
116
    {
117 14
        if (self::$registry === null) {
118
            return false;
119
        }
120
121 14
        return self::$registry->offsetExists($index);
122
    }
123
124
    /**
125
     * the constructor
126
     *
127
     * @param array   $array data array
128
     * @param integer $flags ArrayObject flags
129
     */
130
    public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
131
    {
132
        parent::__construct($array, $flags);
133
    }
134
135
    /**
136
     * method to check if offset exists
137
     *
138
     * @param string $index
139
     *
140
     * @returns mixed
141
     *
142
     * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
143
     */
144 36
    public function offsetExists($index)
145
    {
146 36
        return array_key_exists($index, $this);
147
    }
148
}
149