Container   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 19

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 51
cts 54
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 2
A add() 0 6 3
A addComposed() 0 6 1
A wrapComposed() 0 3 1
A addStack() 0 6 1
A wrapStack() 0 3 1
A merge() 0 4 1
A wrap() 0 9 2
A get() 0 13 4
A has() 0 3 1
A unbox() 0 7 2
1
<?php
2
3
namespace League\Plates\Util;
4
5
final class Container
6
{
7
    private $boxes = [];
8
    private $cached = [];
9
10 20
    public static function create(array $defs) {
11 20
        $c = new self();
12 20
        foreach ($defs as $key => $val) {
13 20
            $c->add($key, $val);
14
        }
15 20
        return $c;
16
    }
17
18 48
    public function add($id, $value) {
19 48
        if (array_key_exists($id, $this->cached)) {
20
            throw new \LogicException('Cannot add service after it has been frozen.');
21
        }
22 48
        $this->boxes[$id] = [$value, $value instanceof \Closure ? true : false];
23 48
    }
24
25
    public function addComposed($id, callable $define_composers) {
26 20
        $this->add($id, function($c) use ($id) {
27 20
            return compose(...array_values($c->get($id . '.composers')));
28 20
        });
29 20
        $this->add($id . '.composers', $define_composers);
30 20
    }
31
32 20
    public function wrapComposed($id, callable $wrapped) {
33 20
        $this->wrap($id . '.composers', $wrapped);
34 20
    }
35
36
    public function addStack($id, callable $define_stack) {
37 20
        $this->add($id, function($c) use ($id) {
38 20
            return stack($c->get($id . '.stack'));
39 20
        });
40 20
        $this->add($id . '.stack', $define_stack);
41 20
    }
42
43 20
    public function wrapStack($id, callable $wrapped) {
44 20
        $this->wrap($id . '.stack', $wrapped);
45 20
    }
46
47 20
    public function merge($id, array $values) {
48 20
        $old = $this->get($id);
49 20
        $this->add($id, array_merge($old, $values));
50 20
    }
51
52 24
    public function wrap($id, $wrapper) {
53 24
        if (!$this->has($id)) {
54
            throw new \LogicException('Cannot wrap service ' . $id . ' that does not exist.');
55
        }
56 24
        $box = $this->boxes[$id];
57 24
        $this->boxes[$id] = [function($c) use ($box, $wrapper) {
58 24
            return $wrapper($this->unbox($box, $c), $c);
59 24
        }, true];
60 24
    }
61
62 44
    public function get($id) {
63 44
        if (array_key_exists($id, $this->cached)) {
64 24
            return $this->cached[$id];
65
        }
66 44
        if (!$this->has($id)) {
67
            throw new \LogicException('Cannot retrieve service ' . $id . ' that does exist.');
68
        }
69 44
        $result = $this->unbox($this->boxes[$id], $this);
70 44
        if ($this->boxes[$id][1]) { // only cache services
71 36
            $this->cached[$id] = $result;
72
        }
73 44
        return $result;
74
    }
75
76 48
    public function has($id) {
77 48
        return array_key_exists($id, $this->boxes);
78
    }
79
80 44
    private function unbox($box, Container $c) {
81 44
        list($value, $is_factory) = $box;
82 44
        if (!$is_factory) {
83 36
            return $value;
84
        }
85 36
        return $value($c);
86
    }
87
}
88