diff --git a/src/DI/Attributes/Inject.php b/src/DI/Attributes/Inject.php new file mode 100644 index 000000000..325e2b291 --- /dev/null +++ b/src/DI/Attributes/Inject.php @@ -0,0 +1,18 @@ +name, $prop->getDocComment(), Reflection::getPropertyTypes($prop), + PHP_VERSION_ID >= 80000 ? count($prop->getAttributes(Attributes\Inject::class)) : null, ]; } } diff --git a/src/DI/Extensions/InjectExtension.php b/src/DI/Extensions/InjectExtension.php index 84e8c9360..f5a890a58 100644 --- a/src/DI/Extensions/InjectExtension.php +++ b/src/DI/Extensions/InjectExtension.php @@ -112,9 +112,10 @@ public static function getInjectProperties(string $class): array $res = []; foreach (get_class_vars($class) as $name => $foo) { $rp = new \ReflectionProperty($class, $name); - if (DI\Helpers::parseAnnotation($rp, 'inject') !== null) { + $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 ($type = DI\Helpers::parseAnnotation($rp, 'var')) { + } 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.'); } diff --git a/tests/DI/InjectExtension.getInjectProperties().php80.phpt b/tests/DI/InjectExtension.getInjectProperties().php80.phpt index 737280401..d979438c0 100644 --- a/tests/DI/InjectExtension.getInjectProperties().php80.phpt +++ b/tests/DI/InjectExtension.getInjectProperties().php80.phpt @@ -19,7 +19,17 @@ class AClass public AClass|\stdClass $var; } +class EClass +{ + #[\Nette\DI\Attributes\Inject] + public EInjected $varA; +} + Assert::exception(function () { InjectExtension::getInjectProperties(AClass::class); }, Nette\InvalidStateException::class, 'The AClass::$var is not expected to have a union type.'); + +Assert::same([ + 'varA' => 'EInjected', +], InjectExtension::getInjectProperties(EClass::class));