Skip to content

Commit 55a5e10

Browse files
committed
porting AnnotationBasedAutowiring for php-di v7
1 parent 9d6e0b3 commit 55a5e10

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

src/ContainerBuilder.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ class ContainerBuilder
5252

5353
private bool $useAutowiring = true;
5454

55-
private int $annotationsFlags = 0;
56-
57-
private bool $ignorePhpDocErrors = false;
55+
private int $attributesFlags = 0;
5856

5957
private bool $useAttributes = false;
6058

@@ -225,7 +223,7 @@ public function useAttributes(bool $bool, int $flags = 0) : self
225223
$this->ensureNotLocked();
226224

227225
$this->useAttributes = $bool;
228-
$this->annotationsFlags = $flags;
226+
$this->attributesFlags = $flags;
229227

230228
return $this;
231229
}

src/Definition/AutowireDefinition.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@
99
*/
1010
class AutowireDefinition extends ObjectDefinition
1111
{
12-
/**
13-
* @var bool|null
14-
*/
15-
protected $useAnnotations;
12+
protected ?bool $useAttributes;
1613

1714
/**
18-
* Enable/disable reading annotations for this definition, regardless of a container configuration.
19-
* @param bool $flag
15+
* Enable/disable reading attributes for this definition, regardless of a container configuration.
2016
*/
21-
public function useAnnotations(bool $flag = true)
17+
public function useAttributes(bool $flag = true) : void
2218
{
23-
$this->useAnnotations = $flag;
19+
$this->useAttributes = $flag;
2420
}
2521

2622
/**
27-
* Returns boolean if the useAnnotation flag was explicitly set, otherwise null.
28-
* @return bool|null
23+
* Returns boolean if the useAttributes flag was explicitly set, otherwise null.
2924
*/
30-
public function isUsingAnnotations()
25+
public function isUsingAttributes() : ?bool
3126
{
32-
return $this->useAnnotations;
27+
return $this->useAttributes;
3328
}
3429
}

src/Definition/Helper/AutowireDefinitionHelper.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace DI\Definition\Helper;
66

77
use DI\Definition\AutowireDefinition;
8-
use DI\Definition\Definition;
8+
use DI\Definition\ObjectDefinition;
99

1010
/**
1111
* Helps defining how to create an instance of a class using autowiring.
@@ -16,7 +16,7 @@ class AutowireDefinitionHelper extends CreateDefinitionHelper
1616
{
1717
public const DEFINITION_CLASS = AutowireDefinition::class;
1818

19-
protected $useAnnotations;
19+
protected ?bool $useAttributes = true;
2020

2121
/**
2222
* Defines a value for a specific argument of the constructor.
@@ -74,27 +74,26 @@ public function methodParameter(string $method, string|int $parameter, mixed $va
7474
}
7575

7676
/**
77-
* Define if entry should use annotation reader for reading dependencies.
77+
* Define if entry should use attributes reader for reading dependencies.
7878
* This is turned off by default if autowire() helper is used, and turned on if entry is not defined explicitly in the di config.
79-
* @param bool $useAnnotations
8079
* @return $this
8180
*/
82-
public function useAnnotations(bool $useAnnotations = true)
81+
public function useAttributes(bool $useAttributes = true) : self
8382
{
84-
$this->useAnnotations = $useAnnotations;
83+
$this->useAttributes = $useAttributes;
8584

8685
return $this;
8786
}
8887

