1 | <?php |
||
19 | abstract class Container implements AspectContainer |
||
20 | { |
||
21 | /** |
||
22 | * List of services in the container |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $values = []; |
||
27 | |||
28 | /** |
||
29 | * Store identifiers os services by tags |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $tags = []; |
||
34 | |||
35 | /** |
||
36 | * Set a service into the container |
||
37 | * |
||
38 | * @param string $id Identifier |
||
39 | * @param mixed $value Value to store |
||
40 | * @param array $tags Additional tags |
||
41 | */ |
||
42 | 10 | public function set(string $id, $value, array $tags = []) |
|
49 | |||
50 | /** |
||
51 | * Set a shared value in the container |
||
52 | * |
||
53 | * @param string $id Identifier |
||
54 | * @param Closure $value Value to store |
||
55 | * @param array $tags Additional tags |
||
56 | */ |
||
57 | public function share(string $id, Closure $value, array $tags = []) |
||
70 | |||
71 | /** |
||
72 | * Return a service or value from the container |
||
73 | * |
||
74 | * @param string $id Identifier |
||
75 | * |
||
76 | * @return mixed |
||
77 | * @throws \OutOfBoundsException if service was not found |
||
78 | */ |
||
79 | 9 | public function get(string $id) |
|
80 | { |
||
81 | 9 | if (!isset($this->values[$id])) { |
|
82 | throw new \OutOfBoundsException("Value {$id} is not defined in the container"); |
||
83 | } |
||
84 | 9 | if (is_callable($this->values[$id])) { |
|
85 | 6 | return $this->values[$id]($this); |
|
86 | } |
||
87 | |||
88 | 8 | return $this->values[$id]; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Checks if item with specified id is present in the container |
||
93 | * |
||
94 | * @param string $id Identifier |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function has(string $id): bool |
||
102 | |||
103 | /** |
||
104 | * Return list of service tagged with marker |
||
105 | * |
||
106 | * @param string $tag Tag to select |
||
107 | * @return array |
||
108 | */ |
||
109 | 3 | public function getByTag(string $tag): array |
|
120 | } |
||
121 |