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