项目作者: nazg-hack

项目描述 :
Dependency Injection Container For HHVM/Hack
高级语言: Hack
项目地址: git://github.com/nazg-hack/glue.git
创建时间: 2018-12-22T13:46:23Z
项目社区:https://github.com/nazg-hack/glue

开源协议:MIT License

下载


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:

  1. "require": {
  2. "nazg/glue": "^1.4"
  3. }

Run Composer commands using HHVM like so:

  1. $ composer install

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

or

  1. $ composer require nazg/glue

Usage

First steps

Create Class

  1. interface AnyInterface {
  2. }
  1. final class Any implements AnyInterface {
  2. // any
  3. }

Bindings

  1. use type Nazg\Glue\Container;
  2. use type Nazg\Glue\Scope;
  3. $container = new Container();
  4. $container->bind(AnyInterface::class)
  5. ->to(Mock::class)
  6. ->in(Scope::PROTOTYPE);
  7. \HH\Asio\join($container->lockAsync());

dependencies will be automatically resolved

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

Scopes

use the Nazg\Glue\Scope enum.

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

Providers

use \Nazg\Glue\ProviderInterface.

  1. use type Nazg\Glue\ProviderInterface;
  2. final class AnyProvider implements ProviderInterface<AnyInterface> {
  3. public function get(): AnyInterface {
  4. return new Any();
  5. }
  6. }
  1. $container->bind(AnyInterface::class)
  2. ->provider(new AnyProvider();

Binding Serialization Cache

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