1 | <?php |
||
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) { |
||
31 | 16 | ||
32 | 16 | public function wrapComposed($id, callable $wrapped) { |
|
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) { |
|
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)); |
|
51 | 40 | ||
52 | public function wrap($id, $wrapper) { |
||
61 | 44 | ||
62 | public function get($id) { |
||
75 | |||
76 | public function has($id) { |
||
79 | |||
80 | private function unbox($box, Container $c) { |
||
87 | } |
||
88 |