2017 © Pedro Peláez
 

library hh-container

simple light weight service location / dependency injection container

image

ytake/hh-container

simple light weight service location / dependency injection container

  • Thursday, May 17, 2018
  • by ytake
  • Repository
  • 2 Watchers
  • 2 Stars
  • 410 Installations
  • Hack
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 1 % Grown

The README.md

This Project Has Been Deprecated

HH-Container

simple light weight service location / dependency injection container, (*1)

Build Status, (*2)

Installation

$ hhvm $(which composer) require ytake/hh-container
"require": {
  "hhvm": ">=3.24",
  "ytake/hh-container": "^1.0"
},

Usage

$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing', $container ==> 'testing');
$container->get('testing'); // return string

Singleton or Prototype

default prototype, (*3)

SINGLETON

$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('scope:singleton', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::Singleton);

PROTOTYPE

$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('scope:prototype', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::Prototype);

Dependency Injection

set parameters

sample class, (*4)

final class MessageClass {
  public function __construct(
    protected string $message
  ) {}

  public function message(): string {
    return $this->message;
  }
}

final class MessageClient {
  public function __construct(
    protected MessageClass $message
  ) {}

  public function message(): MessageClass {
    return $this->message;
  }
}

Inject

$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('message.class', $container ==> new MessageClass('testing'));
$container->set(MessageClient::class, $container ==> {
  $instance = $container->get('message.class');
  invariant($instance instanceof MockMessageClass, 'error');
  new MessageClient($instance);
});
$instance = $container->get(MessageClient::class);

callable

returns the value of a callable with parameters supplied at calltime., (*5)

final class TestingInvokable {
  public function __invoke(FactoryContainer $container): int {
    return 1;
  }
}

$container = new \Ytake\HHContainer\FactoryContainer();
$container->set(TestingInvokable::class, $container ==> 
  $container->callable(
    new \Ytake\HHContainer\Invokable(
      new TestingInvokable(), '__invoke', $container
    )
  )
);

Use modules


use Ytake\HHContainer\ServiceModule; use Ytake\HHContainer\FactoryContainer; class ExampleModule extends ServiceModule { public function provide(FactoryContainer $container): void { $container->set('example', $container ==> new \stdClass()); } }
$container = new \Ytake\HHContainer\FactoryContainer();
$container->register(ExampleModule::class);
$container->lockModule();

Service Factory

use Ytake\HHContainer\Scope;
use Ytake\HHContainer\ServiceFactory;
use Ytake\HHContainer\FactoryContainer;
use Ytake\HHContainer\FactoryInterface;

class StringFactory implements FactoryInterface {
  const type T = string;
  public function provide(FactoryContainer $_container): StringFactory::T {
    return 'testing';
  }
  public function scope(): Scope {
    return Scope::Singleton;
  }

  public function name(): string {
    return 'testing'
  }
}

$factory = new ServiceFactory(new FactoryContainer());
$factory->registerFactory(new StringFactory());
$factory->create('testing');

The Versions

17/05 2018

dev-feature/refactor-resolve-types

dev-feature/refactor-resolve-types

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

07/05 2018

dev-master

9999999-dev

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

07/05 2018

0.5.2

0.5.2.0

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

06/03 2018

0.5.1

0.5.1.0

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

06/01 2018

0.5.0

0.5.0.0

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

06/01 2018

dev-feature/added-async-resolver

dev-feature/added-async-resolver

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

04/11 2017

0.4.0

0.4.0.0

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm

31/10 2017

0.3.0

0.3.0.0

simple light weight service location / dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

container hack hhvm