2017 © Pedro Peláez
 

library brunt

PHP7 Dependency Injection Framework

image

sysvyz/brunt

PHP7 Dependency Injection Framework

  • Tuesday, March 21, 2017
  • by mo-ba
  • Repository
  • 1 Watchers
  • 1 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

Brunt

Brunt is a simple but powerful dependency injection framework. Since php7, reflection can be used to analyze constructors properly., (*1)

Usage

Composer

  "require": {
    "sysvyz/brunt": "1.0.*",
  },

Test

Unit tests in /test/, (*2)

phpunit php vendor/bin/phpunit, (*3)

Include

simply use composer dump-autoload, (*4)

then include __DIR__ . '/vendor/autoload.php';, (*5)

Examples

Example 1: basic usage


$injector = new Brunt\Injector(null); /** @var Engine $engine */ $engine = $injector->get(Engine::class);

Example 2: magic get


$engine = $injector->get(Engine::class); //is equivalent to $engine = $injector->{Engine::class};

Example 3: define providers


$injector = new Injector(null); // TOKEN PROVIDER CLASS $injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)]); /** @var Engine $engine */ $engine = $injector->get(Engine::class);

Example 4: Binding

Bindings are a more convenient way to define Providers, (*6)

$injector->bind([

    bind('%SomeValue%')
    ->toValue(3.1415),

    bind(Car::class)
    ->toClass(Car::class),

    bind(Request::class)
        ->toFactory(function (Injector $injector) {
            return Request::createFromGlobals();
        })
])

Example 5: Singleton

Just call singleton() and the provider always returns the same object., (*7)

$injector = new Injector(null);

//                          TOKEN           PROVIDER            CLASS              SINGLETON
$injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)->singleton()]);

/** @var Engine $engine */
$engine = $injector->get(Engine::class);

or as binding, (*8)

$injector = new Injector(null);
$injector->bind([

    bind(Car::class)
    ->toClass(Car::class)->singleton(),

]);
$car = $injector->get(Car::class)

Example 6: Lazy

Just call lazy() and the provider returns a proxy object the real object will be created on first use., (*9)

$injector = new Injector(null);

//                          TOKEN           PROVIDER            CLASS              LAZY
$injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)->lazy()]);

/** @var Engine $engine */
$engine = $injector->get(Engine::class); //returns a proxy object

or as binding, (*10)

$injector = new Injector(null);
$injector->bind([

    bind(Car::class)
    ->toClass(Car::class)->lazy(),

]);
$car = $injector->get(Car::class) //returns a proxy object 

$car->honk() //creates the actual car and honks

the proxy object inherits from the actual class, so it can be used as if it was the object it passes instanceof and function parameter type declarations, (*11)

Example 7: Lazy and Singleton

combine lazy and singleton (order doesn't matter), (*12)


bind(Car::class)->lazy()->singleton()
ClassProvider::init(Car::class)->lazy()->singleton();

Example 7: Alias

... alias, (*13)

$injector->addProviders([
    HeavyEngine::class => ClassProvider::init(HeavyEngine::class)->lazy()
    Engine::class => AliasProvider::init(HeavyEngine::class)
]);
$heavyEngine = $injector->get(Engine::class); //returns a proxy object for HeavyEngine

Example 7: Hierarchy

coming up..., (*14)

Example Repo:

working example using brunt, (*15)

The Versions

21/03 2017

dev-master

9999999-dev https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection di

21/03 2017

1.0.2

1.0.2.0 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection di

15/03 2017

1.0.1

1.0.1.0 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection di

02/03 2017

1.0.0

1.0.0.0 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection di

29/05 2016

dev-invoker

dev-invoker https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

21/05 2016

1.0.0-beta.1.2

1.0.0.0-beta1.2 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

21/05 2016

1.0.0-beta.1.1

1.0.0.0-beta1.1 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

21/05 2016

1.0.0-beta.1

1.0.0.0-beta1 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

09/05 2016

1.0.0-alpha.0.4

1.0.0.0-alpha0.4 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

07/05 2016

1.0.0-alpha.0.3

1.0.0.0-alpha0.3 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

07/05 2016

1.0.0-alpha.0.1

1.0.0.0-alpha0.1 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

07/05 2016

1.0.0-alpha.0.2

1.0.0.0-alpha0.2 https://github.com/sysvyz/brunt

PHP7 Dependency Injection Framework

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

07/05 2016

dev-dev

dev-dev https://github.com/sysvyz/brunt

PHP7 Dependency Injection

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di

26/04 2016

1.0.0-alpha-0-1

1.0.0.0-alpha0-1 https://github.com/sysvyz/brunt

PHP7 Dependency Injection

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

dependency injection di