Completed
Push — master ( eed05d...5ff1a2 )
by James Ekow Abaka
01:21
created

Bindings::asSingleton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ntentan\panie;
4
5
/**
6
 * Holds all the bindings for the dependency injection.
7
 */
8
class Bindings
9
{
10
11
    /**
12
     * An array of all the bindings.
13
     * @var array
14
     */
15
    private $bindings = [];
16
    
17
    /**
18
     * An active key that would be altered with subsequent calls to the bindings object.
19
     * @var string 
20
     */
21
    private $activeKey;
22
23 5
    public function setActiveKey($activeKey)
24
    {
25 5
        $this->activeKey = $activeKey;
26 5
        return $this;
27
    }
28
29
    public function call($method, $parameters = [])
30
    {
31
        $this->bindings[$this->activeKey]['calls'][] = ['setter' => $method, 'parameters' => $parameters];
32
    }
33
34
    public function setProperty($property, $binding)
35
    {
36
        $this->bindings[$this->activeKey]['sets'][] = ['property' => $property, 'binding' => $binding];
37
    }
38
39 5
    public function to($value)
40
    {
41 5
        $this->bindings[$this->activeKey] = ['binding' => $value, 'calls'=>[], 'properties' => []];
42 5
        return $this;
43
    }
44
45 5
    public function get($key)
46
    {
47 5
        return $this->bindings[$key];
48
    }
49
50 7
    public function has($key)
51
    {
52 7
        return isset($this->bindings[$key]);
53
    }
54
55 1
    public function asSingleton()
56
    {
57 1
        $this->bindings[$this->activeKey]['singleton'] = true;
58 1
    }    
59
60
    public function merge($bindings, $replace = true)
61
    {
62
        foreach($bindings as $key => $binding) {
63
            if(isset($this->bindings[$key]) && !$replace) {
64
                continue;
65
            }
66
            if(is_array($binding)) {
67
                $this->bindings[$key] = ['binding' => $binding[0]];
68
                if(isset($binding['singleton'])) {
69
                    $this->bindings[$key]['singleton'] = $binding['singleton'];
70
                }
71
            } else {
72
                $this->bindings[$key] = ['binding' => $binding];
73
            }
74
        }
75
    }
76
77
}
78