Completed
Push — master ( b3978b...8d3a7c )
by
unknown
26:09 queued 12:30
created

Container::wrapComposed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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