-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
InjectExtension.php
174 lines (149 loc) 路 5.12 KB
/
InjectExtension.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI\Extensions;
use Nette;
use Nette\DI;
use Nette\DI\Definitions;
use Nette\Utils\Reflection;
/**
* Calls inject methods and fills @inject properties.
*/
final class InjectExtension extends DI\CompilerExtension
{
public const TAG_INJECT = 'nette.inject';
public function getConfigSchema(): Nette\Schema\Schema
{
return Nette\Schema\Expect::structure([]);
}
public function beforeCompile()
{
foreach ($this->getContainerBuilder()->getDefinitions() as $def) {
if ($def->getTag(self::TAG_INJECT)) {
$def = $def instanceof Definitions\FactoryDefinition
? $def->getResultDefinition()
: $def;
if ($def instanceof Definitions\ServiceDefinition) {
$this->updateDefinition($def);
}
}
}
}
private function updateDefinition(Definitions\ServiceDefinition $def): void
{
$resolvedType = (new DI\Resolver($this->getContainerBuilder()))->resolveEntityType($def->getFactory());
$class = is_subclass_of($resolvedType, $def->getType())
? $resolvedType
: $def->getType();
$setups = $def->getSetup();
foreach (self::getInjectProperties($class) as $property => $type) {
$builder = $this->getContainerBuilder();
$inject = new Definitions\Statement('$' . $property, [Definitions\Reference::fromType((string) $type)]);
foreach ($setups as $key => $setup) {
if ($setup->getEntity() === $inject->getEntity()) {
$inject = $setup;
$builder = null;
unset($setups[$key]);
}
}
self::checkType($class, $property, $type, $builder, $def);
array_unshift($setups, $inject);
}
foreach (array_reverse(self::getInjectMethods($class)) as $method) {
$inject = new Definitions\Statement($method);
foreach ($setups as $key => $setup) {
if ($setup->getEntity() === $inject->getEntity()) {
$inject = $setup;
unset($setups[$key]);
}
}
array_unshift($setups, $inject);
}
$def->setSetup($setups);
}
/**
* Generates list of inject methods.
* @internal
*/
public static function getInjectMethods(string $class): array
{
$classes = [];
foreach (get_class_methods($class) as $name) {
if (substr($name, 0, 6) === 'inject') {
$classes[$name] = (new \ReflectionMethod($class, $name))->getDeclaringClass()->name;
}
}
$methods = array_keys($classes);
uksort($classes, function (string $a, string $b) use ($classes, $methods): int {
return $classes[$a] === $classes[$b]
? array_search($a, $methods, true) <=> array_search($b, $methods, true)
: (is_a($classes[$a], $classes[$b], true) ? 1 : -1);
});
return array_keys($classes);
}
/**
* Generates list of properties with annotation @inject.
* @internal
*/
public static function getInjectProperties(string $class): array
{
$res = [];
foreach (get_class_vars($class) as $name => $foo) {
$rp = new \ReflectionProperty($class, $name);
$hasAttr = PHP_VERSION_ID >= 80000 && $rp->getAttributes(DI\Attributes\Inject::class);
if ($hasAttr || DI\Helpers::parseAnnotation($rp, 'inject') !== null) {
if ($type = Reflection::getPropertyType($rp)) {
} elseif (!$hasAttr && ($type = DI\Helpers::parseAnnotation($rp, 'var'))) {
if (strpos($type, '|') !== false) {
throw new Nette\InvalidStateException('The ' . Reflection::toString($rp) . ' is not expected to have a union type.');
}
$type = Reflection::expandClassName($type, Reflection::getPropertyDeclaringClass($rp));
}
$res[$name] = $type;
}
}
ksort($res);
return $res;
}
/**
* Calls all methods starting with with "inject" using autowiring.
* @param object $service
*/
public static function callInjects(DI\Container $container, $service): void
{
if (!is_object($service)) {
throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
}
foreach (self::getInjectMethods(get_class($service)) as $method) {
$container->callMethod([$service, $method]);
}
foreach (self::getInjectProperties(get_class($service)) as $property => $type) {
self::checkType($service, $property, $type, $container);
$service->$property = $container->getByType($type);
}
}
/**
* @param object|string $class
* @param DI\Container|DI\ContainerBuilder|null $container
*/
private static function checkType(
$class,
string $name,
?string $type,
$container,
Definitions\Definition $def = null
): void {
$propName = Reflection::toString(new \ReflectionProperty($class, $name));
$desc = $def ? $def->getDescriptiveName() . "\n" : '';
if (!$type) {
throw new Nette\InvalidStateException($desc . "Property $propName has no type.");
} elseif (!class_exists($type) && !interface_exists($type)) {
throw new Nette\InvalidStateException($desc . "Class '$type' required by $propName not found.\nCheck the property type and 'use' statements.");
} elseif ($container && !$container->getByType($type, false)) {
throw new Nette\DI\MissingServiceException($desc . "Service of type $type required by $propName not found.\nDid you add it to configuration file?");
}
}
}