Skip to content

Commit

Permalink
Merge pull request #31 from blackshadev/docs-builtin-dependency
Browse files Browse the repository at this point in the history
docs: Add built-in type dependencies allong with tagged collections
  • Loading branch information
brendt authored Oct 8, 2024
2 parents 371acf8 + 9aca629 commit 90363c3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/Front/Docs/Content/framework/02-the-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 90363c3

Please sign in to comment.