Skip to content

Repository files navigation

Quillstack DI Container

Build StatusDownloadsCoverageLines of CodeStyleCICodeFactorPackagist LicenseReliability RatingMaintainabilitySecurity RatingPackagist PHP Version Support

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:

<?phpuseQuillstack\DI\Container;
require__DIR__ . '/../vendor/autoload.php';
$container = newContainer();
$controller = $container->get(ExampleController::class);

Where your ExampleController class looks like:

<?phpclass ExampleController
{
private$example = 3;
}

Dependencies based on interfaces

If you want to define which class should be loaded based on an interface:

$container = newContainer([
LoggerInterface::class => Logger::class,
]);
$controller = $container->get(ExampleController::class);

You can define your dependencies using interfaces:

<?phpclass ExampleController
{
publicfunction__construct(
privateLoggerInterface$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 = newContainer([
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:

<?phpclass Database
{
publicfunction__construct(
privatestring$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:

<?phpclass 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:

<?phpuseQuillstack\DI\CustomFactoryInterface;
class RequestClassFactory implements CustomFactoryInterface
{
/** * {@inheritDoc} */publicfunctioncreate(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 = newContainer([
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 = newLogger('name');
$logger->pushHandler(newStreamHandler('var/app.log'););
$container = newContainer([
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

About

The dependency injection container, based on PSR-11: Container interface.

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Contributors

Languages