Skip to content

Commit e639ccf

Browse files
committed
added PHP 8 attribute Inject
1 parent c5a116c commit e639ccf

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/DI/Attributes/Inject.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\DI\Attributes;
11+
12+
use Attribute;
13+
14+
15+
#[Attribute(Attribute::TARGET_PROPERTY)]
16+
class Inject
17+
{
18+
}

src/DI/DependencyChecker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class_uses($name),
122122
$prop->name,
123123
$prop->getDocComment(),
124124
Reflection::getPropertyTypes($prop),
125+
PHP_VERSION_ID >= 80000 ? count($prop->getAttributes(Attributes\Inject::class)) : null,
125126
];
126127
}
127128
}

src/DI/Extensions/InjectExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ public static function getInjectProperties(string $class): array
112112
$res = [];
113113
foreach (get_class_vars($class) as $name => $foo) {
114114
$rp = new \ReflectionProperty($class, $name);
115-
if (DI\Helpers::parseAnnotation($rp, 'inject') !== null) {
115+
$hasAttr = PHP_VERSION_ID >= 80000 && $rp->getAttributes(DI\Attributes\Inject::class);
116+
if ($hasAttr || DI\Helpers::parseAnnotation($rp, 'inject') !== null) {
116117
if ($type = Reflection::getPropertyType($rp)) {
117-
} elseif ($type = DI\Helpers::parseAnnotation($rp, 'var')) {
118+
} elseif (!$hasAttr && ($type = DI\Helpers::parseAnnotation($rp, 'var'))) {
118119
if (strpos($type, '|') !== false) {
119120
throw new Nette\InvalidStateException('The ' . Reflection::toString($rp) . ' is not expected to have a union type.');
120121
}

tests/DI/InjectExtension.getInjectProperties().php80.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ class AClass
1919
public AClass|\stdClass $var;
2020
}
2121

22+
class EClass
23+
{
24+
#[\Nette\DI\Attributes\Inject]
25+
public EInjected $varA;
26+
}
27+
2228

2329
Assert::exception(function () {
2430
InjectExtension::getInjectProperties(AClass::class);
2531
}, Nette\InvalidStateException::class, 'The AClass::$var is not expected to have a union type.');
32+
33+
Assert::same([
34+
'varA' => 'EInjected',
35+
], InjectExtension::getInjectProperties(EClass::class));

0 commit comments

Comments
 (0)