Skip to content

Commit febe856

Browse files
committed
added folder - config-provider
Signed-off-by: bidi <bidi@apidemia.com>
1 parent 65346de commit febe856

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Benefits
2+
3+
- **Centralized setup** – Instead of hardcoding bootstrap code, you declare it in a config provider so it's easy to read, change, or extend.
4+
- **Modular** – Each package can ship with its own config without interfering with others.
5+
- **Container-friendly** – It works well with frameworks using DI containers like Laminas ServiceManager, PHP-DI, or Pimple.
6+
- **Standardized service definitions** - It has consistent rules for object creation that are separate from business logic.
7+
- **Environment-agnostic** - It returns an array that defines dev, test, or prod environments.
8+
- **Testability** - The consistent, central configuration promotes isolated (e.g. per-module) testing, easier swapping of dependencies and the assertion of pipeline setup (e.g. check if a config key is present).
9+
10+
> **Dotkernel ConfigProviders** have to be **added manually** in `config/config.php`, because all the initial ConfigProviders required to install the applications are already injected.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## How the ConfigProvider works
2+
3+
The ConfigProvider is automatically picked up by the framework during application bootstrap.
4+
Let's look at it step by step:
5+
6+
- **Merge the global configuration** - All ConfigProviders are merged into one array.
7+
- **Read the configuration array** - The call is similar to the below and expects an array of entries:
8+
9+
```php
10+
$config = $container->get('config')['key'] ?? [];
11+
```
12+
13+
- **Resolve item** - `$app->pipe()` is called to resolve one of the below instances:
14+
- Resolve the service name from the container
15+
- Wrap the middleware, if an array is provided
16+
- Call the closure or invokable object.
17+
- **Handle errors** - This middleware is the last one in the pipeline to make sure it handles any exceptions.
18+
- **Execute at runtime** - [Laminas Stratigility](https://docs.laminas.dev/laminas-stratigility/) iterates over the pipeline in the order it was registered.
19+
- Each middleware can **handle** the request and return a response, or **delegate** execution to the next middleware in the pipeline, until a `ResponseInterface` is returned to the client.
20+
21+
Below you can see how Mezzio and Dotkernel merge and use ConfigProviders to build the middleware pipeline and dependencies.
22+
23+
![Headless Platform with Core Submodule](https://docs.dotkernel.org/img/headless-platform/ConfigProvider.png)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Introduction
2+
3+
In PHP, the `ConfigProvider` is a class that is part of an application's bootstrap process.
4+
It returns an array of configuration, settings, or anything else your application needs.
5+
In Dotkernel, each application module and package contains a `ConfigProvider` that normally returns:
6+
7+
- Dependency injection mappings.
8+
- Request Handlers.
9+
- Template file paths.
10+
11+
Here is an example in Dotkernel, which is an approach similar to Laminas/Mezzio:
12+
13+
```php
14+
class ConfigProvider
15+
{
16+
public function __invoke(): array
17+
{
18+
return [
19+
'dependencies' => $this->getDependencies(),
20+
'templates' => $this->getTemplates(),
21+
];
22+
}
23+
24+
public function getDependencies(): array
25+
{
26+
return [
27+
'factories' => [
28+
Handler\HomepageHandler::class => Handler\HomepageHandlerFactory::class,
29+
Handler\SearchHandler::class => Handler\SearchHandlerFactory::class,
30+
],
31+
'invokables' => [
32+
Console\GenerateSearchData::class => Console\GenerateSearchData::class,
33+
],
34+
];
35+
}
36+
37+
public function getTemplates(): array
38+
{
39+
return [
40+
'app' => [__DIR__ . '/../templates/app'],
41+
'error' => [__DIR__ . '/../templates/error'],
42+
];
43+
}
44+
}
45+
```
46+
47+
All Dotkernel applications and packages use ConfigProviders:
48+
49+
- [Dotkernel API](https://docs.dotkernel.org/api-documentation/)
50+
- [Dotkernel Admin](https://docs.dotkernel.org/admin-documentation/)
51+
- [Dotkernel Frontend](https://docs.dotkernel.org/frontend-documentation/)
52+
- [Dotkernel Light](https://docs.dotkernel.org/light-documentation/)
53+
- [Dotkernel Packages](https://docs.dotkernel.org/packages/)

mkdocs.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ nav:
2020
- "Creation": v1/core/creation.md
2121
- "Usage": v1/core/usage.md
2222
- "Benefits": v1/core/benefits.md
23-
site_name: headless
24-
site_description: "Headless Platform"
23+
- Core:
24+
- "Config Provider":
25+
- "Introduction": v1/other/config-provider/introduction.md
26+
- "Functionality": v1/other/config-provider/functionality.md
27+
- "Benefits": v1/other/config-provider/benefits.md
28+
site_name: "Dotkernel Headless Platform"
29+
site_description: "Dotkernel Headless Platform"
2530
repo_url: "https://github.com/dotkernel/headless-documentation"
2631
plugins:
2732
- search

0 commit comments

Comments
 (0)