forked from Respect/Config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutowire.php
More file actions
48 lines (38 loc) · 1.36 KB
/
Autowire.php
File metadata and controls
48 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace Respect\Config;
use Psr\Container\ContainerInterface;
use Respect\Parameter\Resolver;
class Autowire extends Instantiator
{
protected ContainerInterface|null $container = null;
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}
/** @inheritDoc */
protected function cleanupParams(array $params, bool $forConstructor = true): array
{
$constructor = $this->reflection()->getConstructor();
$container = $this->container;
if ($forConstructor && $constructor && $container) {
foreach ($params as $name => $value) {
if ($value instanceof Ref) {
$params[$name] = $container->get($value->id);
} else {
$this->propagateContainer($value);
$params[$name] = $this->lazyLoad($value);
}
}
return $this->stripTrailingNulls((new Resolver($container))->resolveNamed($constructor, $params));
}
return parent::cleanupParams($params);
}
protected function propagateContainer(mixed $value): void
{
if (!$value instanceof self || $value->container !== null || $this->container === null) {
return;
}
$value->setContainer($this->container);
}
}