2017 © Pedro Peláez
 

library dependency-injection

Descent Framework Dependency Injection Component

image

descent/dependency-injection

Descent Framework Dependency Injection Component

  • Friday, February 17, 2017
  • by nihylum
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

dependency-injection

Descent Framework Dependency Injection Container, (*1)

The dependency builder

This component serves a general dependency builder implementation implemented as Descent\Services\DependencyBuilder. The dependency builder orchestrates dependencies by constructing them without configuration. Optionally provided parameters and a list of enforced parameters are priorized in the building process., (*2)

Building an object from a interface factory
use Descent\Services\{
    DependencyBuilder,
    Entities\Factory
};

$builder = new DependencyBuilder();
$factory = new Factory(
    DateTimeInterface::class, 
    function(string $time, string $zone = null) {
        return new DateTime(
            $time, 
            new DateTimeZone($zone ?? 'europe/berlin')
        );
    }
);

$object = $builder->build($factory, ['time' => 'now']);
Building an object from a interface
use Descent\Services\{
    DependencyBuilder
};

class Foo {

}

class Bar {
    protected $foo;

    public function __constrct(Foo $foo)
    {
        $this->foo = $foo;
    }
}

$builder = new DependencyBuilder();

$object = $builder->make(Bar::class);

The dependency injection container

This component serves a general dependency injection container implementation implemented as Descent\Services\DependencyInjectionContainer. The dependency injection container allows to register services as interface concrete assignments (service) as well as interface callback assignments (factories). The dependency injection container extends the dependency builder and alters the interface resolving of the dependency builder to seek for registered interfaces first., (*3)

Building objects from registered services
use Descent\Services\{
    DependencyInjectionContainer
};

class Foo {

}

class Bar {
    protected $foo;

    public function __constrct(Foo $foo)
    {
        $this->foo = $foo;
    }
}

$container = new DependencyInjectionContainer();

$container->bind(Foo::class)->singleton();
$container->bind(Bar::class);

$object = $container->make(Bar::class);
Building objects from registered factories
use Descent\Services\{
    DependencyInjectionContainer
};

class Foo {

}

class Bar {
    protected $foo;
    protected $bar;

    public function __constrct(Foo $foo, Foo $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }
}

$container = new DependencyInjectionContainer();

$container->bind(Foo::class)->singleton();
$container->factory(
    Bar::class, 
    function(Foo $foo, Foo $bar = null) {
        return new Bar($foo, $bar ?? $foo);
    }
)->enforceParameters('bar');

$object = $container->make(Bar::class);

The Versions

17/02 2017

dev-master

9999999-dev

Descent Framework Dependency Injection Component

  Sources   Download

MIT

The Requires

 

by Matthias Kaschubowski