nazg/glue

Dependency Injection Container For HHVM/Hack

Installs: 10 944

Dependents: 2

Suggesters: 0

Security: 0

Stars: 3

Watchers: 2

Forks: 0

Open Issues: 0

Language:Hack

1.5.1 2020-07-02 23:22 UTC

This package is auto-updated.

Last update: 2024-11-07 21:20:08 UTC


README

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;
}

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();