quillstack / di
The dependency injection container based on PSR-11: Container interface.
Requires
- php: ^8.0
- psr/container: ^2.0
Requires (Dev)
- quillstack/unit-tests: ^0.2.17
README
Quillstack DI Container is the dependency injection container based
on PSR-11: Container interface, and with the main goal: to be fast.
You can find the full documentation on the website:
https://quillstack.org/di
This DI container uses constructors and types of class properties.
Installation
To install this package, run the standard command using Composer:
composer require quillstack/di
Usage
You can use Quillstack DI Container when you want:
- To have a simple and fast DI container.
- Define dependencies based on interfaces.
- Define parameters e.g. credentials for a database in the
Database
class. - To use constructors or/and class properties.
- To implement your own instance factories e.g. for
Request
classes. - To use objects as dependencies.
Simple usage
You can easily start using a DI Container:
<?php use Quillstack\DI\Container; require __DIR__ . '/../vendor/autoload.php'; $container = new Container(); $controller = $container->get(ExampleController::class);
Where your ExampleController
class looks like:
<?php class ExampleController { private $example = 3; }
Dependencies based on interfaces
If you want to define which class should be loaded based on an interface:
$container = new Container([ LoggerInterface::class => Logger::class, ]); $controller = $container->get(ExampleController::class);
You can define your dependencies using interfaces:
<?php class ExampleController { public function __construct( private LoggerInterface $logger ) { } }
When you create the object using the DI container, the type of $logger
property will be set to Logger
.
Dependencies with parameters
If some of your classes require parameters, define them as an array passed on the second parameter to the container:
$container = new Container([ Database::class => [ 'hostname' => 'localhost', ], ]); $controller = $container->get(ExampleController::class);
Every time you will get a database object, a container will use localhost
as
a value for $hostname
parameter:
<?php class Database { public function __construct( private string $hostname ) { } }
Custom instance factories
You can implement your own instance factory. This is especially useful when you want to create many objects in a class family that are very similar in some way.
In our example we want to create different request objects:
<?php class CreateUserRequest implements RequestInterface { }
First, we had to create RequestInterface
as a common interface for all requests.
Next, we have to create an instance factory class. To create it, extend a class with CustomFactoryInterface
:
<?php use Quillstack\DI\CustomFactoryInterface; class RequestClassFactory implements CustomFactoryInterface { /** * {@inheritDoc} */ public function create(string $id): object { $factory = $this->container->get(GivenRequestFromGlobalsFactory::class); return $factory->createGivenServerRequest($id); } }
Also, use this configuration array when you create a DI container:
$container = new Container([ RequestInterface::class => RequestClassFactory::class, ]); $controller = $container->get(ExampleController::class);
Custom factories are useful for objects you want to create similarly.
Dependencies as objects
In this example, whenever a new class of LoggerInterface will be required as a dependency, a container will use a previously defined object. This object can be created once in a bootstrap file and used in the entire application:
$logger = new Logger('name'); $logger->pushHandler(new StreamHandler('var/app.log');); $container = new Container([ LoggerInterface::class => $logger, ]);
This configuration is helpful if an object should be created once and its instance should be used in other places in the application.
Unit tests
Run tests using a command:
phpdbg -qrr ./vendor/bin/unit-tests
Docker
$ docker-compose up -d
$ docker exec -w /var/www/html -it quillstack_di sh