Dependency Injection Container For Hack
HHVM 4.35.0 and above.
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 installIn addition, you will need to use hhvm-autoload as your autoloader.
or
$ composer require nazg/glueinterface AnyInterface {
}final class Any implements AnyInterface {
// any
}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);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 |
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();use type Nazg\Glue\ContainerBuilder;
$builder = new ContainerBuilder(true, 'apc.cache.key.name');
// return a \Nazg\Glue\CachedContainer Instance
$container = $builder->make();