Completed
Pull Request — v4.0-dev (#195)
by
unknown
12:00 queued 10:34
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 40
    public function add($id, $value) {
11 40
        if (array_key_exists($id, $this->cached)) {
12
            throw new \LogicException('Cannot add service after it has been frozen.');
13
        }
14 40
        $this->boxes[$id] = [$value, $value instanceof \Closure ? true : false];
15 40
    }
16
    public function addComposed($id, callable $define_composers) {
17 12
        $this->add($id, function($c) use ($id) {
18 12
            return compose(...array_values($c->get($id . '.composers')));
19 12
        });
20 12
        $this->add($id . '.composers', $define_composers);
21 12
    }
22 12
    public function wrapComposed($id, callable $wrapped) {
23 12
        $this->wrap($id . '.composers', $wrapped);
24 12
    }
25
    public function addStack($id, callable $define_stack) {
26 12
        $this->add($id, function($c) use ($id) {
27 12
            return stack($c->get($id . '.stack'));
28 12
        });
29 12
        $this->add($id . '.stack', $define_stack);
30 12
    }
31 12
    public function wrapStack($id, callable $wrapped) {
32 12
        $this->wrap($id . '.stack', $wrapped);
33 12
    }
34 12
    public function merge($id, array $values) {
35 12
        $old = $this->get($id);
36 12
        $this->add($id, array_merge($old, $values));
37 12
    }
38 16
    public function wrap($id, $wrapper) {
39 16
        if (!$this->has($id)) {
40
            throw new \LogicException('Cannot wrap service ' . $id . ' that does not exist.');
41
        }
42 16
        $box = $this->boxes[$id];
43 16
        $this->boxes[$id] = [function($c) use ($box, $wrapper) {
44 16
            return $wrapper($this->unbox($box, $c), $c);
45 16
        }, true];
46 16
    }
47 36
    public function get($id) {
48 36
        if (array_key_exists($id, $this->cached)) {
49 16
            return $this->cached[$id];
50
        }
51 36
        if (!$this->has($id)) {
52
            throw new \LogicException('Cannot retrieve service ' . $id . ' that does exist.');
53
        }
54 36
        $result = $this->unbox($this->boxes[$id], $this);
55 36
        if ($this->boxes[$id][1]) { // only cache services
56 28
            $this->cached[$id] = $result;
57
        }
58 36
        return $result;
59
    }
60 40
    public function has($id) {
61 40
        return array_key_exists($id, $this->boxes);
62
    }
63 36
    private function unbox($box, Container $c) {
64 36
        list($value, $is_factory) = $box;
65 36
        if (!$is_factory) {
66 28
            return $value;
67
        }
68 28
        return $value($c);
69
    }
70
}
71