8988
/**
9089
* @return AutowireDefinition
9190
*/
92-
public function getDefinition(string $entryName) : Definition
91+
public function getDefinition(string $entryName) : ObjectDefinition
9392
{
9493
/** @var AutowireDefinition $definition */
9594
$definition = parent::getDefinition($entryName);
96-
if ($this->useAnnotations !== null) {
97-
$definition->useAnnotations($this->useAnnotations);
95+
if ($this->useAttributes !== null) {
96+
$definition->useAttributes($this->useAttributes);
9897
}
9998

10099
return $definition;

src/Definition/Source/AttributeBasedAutowiring.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ class AttributeBasedAutowiring implements DefinitionSource, Autowiring
3131
{
3232
// Annotations configuration flags:
3333
// enable on implicit definitions
34-
const IMPLICIT = 1;
34+
public const IMPLICIT = 1;
3535
// enable on all autowire definitions (which are written in DI config) by default
36-
const EXPLICIT = 2;
36+
public const EXPLICIT = 2;
3737
// read @Injectable annotations for classes
38-
const INJECTABLE = 4;
38+
public const INJECTABLE = 4;
3939
// read @Inject annotations for properties
40-
const PROPERTIES = 8;
40+
public const PROPERTIES = 8;
4141
// read @Inject annotations for methods' parameters
42-
const METHODS = 16;
42+
public const METHODS = 16;
4343
// all options enabled
44-
const ALL = 31;
44+
public const ALL = 31;
4545

4646
public function __construct(private int $flags = 0)
4747
{
@@ -57,17 +57,17 @@ public function autowire(string $name, ObjectDefinition $definition = null) : Ob
5757
}
5858

5959
$definition = $definition ?: new ObjectDefinition($name);
60-
$useAnnotations = $definition instanceof AutowireDefinition
61-
? ($definition->isUsingAnnotations() ?? ($this->flags & self::EXPLICIT))
62-
: ($this->flags & self::IMPLICIT);
60+
$useAttributes = $definition instanceof AutowireDefinition
61+
? ($definition->isUsingAttributes() ?? (bool) ($this->flags & self::EXPLICIT))
62+
: (bool) ($this->flags & self::IMPLICIT);
6363

6464
$class = null;
65-
if ($useAnnotations && $this->flags >= self::INJECTABLE) {
65+
if ($useAttributes && $this->flags >= self::INJECTABLE) {
6666
$class = new ReflectionClass($className);
6767

68-
$this->readInjectableAttribute($class, $definition);
68+
$this->readInjectableAttribute($class, $definition);
6969
if ($this->flags & self::INJECTABLE) {
70-
$this->readInjectableAnnotation($class, $definition);
70+
$this->readInjectableAttribute($class, $definition);
7171
}
7272

7373
// Browse the class properties looking for annotated properties
@@ -83,8 +83,8 @@ public function autowire(string $name, ObjectDefinition $definition = null) : Ob
8383

8484
// constructor parameters should always be read, even if annotations are disabled (completely or i.a. for methods)
8585
// so that it behaves at least as ReflectionBasedAutowiring
86-
if (!$useAnnotations || !($this->flags & self::METHODS)) {
87-
$class = $class ?? new ReflectionClass($className);
86+
if (!$useAttributes || !($this->flags & self::METHODS)) {
87+
$class ??= new ReflectionClass($className);
8888
$this->readConstructor($class, $definition);
8989
}
9090

@@ -109,7 +109,7 @@ public function getDefinitions() : array
109109
}
110110

111111
/**
112-
* Browse the class properties looking for annotated properties.
112+
* Browse the class properties looking for properties with attributes.
113113
*/
114114
private function readProperties(ReflectionClass $class, ObjectDefinition $definition) : void
115115
{
@@ -182,7 +182,7 @@ private function readProperty(ReflectionProperty $property, ObjectDefinition $de
182182
}
183183

184184
/**
185-
* Browse the object's methods looking for annotated methods.
185+
* Browse the object's methods looking for methods with attributes.
186186
*/
187187
private function readMethods(ReflectionClass $class, ObjectDefinition $objectDefinition) : void
188188
{
@@ -214,17 +214,17 @@ private function getMethodInjection(ReflectionMethod $method) : ?MethodInjection
214214
if ($attribute) {
215215
/** @var Inject $inject */
216216
$inject = $attribute->newInstance();
217-
$annotationParameters = $inject->getParameters();
217+
$attributeParameters = $inject->getParameters();
218218
} elseif ($method->isConstructor()) {
219219
// #[Inject] on constructor is implicit, we continue
220-
$annotationParameters = [];
220+
$attributeParameters = [];
221221
} else {
222222
return null;
223223
}
224224

225225
$parameters = [];
226226
foreach ($method->getParameters() as $index => $parameter) {
227-
$entryName = $this->getMethodParameter($index, $parameter, $annotationParameters);
227+
$entryName = $this->getMethodParameter($index, $parameter, $attributeParameters);
228228

229229
if ($entryName !== null) {
230230
$parameters[$index] = new Reference($entryName);
@@ -241,7 +241,7 @@ private function getMethodInjection(ReflectionMethod $method) : ?MethodInjection
241241
/**
242242
* @return string|null Entry name or null if not found.
243243
*/
244-
private function getMethodParameter(int $parameterIndex, ReflectionParameter $parameter, array $annotationParameters) : ?string
244+
private function getMethodParameter(int $parameterIndex, ReflectionParameter $parameter, array $attributeParameters) : ?string
245245
{
246246
// Let's check if this parameter has an #[Inject] attribute
247247
$attribute = $parameter->getAttributes(Inject::class)[0] ?? null;
@@ -253,11 +253,11 @@ private function getMethodParameter(int $parameterIndex, ReflectionParameter $pa
253253
}
254254

255255
// #[Inject] has definition for this parameter (by index, or by name)
256-
if (isset($annotationParameters[$parameterIndex])) {
257-
return $annotationParameters[$parameterIndex];
256+
if (isset($attributeParameters[$parameterIndex])) {
257+
return $attributeParameters[$parameterIndex];
258258
}
259-
if (isset($annotationParameters[$parameter->getName()])) {
260-
return $annotationParameters[$parameter->getName()];
259+
if (isset($attributeParameters[$parameter->getName()])) {
260+
return $attributeParameters[$parameter->getName()];
261261
}
262262

263263
// Skip optional parameters if not explicitly defined

0 commit comments

Comments
 (0)