1 | <?php |
||
19 | abstract class AbstractContainer implements ContainerInterface |
||
20 | { |
||
21 | /** Default logic function name */ |
||
22 | const LOGIC_FUNCTION_NAME = 'diContainer'; |
||
23 | |||
24 | /** @var array[string] Collection of alias => class name for alias resolving */ |
||
25 | protected $aliases = array(); |
||
26 | |||
27 | /** @var array[string] Collection of class name dependencies trees */ |
||
28 | protected $dependencies = array(); |
||
29 | |||
30 | /** @var ContainerInterface[] Collection of delegated containers */ |
||
31 | protected $delegates = array(); |
||
32 | |||
33 | /** @var array[string] Collection of dependency parameters */ |
||
34 | protected $parameters = array(); |
||
35 | |||
36 | /** @var \samsonphp\generator\Generator */ |
||
37 | protected $generator; |
||
38 | |||
39 | /** @var callable Logic function */ |
||
40 | protected $logicCallable; |
||
41 | |||
42 | /** |
||
43 | * Container constructor. |
||
44 | * |
||
45 | * @param Generator $generator |
||
46 | 6 | */ |
|
47 | public function __construct(Generator $generator) |
||
51 | |||
52 | /** |
||
53 | * Internal logic handler. Calls generated logic function |
||
54 | * for performing entity creation or search. This is encapsulated |
||
55 | * method for further overriding. |
||
56 | * |
||
57 | * @param string $alias Entity alias |
||
58 | * |
||
59 | * @return mixed Created instance or null |
||
60 | 6 | * @throws ContainerException |
|
61 | */ |
||
62 | 6 | protected function logic($alias) |
|
70 | |||
71 | /** |
||
72 | * Finds an entry of the container by its identifier and returns it. |
||
73 | * |
||
74 | * @param string $alias Identifier of the entry to look for. |
||
75 | * |
||
76 | * @throws \Interop\Container\Exception\NotFoundException |
||
77 | 3 | * @throws \Interop\Container\Exception\ContainerException |
|
78 | * @throws \samsonframework\di\exception\ContainerException |
||
79 | 3 | * @throws \samsonframework\di\exception\ClassNotFoundException |
|
80 | 1 | * |
|
81 | * @return mixed Entry. |
||
82 | */ |
||
83 | 2 | public function get($alias) |
|
107 | |||
108 | 1 | /** |
|
109 | * Implementing delegate lookup feature. |
||
110 | * If current container cannot resolve entity dependency |
||
111 | 1 | * resolving process is passed to delegated container. |
|
112 | 1 | * |
|
113 | * @param ContainerInterface $container Container for delegate lookup |
||
114 | 2 | */ |
|
115 | 1 | public function delegate(ContainerInterface $container) |
|
119 | |||
120 | /** |
||
121 | * Returns true if the container can return an entry for the given identifier. |
||
122 | * Returns false otherwise. |
||
123 | * |
||
124 | * @param string $alias Identifier of the entry to look for. |
||
125 | * |
||
126 | * @return boolean |
||
127 | */ |
||
128 | 6 | public function has($alias) |
|
143 | 1 | ||
144 | 1 | /** |
|
145 | * Generate logic conditions and their implementation for container and its delegates. |
||
146 | 1 | * |
|
147 | 1 | * @param string $inputVariable Input condition parameter variable name |
|
148 | 1 | * @param bool|false $started Flag if condition branching has been started |
|
149 | 1 | */ |
|
150 | public function generateConditions($inputVariable = '$alias', $started = false) |
||
182 | |||
183 | 2 | /** |
|
184 | 2 | * Generate dependency injection logic function. |
|
185 | * |
||
186 | * @param string $functionName |
||
187 | 1 | * |
|
188 | * @return string PHP logic function code |
||
189 | 1 | */ |
|
190 | 1 | public function build($functionName = self::LOGIC_FUNCTION_NAME) |
|
210 | 2 | ||
211 | /** |
||
212 | * Set container dependency. |
||
213 | 2 | * |
|
214 | * @param mixed $entity Entity |
||
215 | * @param array $parameters Collection of additional parameters |
||
216 | 1 | * @param string|null $alias Entity alias for simple finding |
|
217 | 1 | * |
|
218 | 1 | * @return $this Chaining |
|
219 | 1 | */ |
|
220 | abstract public function set($entity, array $parameters, $alias = null); |
||
221 | |||
222 | /** |
||
223 | * Generate container dependency condition code. |
||
224 | * |
||
225 | * @param string $alias Entity alias |
||
226 | * @param mixed $entity Entity |
||
227 | */ |
||
228 | abstract protected function generateCondition($alias, &$entity); |
||
229 | } |
||
230 |