Skip to content

Commit 8871acd

Browse files
Improved resolving #[Inject] attribute's value
1 parent 6bfa1c3 commit 8871acd

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

Diff for: src/Attribute/Inject.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace Rade\DI\Attribute;
1919

20+
use Rade\DI\Definitions\Parameter;
21+
use Rade\DI\Definitions\TaggedLocator;
2022
use Rade\DI\Resolver;
2123

2224
/**
@@ -27,15 +29,23 @@
2729
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::TARGET_PARAMETER)]
2830
final class Inject
2931
{
32+
public const
33+
VALUE = 0,
34+
REFERENCE = 1,
35+
PARAMETER = 2;
36+
37+
private int $type;
38+
3039
/** @var mixed */
3140
private $value;
3241

3342
/**
3443
* @param mixed $value
3544
*/
36-
public function __construct($value = null)
45+
public function __construct($value = null, $type = self::REFERENCE)
3746
{
3847
$this->value = $value;
48+
$this->type = $type;
3949
}
4050

4151
/**
@@ -45,14 +55,22 @@ public function __construct($value = null)
4555
*/
4656
public function resolve(Resolver $resolver, string $typeName = null)
4757
{
48-
if (\is_string($value = $this->value ?? $typeName)) {
58+
if (null === $value = $this->value ?? $typeName) {
59+
return null;
60+
}
61+
62+
if ($value instanceof TaggedLocator) {
63+
throw new \LogicException(\sprintf('Use the #[%s] attribute instead for lazy loading tags.', Tagged::class));
64+
}
65+
66+
if (self::REFERENCE === $this->type) {
4967
return $resolver->resolveReference($value);
5068
}
5169

52-
if (null !== $value) {
53-
return $resolver->resolve($value);
70+
if (self::PARAMETER === $this->type) {
71+
$value = new Parameter($value, true);
5472
}
5573

56-
return $value;
74+
return $resolver->resolve($value);
5775
}
5876
}

0 commit comments

Comments
 (0)