2017 © Pedro Peláez
 

library di

Goez Dependency Injection Container

image

goez/di

Goez Dependency Injection Container

  • Wednesday, May 23, 2018
  • by jaceju
  • Repository
  • 0 Watchers
  • 3 Stars
  • 15,123 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 14 % Grown

The README.md

Goez Dependency Injection Container

Build Status, (*1)

A simple dependency injection container which was inspired by Laravel Service Container., (*2)

Features

  • Nested dependency injection.
  • Interface binding.

TODO

  • Method injection

Installation

$ composer require goez/di

Usage

Initialize

use Goez\Di\Container;
$container = Container::createInstance();

make($name[, $arguments])

Make an instance:, (*3)

class App
{
    private $appName;

    public function __construct($appName = 'ThisApp')
    {
        $this->appName = $appName;
    }

    public function getAppName()
    {
        return $this->appName;
    }
}

$app = $container->make(App::class);
echo $app->getAppName(); // ThisApp

$app = $container->make(App::class, ['MyApp']);
echo $app->getAppName(); // MyApp

Inject object by type-hint:, (*4)

class App
{
    private $auth;
    private $appName;

    public function __construct(Auth $auth, $appName = 'ThisApp')
    {
        $this->auth = $auth;
        $this->appName = $appName;
    }
}

class Auth {}

$app = $container->make(App::class);

Nested dependency injection:, (*5)

class App
{
    private $auth;
    private $appName;

    public function __construct(Auth $auth, $appName = 'ThisApp')
    {
        $this->auth = $auth;
        $this->appName = $appName;
    }
}

class Auth 
{
    private $db;

    public function __construct(Db $db)
    {
        $this->db = $db;
    }
}

$app = $container->make(App::class);

bind($name, $className|$closure)

Binding by key:, (*6)

$container->bind('db', function ($container) {
    return new Db();
});
$db = $container->make('db');

Binding by interface:, (*7)

interface DbInterface {}

class Db implements DbInterface {}

$container->bind(DbInterface::class, Db::class);
$db = $container->make(DbInterface::class);

instance($name, $instance)

Bind an existed instance., (*8)

interface DbInterface {}

class Db implements DbInterface {}

$container->instance(DbInterface::class, new Db());
$db1 = $container->make(DbInterface::class);

$container->instance(DbInterface::class, new Db());
$db2 = $container->make(DbInterface::class);

assert($db1 !== $db2); // true

singleton($name, $instance|$closure)

Create singleton instance by closure., (*9)

interface DbInterface {}

class Db implements DbInterface {}

$container->singleton(DbInterface::class, function (Container $c) {
    return $c->make(Db::class);
    // Or
    return new Db();
});
$db1 = $container->make(DbInterface::class);
$db2 = $container->make(DbInterface::class);

assert($db1 === $db2);

Create singleton instance by an existed instance., (*10)

interface DbInterface {}

class Db implements DbInterface {}

$container->singleton(DbInterface::class, new Db());
$db1 = $container->make(DbInterface::class);
$db2 = $container->make(DbInterface::class);

assert($db1 === $db2);

License

MIT, (*11)

The Versions

23/05 2018

dev-master

9999999-dev

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

23/05 2018

1.0.8

1.0.8.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

23/05 2018

1.0.7

1.0.7.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

06/04 2017

1.0.6

1.0.6.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

19/01 2017

1.0.5

1.0.5.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

12/12 2016

1.0.4

1.0.4.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

12/12 2016

1.0.3

1.0.3.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

08/12 2016

1.0.2

1.0.2.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

08/12 2016

1.0.1

1.0.1.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju

07/12 2016

1.0.0

1.0.0.0

Goez Dependency Injection Container

  Sources   Download

MIT

The Requires

  • php >= 5.6

 

The Development Requires

by Jace Ju