|
11 | 11 | use function ltrim; |
12 | 12 |
|
13 | 13 | /** |
14 | | - * Use this annotation to autowire a service from the container into a given parameter of a field/query/mutation. |
15 | | - * |
16 | | - * @Annotation |
17 | | - * @Target({"METHOD", "ANNOTATION"}) |
18 | | - * @Attributes({ |
19 | | - * @Attribute("for", type = "string"), |
20 | | - * @Attribute("identifier", type = "string") |
21 | | - * }) |
| 14 | + * Use this attribute to autowire a service from the container into a given parameter of a field/query/mutation. |
22 | 15 | */ |
23 | 16 | #[Attribute(Attribute::TARGET_PARAMETER)] |
24 | 17 | class Autowire implements ParameterAnnotationInterface |
25 | 18 | { |
26 | | - /** @var string|null */ |
27 | | - private $for; |
28 | | - /** @var string|null */ |
29 | | - private $identifier; |
30 | | - |
31 | | - /** @param array<string, mixed>|string $identifier */ |
32 | | - public function __construct(array|string $identifier = []) |
| 19 | + private string|null $for = null; |
| 20 | + private string|null $identifier = null; |
| 21 | + |
| 22 | + /** @param array<string, mixed>|string $params */ |
| 23 | + public function __construct( |
| 24 | + array|string $params = [], |
| 25 | + string|null $for = null, |
| 26 | + string|null $identifier = null, |
| 27 | + ) |
33 | 28 | { |
34 | | - $values = $identifier; |
| 29 | + $values = $params; |
35 | 30 | if (is_string($values)) { |
36 | 31 | $this->identifier = $values; |
37 | 32 | } else { |
38 | | - $this->identifier = $values['identifier'] ?? $values['value'] ?? null; |
39 | | - if (isset($values['for'])) { |
40 | | - $this->for = ltrim($values['for'], '$'); |
| 33 | + $this->identifier = $identifier ?? $values['identifier'] ?? $values['value'] ?? null; |
| 34 | + if (isset($values['for']) || $for !== null) { |
| 35 | + $this->for = ltrim($for ?? $values['for'], '$'); |
41 | 36 | } |
42 | 37 | } |
43 | 38 | } |
44 | 39 |
|
45 | 40 | public function getTarget(): string |
46 | 41 | { |
47 | 42 | if ($this->for === null) { |
48 | | - throw new BadMethodCallException('The @Autowire annotation must be passed a target. For instance: "@Autowire(for="$myService")"'); |
| 43 | + throw new BadMethodCallException('The #[Autowire] attribute must be passed a target. For instance: "#[Autowire(for: "$myService")]"'); |
49 | 44 | } |
50 | 45 | return $this->for; |
51 | 46 | } |
|
0 commit comments