Dependency Injection Container For HHVM/Hack


Keywords
container, hack, hhvm, dependency-injection, hacklang, servicelocator
License
MIT

Documentation

Nazg\Glue

Dependency Injection Container For Hack

Travis (.org) Packagist Packagist Version Packagist

Requirements

HHVM 4.35.0 and above.

Installation

Composer is the recommended installation method.
To add Nazg\Glue to your project, add the following to your composer.json then re-run composer:

  "require": {
    "nazg/glue": "^1.4"
  }

Run Composer commands using HHVM like so:

$ composer install

In addition, you will need to use hhvm-autoload as your autoloader.

or

$ composer require nazg/glue

Usage

First steps

Create Class

interface AnyInterface {

}
final class Any implements AnyInterface {
  // any
}

Bindings

use type Nazg\Glue\Container;
use type Nazg\Glue\Scope;

$container = new Container();
$container->bind(AnyInterface::class)
  ->to(Mock::class)
  ->in(Scope::PROTOTYPE);
\HH\Asio\join($container->lockAsync());

dependencies will be automatically resolved

$container->get(AnyInterface::class);

Scopes

use the Nazg\Glue\Scope enum.

enum Scope : int {
  PROTOTYPE = 0;
  SINGLETON = 1;
}
enum
Nazg\Glue\Scope\PROTOTYPE single instance
Nazg\Glue\Scope\SINGLETON prototype instance

Providers

use \Nazg\Glue\ProviderInterface.

use type Nazg\Glue\ProviderInterface;

final class AnyProvider implements ProviderInterface<AnyInterface> {

  public function get(): AnyInterface {
    return new Any();
  }
}
$container->bind(AnyInterface::class)
  ->provider(new AnyProvider();

Binding Serialization Cache

use type Nazg\Glue\ContainerBuilder;

$builder = new ContainerBuilder(true, 'apc.cache.key.name');
// return a \Nazg\Glue\CachedContainer Instance
$container = $builder->make();