https://github.com/kodedphp/container
A simple Dependency Injection Container with application modules support
https://github.com/kodedphp/container
container dependency dependency-injection injection psr-11
Last synced: 5 months ago
JSON representation
A simple Dependency Injection Container with application modules support
- Host: GitHub
- URL: https://github.com/kodedphp/container
- Owner: kodedphp
- License: bsd-3-clause
- Created: 2019-09-09T21:05:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-25T16:41:07.000Z (about 4 years ago)
- Last Synced: 2025-07-27T23:22:02.261Z (6 months ago)
- Topics: container, dependency, dependency-injection, injection, psr-11
- Language: PHP
- Size: 111 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Dependency Injection Container - Koded
--------------------------------------
[](https://github.com/kodedphp/container/actions/workflows/unit-tests.yml)
[](https://packagist.org/packages/koded/container)
[](https://php.net/)
`koded/container` is an OOP application bootstrapping and wiring library.
In other words, `Koded\DIContainer` implements a **design pattern** called **Dependency Inversion**.
The main principle of DIP is to separate the behavior from dependency resolution.
```bash
composer require koded/container
```
## Example
Let's look at a blog application that has
- interfaces for the database repositories and corresponding implementations
- a shared PDO instance
- a service class for the blog content fetching
- a handler class that maps the request method
```php
use PDO;
interface PostRepository {
public function findBySlug(string $slug);
}
interface UserRepository {
public function findById(int $id);
}
class DatabasePostRepository implements PostRepository {
private $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
public function findBySlug(string $slug) {
// $this->pdo ...
}
}
class DatabaseUserRepository implements UserRepository {
private $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
public function findById(int $id) {
// $this->pdo ...
}
}
```
Somewhere we may have a service class that uses the dependent repositories:
```php
class PostService {
private $post, $user;
public function __construct(PostRepository $post, UserRepository $user) {
$this->post = $post;
$this->user = $user;
}
// a service method that uses the post and user repository instances
public function findBlogPostBySlug(string $slug) {
$post = $this->post->findBySlug($slug);
$user = $this->user->findById($post->userId());
// ... do something with the results, create a result data structure...
}
}
```
Then somewhere we might have a handler/controller that asks for its own dependencies:
```php
class BlogHandler {
public function get(ServerRequestInterface $request, PostService $service): ResponseInterface {
$slug = slugify($request->getUri()->getPath());
$post = $service->findBlogPostBySlug($slug);
// some PSR-7 compatible response object
return new ServerResponse($post);
}
}
```
### Wire All The Things
This is the bootstrapping / wiring application module
(or container's "configuration" class) where all known dependencies
are binded and shared
```php
class BlogModule implements DIModule {
public function configure(DIContainer $container): void {
// bind interfaces to concrete class implementations
$container->bind(PostRepository::class, DatabasePostRepository::class);
$container->bind(UserRepository::class, DatabaseUserRepository::class);
$container->bind(ServerRequestInterface::class, /*some PSR-7 server request class name*/);
// share one PDO instance
$container->singleton(PDO::class, ['sqlite:database.db']);
}
}
```
And finally in the dispatcher file, we process the request
```php
// index.php
// (resolved through an HTTP router or other means)
$handler = BlogHandler::class;
$method = 'get';
// by invoking the container
$response = (new DIContainer(new BlogModule))([$handler, $method]);
// we have a `$response` object to output the content
// ex. `echo $response->getBody()->getContents();`
```
The container implements the [__invoke()][invoke] method, so the instance can be used as a function:
```php
$container('method', ['arg1', 'arg2', ...]);
```
> To be continued...
Code quality
------------
[](https://scrutinizer-ci.com/g/kodedphp/container/?branch=master)
[](https://scrutinizer-ci.com/g/kodedphp/container/?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/kodedphp/container/master)
```shell script
vendor/bin/infection --threads=4
vendor/bin/phpbench run --report=default
vendor/bin/phpunit
```
License
-------
[](LICENSE)
The code is distributed under the terms of [The 3-Clause BSD license](LICENSE).
[invoke]: https://php.net/manual/en/language.oop5.magic.php#object.invoke