|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Attribute Resolution |
| 4 | +sections: |
| 5 | + Introduction: introduction |
| 6 | + Usage: usage |
| 7 | + Extending Attributes: extending-attributes |
| 8 | + Benefits: benefits |
| 9 | +--- |
| 10 | +## Introduction |
| 11 | + |
| 12 | +> Note: Attribute resolution is turned off by default but can be turned on by registering the `ReflectionContainer` as a container delegate. Read below and see the [documentation on delegate containers](/5.x/delegate-containers/). |
| 13 | +
|
| 14 | +Attribute resolution allows you to use PHP attributes to control how dependencies are resolved for your services. This provides a powerful and flexible alternative to auto wiring, enabling you to inject values, services, or even custom logic directly into your constructors or methods using attributes. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +The container provides built-in attributes for common resolution scenarios: |
| 19 | + |
| 20 | +- `#[Inject('service.id')]` — Injects a value or service from the container by its ID. |
| 21 | +- `#[Resolve('resolver.id', 'path.to.value')]` — Resolves a value from a service or array in the container, traversing the given path. |
| 22 | + - Method calls are supported in the path, allowing you to resolve complex values or configurations. |
| 23 | + - e.g. `#[Resolve('config', 'getDbConfig.host')]` |
| 24 | + |
| 25 | +### Using `Inject` |
| 26 | + |
| 27 | +~~~php |
| 28 | +<?php |
| 29 | + |
| 30 | +declare(strict_types=1); |
| 31 | + |
| 32 | +namespace Acme; |
| 33 | + |
| 34 | +use League\Container\Attribute\Inject; |
| 35 | + |
| 36 | +class Bar { |
| 37 | + public function hello(): string |
| 38 | + { |
| 39 | + return 'hello'; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class Foo |
| 44 | +{ |
| 45 | + public function __construct( |
| 46 | + #[Inject('bar')] Bar $bar |
| 47 | + ) { |
| 48 | + $this->bar = $bar; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +$container = new League\Container\Container(); |
| 53 | +$container->add('bar', new Bar()); |
| 54 | +$container->delegate(new League\Container\ReflectionContainer()); |
| 55 | + |
| 56 | +$foo = $container->get(Foo::class); |
| 57 | +echo $foo->bar->hello(); // 'hello' |
| 58 | +~~~ |
| 59 | + |
| 60 | +### Using `Resolve` |
| 61 | + |
| 62 | +~~~php |
| 63 | +<?php |
| 64 | + |
| 65 | +use League\Container\Attribute\Resolve; |
| 66 | + |
| 67 | +class Bar {} |
| 68 | + |
| 69 | +class Config { |
| 70 | + public array $settings = [ |
| 71 | + 'db' => [ |
| 72 | + 'host' => 'localhost', |
| 73 | + 'user' => 'root', |
| 74 | + ] |
| 75 | + ]; |
| 76 | +} |
| 77 | + |
| 78 | +class Baz |
| 79 | +{ |
| 80 | + public function __construct( |
| 81 | + #[Resolve('config', 'settings.db.host')] public string $dbHost |
| 82 | + ) { |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +$container = new League\Container\Container(); |
| 87 | +$container->add('config', new Config()); |
| 88 | +$container->delegate(new League\Container\ReflectionContainer()); |
| 89 | + |
| 90 | +$baz = $container->get(Baz::class); |
| 91 | +// $baz->dbHost === 'localhost' |
| 92 | +~~~ |
| 93 | + |
| 94 | +## Extending Attributes |
| 95 | + |
| 96 | +You can create your own attributes to implement custom resolution logic. To access the container within your attribute, implement `ContainerAwareInterface` and use the `ContainerAwareTrait`. This gives you access to `$this->getContainer()`. |
| 97 | + |
| 98 | +For example, to inject an environment variable: |
| 99 | + |
| 100 | +~~~php |
| 101 | +<?php |
| 102 | + |
| 103 | +use Attribute; |
| 104 | +use League\Container\Attribute\AttributeInterface; |
| 105 | +use League\Container\ContainerAwareInterface; |
| 106 | +use League\Container\ContainerAwareTrait; |
| 107 | + |
| 108 | +#[Attribute(Attribute::TARGET_PARAMETER)] |
| 109 | +class Env implements AttributeInterface, ContainerAwareInterface |
| 110 | +{ |
| 111 | + use ContainerAwareTrait; |
| 112 | + |
| 113 | + public function __construct(private string $name) {} |
| 114 | + |
| 115 | + public function resolve(): string |
| 116 | + { |
| 117 | + // You can access the container if needed via $this->getContainer() |
| 118 | + return getenv($this->name) ?: ''; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +class NeedsSecret |
| 123 | +~~~ |
| 124 | + |
| 125 | +## Benefits |
| 126 | + |
| 127 | +Attribute resolution offers several advantages over auto wiring: |
| 128 | + |
| 129 | +- **Fine-grained control:** Specify exactly how each dependency should be resolved, including primitives, services, or custom logic. |
| 130 | +- **Extensibility:** Create your own attributes to integrate with configuration, environment, or any other source. Attributes can access the container for advanced scenarios. |
| 131 | +- **Clarity:** Resolution logic is explicit and self-documenting in your code, making dependencies easier to understand and maintain. |
| 132 | +- **Beyond constructor injection:** Unlike auto wiring, attribute resolution is not limited to objects or constructor arguments—you can resolve scalars, arrays, or any value your attribute logic supports. |
| 133 | + |
| 134 | +While auto wiring is convenient for simple object graphs and constructor injection, attribute resolution is ideal for more complex scenarios where you need precise control over how dependencies are provided. |
0 commit comments