-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInjector.php
More file actions
137 lines (112 loc) · 3.98 KB
/
Injector.php
File metadata and controls
137 lines (112 loc) · 3.98 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <vection@davidlung.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\DependencyInjection;
use Vection\Contracts\DependencyInjection\InjectorInterface;
use Vection\Contracts\DependencyInjection\ResolverInterface;
/**
* Class Injector
*
* @package Vection\Component\DependencyInjection
*
* @author David M. Lung <vection@davidlung.de>
*/
class Injector implements InjectorInterface
{
protected Container $container;
/** @var object[] */
protected array $infiniteLoopPreventedObjects = [];
protected ResolverInterface $resolver;
/**
* Injector constructor.
*
* @param Container $container
* @param ResolverInterface $resolver
*/
public function __construct(Container $container, ResolverInterface $resolver)
{
$this->container = $container;
$this->resolver = $resolver;
}
/**
* Injects all dependencies defined by the given object.
*
* @param object $object
*/
public function injectDependencies(object $object): void
{
$this->infiniteLoopPreventedObjects[get_class($object)] = $object;
$this->injectViaSetter($object);
$this->injectViaAnnotations($object);
$this->injectViaMagic($object);
unset($this->infiniteLoopPreventedObjects[get_class($object)]);
# If object uses ContainerAwareTrait, inject the container itself
if ( method_exists($object, '__setContainer') ) {
$object->__setContainer($this->container);
}
}
/**
* @param object $object
*/
protected function injectViaSetter(object $object): void
{
$id = get_class($object);
$classDependencies = $this->resolver->getClassDependencies($id);
foreach ($classDependencies['setter'] ?? [] as $setter => $dependency) {
if (isset($this->infiniteLoopPreventedObjects[$dependency])) {
$dependencyObject = $this->infiniteLoopPreventedObjects[$dependency];
} else {
// @phpstan-ignore-next-line
$dependencyObject = $this->container->get($dependency);
}
$object->$setter($dependencyObject);
}
}
/**
* @param object $object
*/
protected function injectViaAnnotations(object $object): void
{
$id = get_class($object);
$dependencies = [];
$classDependencies = $this->resolver->getClassDependencies($id);
if ($classDependencies['annotation'] ?? false) {
foreach ($classDependencies['annotation'] ?? [] as $property => $dependency ) {
if (isset($this->infiniteLoopPreventedObjects[$dependency])) {
$dependencies[$property] = $this->infiniteLoopPreventedObjects[$dependency];
} else {
// @phpstan-ignore-next-line
$dependencies[$property] = $this->container->get($dependency);
}
}
$object->__annotationInjection($dependencies);
}
}
/**
* @param object $object
*/
protected function injectViaMagic(object $object): void
{
$id = get_class($object);
$classDependencies = $this->resolver->getClassDependencies($id);
if ($classDependencies['magic'] ?? false) {
$dependencies = [];
foreach ( $classDependencies['magic'] as $dependency ) {
if (isset($this->infiniteLoopPreventedObjects[$dependency])) {
$dependencies[] = $this->infiniteLoopPreventedObjects[$dependency];
} else {
// @phpstan-ignore-next-line
$dependencies[] = $this->container->get($dependency);
}
}
$object->__inject(...$dependencies);
}
}
}