1 | <?php |
||
18 | class Definition implements ArgumentResolverInterface, DefinitionInterface |
||
19 | { |
||
20 | use ArgumentResolverTrait; |
||
21 | use ContainerAwareTrait; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $alias; |
||
27 | |||
28 | /** |
||
29 | * @var mixed |
||
30 | */ |
||
31 | protected $concrete; |
||
32 | |||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $shared = false; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $tags = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $arguments = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $methods = []; |
||
52 | |||
53 | /** |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $resolved; |
||
57 | |||
58 | /** |
||
59 | * @param string $id |
||
60 | * @param mixed|null $concrete |
||
61 | */ |
||
62 | 72 | public function __construct(string $id, $concrete = null) |
|
68 | |||
69 | 9 | public function addTag(string $tag): DefinitionInterface |
|
74 | |||
75 | 9 | public function hasTag(string $tag): bool |
|
79 | |||
80 | 39 | public function setAlias(string $id): DefinitionInterface |
|
85 | |||
86 | 36 | public function getAlias(): string |
|
90 | |||
91 | 9 | public function setShared(bool $shared = true): DefinitionInterface |
|
96 | |||
97 | 9 | public function isShared(): bool |
|
101 | |||
102 | 12 | public function getConcrete() |
|
106 | |||
107 | 3 | public function setConcrete($concrete): DefinitionInterface |
|
113 | |||
114 | 12 | public function addArgument($arg): DefinitionInterface |
|
119 | |||
120 | 3 | public function addArguments(array $args): DefinitionInterface |
|
128 | |||
129 | 3 | public function addMethodCall(string $method, array $args = []): DefinitionInterface |
|
138 | |||
139 | 3 | public function addMethodCalls(array $methods = []): DefinitionInterface |
|
147 | |||
148 | 48 | public function resolve() |
|
156 | |||
157 | 48 | public function resolveNew() |
|
158 | { |
||
159 | 48 | $concrete = $this->concrete; |
|
160 | |||
161 | 48 | if (is_callable($concrete)) { |
|
162 | 12 | $concrete = $this->resolveCallable($concrete); |
|
163 | } |
||
164 | |||
165 | 48 | if ($concrete instanceof LiteralArgumentInterface) { |
|
166 | 3 | $this->resolved = $concrete->getValue(); |
|
167 | 3 | return $concrete->getValue(); |
|
168 | } |
||
169 | |||
170 | 45 | if ($concrete instanceof ArgumentInterface) { |
|
171 | 3 | $concrete = $concrete->getValue(); |
|
172 | } |
||
173 | |||
174 | 45 | if (is_string($concrete) && class_exists($concrete)) { |
|
175 | 36 | $concrete = $this->resolveClass($concrete); |
|
176 | } |
||
177 | |||
178 | 45 | if (is_object($concrete)) { |
|
179 | 42 | $concrete = $this->invokeMethods($concrete); |
|
180 | } |
||
181 | |||
182 | try { |
||
183 | 45 | $container = $this->getContainer(); |
|
184 | 15 | } catch (ContainerException $e) { |
|
185 | 15 | $container = null; |
|
186 | } |
||
187 | |||
188 | // if we still have a string, try to pull it from the container |
||
189 | // this allows for `alias -> alias -> ... -> concrete |
||
190 | 45 | if (is_string($concrete) && $container instanceof ContainerInterface && $container->has($concrete)) { |
|
191 | 3 | $concrete = $container->get($concrete); |
|
192 | } |
||
193 | |||
194 | 45 | $this->resolved = $concrete; |
|
195 | 45 | return $concrete; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param callable $concrete |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 12 | protected function resolveCallable(callable $concrete) |
|
207 | |||
208 | 36 | protected function resolveClass(string $concrete): object |
|
214 | |||
215 | 42 | protected function invokeMethods(object $instance): object |
|
225 | } |
||
226 |