Registry::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace DICIT;
3
4
/**
5
 * Registry for storing built object instances.
6
 * @author Olivier Madre
7
 * @author Thibaud Fabre
8
 *
9
 */
10
class Registry
11
{
12
    protected $data = array();
13
14
    /**
15
     * Flush all stored instances from the registry.
16
     * @return \DICIT\Registry
17
     */
18
    public function flush()
19
    {
20
        $this->data = array();
21
    }
22
23
    /**
24
     * Fetches an object from the registry.
25
     * @param string $key
26
     * @return mixed
27
     */
28
    public function get($key, $throwIfNotFound = false)
29
    {
30
        if ($this->has($key)) {
31
            return $this->data[$key];
32
        }
33
        else if ($throwIfNotFound) {
34
            throw new \RuntimeException('Key ' . $key . ' not found in DI Container registry');
35
        }
36
        else {
37
            return null;
38
        }
39
    }
40
41
    /**
42
     * Returns a boolean indicating whether there is an object associated to a given key in the registry.
43
     * @param string $key
44
     * @return boolean
45
     */
46
    public function has($key)
47
    {
48
        return array_key_exists($key, $this->data);
49
    }
50
51
    /**
52
     * Stores an object instance in the registry.
53
     * @param string $key
54
     * @param mixed $value
55
     */
56
    public function set($key, & $value)
57
    {
58
        $this->data[$key] = & $value;
59
    }
60
}
61