Skip to content

Commit 37824bf

Browse files
authored
Merge pull request #42 from dotkernel/di-docs
added di docs
2 parents da1a2d5 + 69b3e63 commit 37824bf

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Dependency Injection
2+
3+
Dependency injection is a design pattern used in software development to implement inversion of control. In simpler
4+
terms, it's the act of providing dependencies for an object during instantiation.
5+
6+
In PHP, dependency injection can be implemented in various ways, including through constructor injection, setter
7+
injection and property injection.
8+
9+
DotKernel API, through its [dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package
10+
focuses only on constructor injection.
11+
12+
## Usage
13+
14+
**DotKernel API** comes out of the box with the
15+
[dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package, which provides all we need for
16+
injecting dependencies into any object you want.
17+
18+
`dot-dependency-injection` determines the dependencies by looking at the `#[Inject]` attribute, added to the constructor
19+
of a class. Dependencies are specified as separate parameters of the `#[Inject]` attribute.
20+
21+
For our example we will inject `UserService` and `config` dependencies into a `UseHandler`.
22+
23+
```php
24+
use Dot\DependencyInjection\Attribute\Inject;
25+
26+
class UserHandler implements RequestHandlerInterface
27+
{
28+
#[Inject(
29+
UserService::class,
30+
"config",
31+
)]
32+
public function __construct(
33+
protected UserServiceInterface $userService,
34+
protected array $config,
35+
) {
36+
}
37+
}
38+
```
39+
40+
> If your class needs the value of a specific configuration key, you can specify the path using dot notation:
41+
> `config.example`
42+
43+
The next step is to register the class in the `ConfigProvider` under `factories` using
44+
`Dot\DependencyInjection\Factory\AttributedServiceFactory::class`
45+
46+
```php
47+
public function getDependencies(): array
48+
{
49+
return [
50+
'factories' => [
51+
UserHandler::class => AttributedServiceFactory::class
52+
]
53+
];
54+
}
55+
```
56+
57+
That's it. When your object is instantiated from the container, it will automatically have its
58+
dependencies resolved.
59+
60+
> Dependencies injection is available to any object within DotKernel API. For example, you can inject dependencies in a
61+
> service, a handler and so on, simply by registering it in the `ConfigProvider`.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ nav:
3131
- "Content Validation": v5/core-features/content-validation.md
3232
- "Exceptions": v5/core-features/exceptions.md
3333
- "CORS": v5/core-features/cors.md
34+
- "Dependency Injection": v5/core-features/dependency-injection.md
3435
- Commands:
3536
- "Create admin account": v5/commands/create-admin-account.md
3637
- "Generate database migrations": v5/commands/generate-database-migrations.md

0 commit comments

Comments
 (0)