Skip to content
This repository has been archived by the owner on Jun 1, 2020. It is now read-only.

Latest commit

 

History

History
85 lines (60 loc) · 2.22 KB

README.md

File metadata and controls

85 lines (60 loc) · 2.22 KB

Linio Capsule

Latest Stable Version License Build Status

Linio Capsule is a highly opinionated dependency injection container for Hack, helping you map open generic abstractions to open generic implementations. It aims to take advantage of Hack's features to provide a highly efficient, type-safe, non-invasive container. It also supports dynamic wiring via constructor dependencies.

Install

The recommended way to install Linio Capsule is through composer.

{
    "require": {
        "linio/capsule": "0.1.*"
    }
}

Tests

To run the test suite, you need install the dependencies via composer, then run PHPUnit.

$ composer install
$ bin/phpunit

Usage

Preparing your capsule is quite simple. This is an example of a simple front-controller:

<?hh

require 'vendor/autoload.php';

use Linio\Capsule\Capsule;

class ExampleService
{
    public function __construct(protected \DateTimeInterface $date): void
    {
    }
}

$capsule = new Capsule();
$capsule->register(\DateTimeInterface::class, ($capsule) ==> new \DateTime());
$capsule->register(ExampleService::class, ($capsule) ==> new \ExampleService($capsule->resolve(\DateTimeInterface::class)));

$capsule->resolve(ExampleService::class); // Instance of ExampleService

Dynamic wiring

You can also leave the job to Capsule if you don't want to manually wire dependencies. This feature depends on dependency injection via constructor arguments. Also, Capsule will only autowrite non-optional dependencies.

<?hh

require 'vendor/autoload.php';

use Linio\Capsule\Capsule;

class ExampleService
{
    public function __construct(protected \DateTimeInterface $date): void
    {
    }
}

$capsule = new Capsule();
$capsule->register(\DateTimeInterface::class, ($capsule) ==> new \DateTime());
$capsule->resolve(ExampleService::class); // Instance of ExampleService autowired via DateTimeInterface