Dependency injection
Avocado implements DI based on class scaning which search for Resource annotations. Itself provides a: Leaf, Configuration, RestController, Filter, Controller, Resource classes that can be provided into DI consumers.
WARNING
Dependency injection components cannot have any constructor arguments.
Leaf and Resource
Classes marked as Leaf or Resource are in thought defined for creating simple components that can be needed in other components. For example when you need a user validation based on configuration properties you can define a Leaf that has a Configuration and for it self provides some functionality.
Each resource can have many leafs that are childs for that resource. You can autowire a leaf class or resourece to get all leafs in it.
#[Resource(name: "user-validator")] // but can be resource also
// #[Resource] // now leaf doesnt have name. Is defined by its type.
// #[Resource("user-validator")] // now leaf is `user-validator`
class MockedResourceWithAutowiredProperties {
#[Autowired(name: 'user-config')]
private AnotherDependency $anotherDependency;
public function __construct() {}
public function valideSomething(int $something): boolean {
return $something == $this->anotherDependency->getTest();
}
#[Leaf(name: "another-component")]
public function someLeaf(): SomeLeafClass {
}
}Every leaf should be of its own class. If you want two leafs of the same class you can give them name so DI manager handle it properly.