From 9aca6293d97cdb2826d9a2429941e8850add8cd3 Mon Sep 17 00:00:00 2001 From: Vincent Hagen Date: Mon, 7 Oct 2024 21:30:26 +0200 Subject: [PATCH] docs: Add built-in type dependencies allong with tagged collections --- .../Content/framework/02-the-container.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/Front/Docs/Content/framework/02-the-container.md b/app/Front/Docs/Content/framework/02-the-container.md index 79380a0..255b6c7 100644 --- a/app/Front/Docs/Content/framework/02-the-container.md +++ b/app/Front/Docs/Content/framework/02-the-container.md @@ -234,6 +234,37 @@ final readonly class BladeInitializer implements DynamicInitializer } ``` +## Built-in types dependencies + +Besides being able to depend on objects, sometimes you'd want to depend on built-in types like `string`, `int` or more often `array`. It is possible to depend on these built-in types, but these cannot be autowired and must be initialized through a [tagged singleton](#content-tagged-singletons). + +For example if we want to group a specific set of validators together as a tagged collection, you can initialize them in a tagged singleton initializer like so: + +```php + +final readonly class BookValidatorsInitializer implements Initializer +{ + #[Singleton(tag: 'book-validators')] + public function initialize(Container $container): array + { + return [ + $container->get(HeaderValidator::class), + $container->get(BodyValidator::class), + $container->get(FooterValidator::class), + ]; + } +} +``` + +Now you can use this group of validators as a normal tagged value in your container: + +```php +final readonly class BookController +{ + public function __constructor(#[Tagged('book-validators') private readonly array $contentValidators) { /* … */ } +} +``` + ## Config As mentioned, configuration is represented by objects in Tempest. Tempest provides many configuration classes for you, although the framework is designed to use them as little as possible. Whenever you need fine-grained control over part of the framework's config, you can create a `Config` folder in your main project folder. This folder can contain plain PHP files, each file can return a config instance: