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.
The recommended way to install Linio Capsule is through composer.
{
"require": {
"linio/capsule": "0.1.*"
}
}To run the test suite, you need install the dependencies via composer, then run PHPUnit.
$ composer install
$ bin/phpunit
Preparing your capsule is quite simple. This is an example of a simple front-controller:
<?hhrequire'vendor/autoload.php';
useLinio\Capsule\Capsule;
classExampleService
{
publicfunction__construct(protected\DateTimeInterface$date): void
{
}
}
$capsule=newCapsule();
$capsule->register(\DateTimeInterface::class, ($capsule) ==>new\DateTime());
$capsule->register(ExampleService::class, ($capsule) ==>new\ExampleService($capsule->resolve(\DateTimeInterface::class)));
$capsule->resolve(ExampleService::class); // Instance of ExampleServiceYou 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.
<?hhrequire'vendor/autoload.php';
useLinio\Capsule\Capsule;
classExampleService
{
publicfunction__construct(protected\DateTimeInterface$date): void
{
}
}
$capsule=newCapsule();
$capsule->register(\DateTimeInterface::class, ($capsule) ==>new\DateTime());
$capsule->resolve(ExampleService::class); // Instance of ExampleService autowired via DateTimeInterface