Skip to content

Commit

Permalink
Merge pull request #42 from dotkernel/di-docs
Browse files Browse the repository at this point in the history
added di docs
  • Loading branch information
arhimede authored Jun 21, 2024
2 parents da1a2d5 + 69b3e63 commit 37824bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/book/v5/core-features/dependency-injection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Dependency Injection

Dependency injection is a design pattern used in software development to implement inversion of control. In simpler
terms, it's the act of providing dependencies for an object during instantiation.

In PHP, dependency injection can be implemented in various ways, including through constructor injection, setter
injection and property injection.

DotKernel API, through its [dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package
focuses only on constructor injection.

## Usage

**DotKernel API** comes out of the box with the
[dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package, which provides all we need for
injecting dependencies into any object you want.

`dot-dependency-injection` determines the dependencies by looking at the `#[Inject]` attribute, added to the constructor
of a class. Dependencies are specified as separate parameters of the `#[Inject]` attribute.

For our example we will inject `UserService` and `config` dependencies into a `UseHandler`.

```php
use Dot\DependencyInjection\Attribute\Inject;

class UserHandler implements RequestHandlerInterface
{
#[Inject(
UserService::class,
"config",
)]
public function __construct(
protected UserServiceInterface $userService,
protected array $config,
) {
}
}
```

> If your class needs the value of a specific configuration key, you can specify the path using dot notation:
> `config.example`
The next step is to register the class in the `ConfigProvider` under `factories` using
`Dot\DependencyInjection\Factory\AttributedServiceFactory::class`

```php
public function getDependencies(): array
{
return [
'factories' => [
UserHandler::class => AttributedServiceFactory::class
]
];
}
```

That's it. When your object is instantiated from the container, it will automatically have its
dependencies resolved.

> Dependencies injection is available to any object within DotKernel API. For example, you can inject dependencies in a
> service, a handler and so on, simply by registering it in the `ConfigProvider`.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nav:
- "Content Validation": v5/core-features/content-validation.md
- "Exceptions": v5/core-features/exceptions.md
- "CORS": v5/core-features/cors.md
- "Dependency Injection": v5/core-features/dependency-injection.md
- Commands:
- "Create admin account": v5/commands/create-admin-account.md
- "Generate database migrations": v5/commands/generate-database-migrations.md
Expand Down

0 comments on commit 37824bf

Please sign in to comment.