1 | <?php declare(strict_types=1); |
||
14 | class Builder implements BuilderInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var ContainerInterface |
||
18 | */ |
||
19 | protected $container; |
||
20 | |||
21 | /** |
||
22 | * @var DependenciesResolver |
||
23 | */ |
||
24 | protected $dependenciesResolver; |
||
25 | |||
26 | /** |
||
27 | * @var ArgumentsBuilder |
||
28 | */ |
||
29 | protected $argumentsBuilder; |
||
30 | |||
31 | protected $autoWire = true; |
||
32 | |||
33 | 61 | public function __construct(bool $autoWire = true, ?DependenciesResolverInterface $dependenciesResolver = null) |
|
34 | { |
||
35 | 61 | $this->autoWire = $autoWire; |
|
36 | 61 | $this->dependenciesResolver = $dependenciesResolver ?: new DependenciesResolver(); |
|
37 | 61 | $this->argumentsBuilder = new ArgumentsBuilder(); |
|
38 | 61 | } |
|
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | 48 | public function setContainer(ContainerInterface $container): void |
|
44 | { |
||
45 | 48 | $this->container = $container; |
|
46 | 48 | $this->argumentsBuilder->setContainer($container); |
|
47 | 48 | } |
|
48 | |||
49 | /** |
||
50 | * {@inheritDoc} |
||
51 | */ |
||
52 | 39 | public function construct(DefinitionInterface $definition): object |
|
53 | { |
||
54 | 39 | if ($definition->getFactory()) { |
|
55 | 12 | $object = $this->makeObjectWithFactory($definition); |
|
56 | } else { |
||
57 | 27 | $object = $this->makeObjectSelf($definition); |
|
58 | } |
||
59 | 33 | return $object; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | 10 | public function configure(object $object, DefinitionInterface $definition): object |
|
66 | { |
||
67 | 10 | $this->applyProperties($object, $definition->getProperties()); |
|
68 | 10 | $this->applyCalls($object, $definition->getCalls()); |
|
69 | 10 | return $object; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | 1 | public function invoke(callable $callable, array $arguments = []) |
|
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | 2 | public function inflect(object $object, InflectionInterface $inflection): object |
|
89 | |||
90 | /** |
||
91 | * @param object $object |
||
92 | * @param PropertyInterface[] $properties |
||
93 | */ |
||
94 | 12 | protected function applyProperties(object $object, array $properties): void |
|
100 | |||
101 | /** |
||
102 | * @param object $object |
||
103 | * @param CallInterface[] $calls |
||
104 | * @throws NotFoundException |
||
105 | */ |
||
106 | 12 | protected function applyCalls(object $object, array $calls): void |
|
112 | |||
113 | /** |
||
114 | * @param callable $callable |
||
115 | * @param array $arguments |
||
116 | * @return mixed |
||
117 | * @throws NotFoundException |
||
118 | */ |
||
119 | 5 | protected function call(callable $callable, array $arguments = []) |
|
129 | |||
130 | /** |
||
131 | * Make object without factory |
||
132 | * |
||
133 | * @param DefinitionInterface $definition |
||
134 | * @return mixed |
||
135 | * @throws NotFoundException |
||
136 | */ |
||
137 | 27 | protected function makeObjectSelf(DefinitionInterface $definition) |
|
153 | |||
154 | /** |
||
155 | * Make object with factory |
||
156 | * |
||
157 | * @param DefinitionInterface $definition |
||
158 | * @return mixed |
||
159 | * @throws InvalidConfigurationException |
||
160 | * @throws InvalidFactoryException |
||
161 | * @throws NotFoundException |
||
162 | */ |
||
163 | 12 | protected function makeObjectWithFactory(DefinitionInterface $definition) |
|
182 | |||
183 | /** |
||
184 | * Build callable factory from non-callable |
||
185 | * |
||
186 | * @param DefinitionInterface $definition |
||
187 | * @return callable |
||
188 | * @throws InvalidFactoryException |
||
189 | */ |
||
190 | 8 | protected function buildFactoryFromNonCallable(DefinitionInterface $definition): callable |
|
208 | |||
209 | /** |
||
210 | * Create object with provided arguments and optional construct method |
||
211 | * |
||
212 | * @param string $className |
||
213 | * @param array $arguments |
||
214 | * @param string|null $constructMethod |
||
215 | * @return mixed |
||
216 | */ |
||
217 | 24 | protected function constructObject(string $className, array $arguments, ?string $constructMethod = null) |
|
226 | |||
227 | /** |
||
228 | * Build function attributes for type-value representation |
||
229 | * |
||
230 | * @param array $attributes |
||
231 | * @return Parameter[] |
||
232 | */ |
||
233 | 41 | protected function buildParameters($attributes = []): array |
|
241 | |||
242 | /** |
||
243 | * Create parameter from provided argument |
||
244 | * |
||
245 | * @param $value |
||
246 | * @return ParameterInterface |
||
247 | */ |
||
248 | 18 | protected function buildParameter($value): ParameterInterface |
|
265 | |||
266 | /** |
||
267 | * Try fetch dependency name from string value |
||
268 | * |
||
269 | * @param string $value |
||
270 | * @return string|null |
||
271 | */ |
||
272 | 7 | protected function fetchDependencyId(string $value): ?string |
|
279 | } |
||
280 |